1

this is my serilog configuration :

Log.Logger = new LoggerConfiguration()
.WriteTo.RollingFile(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                     @"Logs\log-{Date}.txt"))
.MinimumLevel.ControlledBy(LogLevelSwitch)
.CreateLogger();

i'm logging like this :

var log = Serilog.Log.ForContext<SomeClassName>();
log.Information("some log text");

My question :

I need a modification in my configuration to create separate logs for each SourceContext.

I need log files somthing like Logs\log-{Date}-{SourceContext}.txt

thanks pros...

Manoochehr Dadashi
  • 715
  • 1
  • 10
  • 28

1 Answers1

3

One way to implement this is to create your own ILogEventSink implementation (the interface is very simple) that internally dispatches to one of several RollingFileSinks based on the SourceContext property of the LogEvent.

To configure it (let's say you call your sink SourceRollingFileSink) you can use WriteTo.Sink():

Log.Logger = new LoggerConfiguration()
  .WriteTo.Sink(new SourceRollingFileSink())
  .CreateLogger();

There's no out-of-the-box implementation so some work is required, but it should not be complicated. Be aware that implementations of ILogEventSink need to be thread-safe.

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101