Logging

This guide describes how to configure logging for the library.

The root cause of many issues can be detected by analyzing DotNetBrowser log messages.

By default, logging in DotNetBrowser is disabled. But if you encounter an issue or observe some unexpected behavior, we recommend you to do the following: 1. Configure DotNetBrowser to save all log messages to a file. 2. Reproduce the issue. 3. Submit a ticket with the collected log messages for further investigation.

Levels of logging

DotNetBrowser supports the following logging levels: All > Verbose > Information > Warning > Error > Critical > Off.

The level Off can be used to turn off logging completely, and All is used to enable logging of all messages, including messages from Chromium.

You can change the logging level using the LoggerProvider.Instance.Level property.

For more information on levels of logging, refer to MSDN description.

Example: Setting Logging Level

To save all log messages with the logging level Verbose and higher, use the DotNetBrowser Logging API as shown in the code sample below:

using DotNetBrowser.Logging;
using System.Diagnostics;
// ...
LoggerProvider.Instance.Level = SourceLevels.Verbose;
Imports DotNetBrowser.Logging
Imports System.Diagnostics
' ...
LoggerProvider.Instance.Level = SourceLevels.Verbose

Logging to a file

To print all log messages to a file, use the LoggerProvider.Instance.FileLoggingEnabled property of DotNetBrowser Logging API as shown in the code sample below:

using DotNetBrowser.Logging;
// ...
LoggerProvider.Instance.FileLoggingEnabled = true;
LoggerProvider.Instance.OutputFile = "C:\\log.txt";
Imports DotNetBrowser.Logging
' ...
LoggerProvider.Instance.FileLoggingEnabled = True
LoggerProvider.Instance.OutputFile = "C:\log.txt"

In the sample above, the value of the LoggerProvider.Instance.OutputFile property represents an absolute or relative path to a file where the log messages are stored.

Redirect DotNetBrowser logging

DotNetBrowser utilizes TraceSource for its logging. It is possible to redirect DotNetBrowser logs to another logger that is used in your application. For this purpose, you need to create your own class that derives from TraceListener and performs redirection.

For instance, this is how it may look like for Serilog:

namespace Example
{
    public class SerilogTraceListener : TraceListener
    {
        private readonly string initializeData;
        private readonly Lazy<ILogger> logger;

        public SerilogTraceListener(string initializeData)
        {
            this.initializeData = initializeData;
            logger = new Lazy<ILogger>(() => Log.ForContext("Context", initializeData));
        }

        public override void Write(string message)
        {
            logger.Value.Information(message);
        }

        public override void WriteLine(string message)
        {
            logger.Value.Information(message);
        }
    }
}
Namespace Example
	Public Class SerilogTraceListener
		Inherits TraceListener

		Private ReadOnly initializeData As String
		Private ReadOnly logger As Lazy(Of ILogger)

		Public Sub New(ByVal initializeData As String)
			Me.initializeData = initializeData
			logger = New Lazy(Of ILogger)(Function()
				Return Log.ForContext("Context", initializeData)
			End Function)
		End Sub

		Public Overrides Sub Write(ByVal message As String)
			logger.Value.Information(message)
		End Sub

		Public Overrides Sub WriteLine(ByVal message As String)
			logger.Value.Information(message)
		End Sub
	End Class
End Namespace

Then, it is necessary to configure your trace listener class for TraceSource. For instance, this can be done via App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.diagnostics>
    <trace autoflush="true"/>
    <sources>
      <source name="Browser" switchValue="All">
        <listeners>
          <add name="SerilogTraceListener"
               type="Example.SerilogTraceListener, ExampleAssembly"
               initializeData="DotNetBrowser" />
        </listeners>
      </source>
      <source name="Process" switchValue="All">
        <listeners>
          <add name="SerilogTraceListener"
               type="Example.SerilogTraceListener, ExampleAssembly"
               initializeData="DotNetBrowser" />
        </listeners>
      </source>
      <source name="IPC" switchValue="All">
        <listeners>
          <add name="SerilogTraceListener"
               type="Example.SerilogTraceListener, ExampleAssembly"
               initializeData="DotNetBrowser" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
</configuration> 

You can find more information on configuring the trace listeners in the corresponding article: How to: Create and Initialize Trace Sources

Go Top