Click or drag to resize

How To Redirect log4net logs to Serilog

This page shows you how to produce a Serilog log which will include all the log messages sent to log4net. This is very useful because the Lyncée Tec libraries send logs to log4net, but Lyncée Tec will switch to the more modern Serilog for future applications. We thus recommend the user of Serilog as a logging library.

How To

  1. Install the Lyncée Tec LogTools NuGet package. It will automatically install the log4net package as well as the core package for Serilog.

  2. Install any Serilog sink package required for your logging. The most common are Serilog.Sinks.File and Serilog.Sinks.Console

  3. In your code, configure Serilog as you would normally do. See below for a snippet example.

  4. Call GetExceptionMessageAndStackTrace once to configure the redirecting.

Your Serilog log should now include the Lyncée Tec libraries' logs, which were initially sent to log4net.
Example
log4net redirection to Serilog
using LynceeTec.LogTools;
using Serilog;

...

static void Main(string[] args)
{
    //Configure Serilog
    Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.File("Log.txt")
    .CreateLogger();

    //Configure log4Net redirection to serilog
    Log4NetToSerilogConfiguration.Configure();

    ...

}
We recommend using LynceeTec.LogTools.LogHelper.GetExceptionMessagesAndStackTraces(System.Exception) to recursively log exceptions and all their sub and inner exceptions, including all stack traces.
See Also