Skip to content

A System.Diagnostics.TraceListener that writes trace data into Serilog

License

Notifications You must be signed in to change notification settings

jotabe-net/SerilogTraceListener

 
 

Repository files navigation

Serilog TraceListener NuGet Build status

This library provides a System.Diagnostics.TraceListener implementation that outputs to Serilog. This means that output from third-party libraries using System.Diagnostics.Trace can be collected through the Serilog pipeline.

Getting started

Before using this package, Serilog needs to be installed and configured in the application.

Installing from NuGet

The package on NuGet is SerilogTraceListener:

Install-Package SerilogTraceListener -DependencyVersion Highest

Enabling the listener (code)

After configuring Serilog, create a SerilogTraceListener and add it to the System.Diagnostics.Trace.Listeners collection:

var listener = new global::SerilogTraceListener.SerilogTraceListener();
Trace.Listeners.Add(listener);

This will write the events through the static Log class. Alternatively, a specific logger instance can be used instead:

var listener = new SerilogTraceListener.SerilogTraceListener(specificLoggerInstance);

Enabling the listener (XML)

To enable the listener through XML in App.config or Web.config, add it to the system.diagnostics/trace/listeners collection:

  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="Serilog"
             type="SerilogTraceListener.SerilogTraceListener, SerilogTraceListener"
             initializeData="Some.Source.Context" />
      </listeners>
    </trace>
  </system.diagnostics>

A SourceContext value can optionally be provided through initializeData.

To log a specific trace source, configure it in this way:

  <system.diagnostics>
    <sharedListeners>
      <add name="serilog" type="SerilogTraceListener.SerilogTraceListener, SerilogTraceListener" />
    </sharedListeners>
    <sources>
      <source name="Example.Source.Name" switchValue="All">
        <listeners>
          <clear/>
          <add name="serilog"/>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
</configuration>
``

For using XML configuration, as it's not possible to pass a logger instance through XML configuration, it's necessary to set the static `Log.Logger` in the application code, which will be used for logging. If `log.Logger` is not set, it fails silently.

About

A System.Diagnostics.TraceListener that writes trace data into Serilog

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • C# 95.6%
  • PowerShell 4.4%