MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
452 stars 55 forks source link

Is there any API for capturing console messages? #2982

Closed daviduuang closed 1 year ago

daviduuang commented 1 year ago

I cannot find any API for capturing console messages.

The situation is I need to capture console messages and process them externally, e.g. submit all console log messages to Exceptionless .

In the past, I use CefSharp.WinForms to capture conole messages:

public event EventHandler\<ConsoleMessageEventArgs> ConsoleMessage;

//     Event arguments to the ConsoleMessage event handler set up in IWebBrowser.
public class ConsoleMessageEventArgs : EventArgs
{
    public ConsoleMessageEventArgs(string message, string source, int line);

    //     The message text of the console message.
    public string Message { get; }

    //     The source of the console message.
    public string Source { get; }

    //     The line number that generated the console message.
    public int Line { get; }
}

But I cannot find any similar APIs in WebView2.

victorthoang commented 1 year ago

Hello @daviduuang,

Thanks for your inquiry! I've assigned this to a dev that might best answer your question.

daviduuang commented 1 year ago

Recently, I found a way to intercept new log entries of browser by using Chrome DevTools Protocol.

In Domain "Log", you can enable\capture logs from webview2.

Using Method "CoreWebView2.GetDevToolsProtocolEventReceiver(String) ", you can regist event receiver to capture it.

The demo code works, as follows: await this.webView.CoreWebView2.CallDevToolsProtocolMethodAsync("Log.enable", "{}"); var logEventReceiver = webView.CoreWebView2.GetDevToolsProtocolEventReceiver("Log.entryAdded"); logEventReceiver.DevToolsProtocolEventReceived += (s, e) => { Console.WriteLine(e.ParameterObjectAsJson); };


Reference:

  1. 在 WebView2 应用中使用 Chrome DevTools 协议
  2. CoreWebView2.GetDevToolsProtocolEventReceiver(String) Method
  3. Log Domain
  4. WebView2简单试用(九)—— Dev Protocol
  5. How do I enable Chrome DevTools Experiments?
  6. https://github.com/ChromeDevTools/devtools-protocol
oggy22 commented 1 year ago

Hi @daviduuang! It seems that you found the answer yourself. I am closing this issue.