8

While I have a general purpose logger for errors, exceptions etc that use rolling file sink to log stuff to my-{date}.log.

However, I need another instance for audit to audit-{date}.log and another instance to write perf info to perf-{date}.log.

How do I create multiple instances of serilog with different configuration or sink?

DarthVader
  • 52,984
  • 76
  • 209
  • 300
  • Interesting question, would [Expression-based event filtering](https://github.com/serilog/serilog-filters-expressions) help? – Mark G Jul 24 '18 at 18:32
  • Take a look at Christo's answer https://stackoverflow.com/questions/51213779/can-i-log-to-separate-files-using-serilog – John81 Feb 27 '19 at 14:22

2 Answers2

0

You can do this like following:

        var loggerConfiguration = new LoggerConfiguration()
                       .MinimumLevel.Verbose()
                       .Enrich.FromLogContext();
        var fileBasePath = "<base log path>";
        loggerConfiguration
            .WriteTo.Console()
            .WriteTo.RollingFile(fileBasePath + "log-info-{Date}.txt")
            .WriteTo.Logger(fileLogger => fileLogger
                    .MinimumLevel.Error()
                    .WriteTo.RollingFile(fileBasePath + "log-error-{Date}.txt"))
            .WriteTo.Logger(fileLogger => fileLogger
                    .Filter.ByIncludingOnly(x =>
                          x.Level == LogEventLevel.Information &&
                          x.Properties.ContainsKey("<Audit Key>"))
                    .WriteTo.RollingFile(fileBasePath + "log-audit-{Date}.txt"));
        Log.Logger = loggerConfiguration.CreateLogger();

Note: Audit Key is a key which used in audit log

adem caglin
  • 22,700
  • 10
  • 58
  • 78
0

I assume you would need different interface name to write audit and perf data (to be injected to your main business code). If that's the case just implement your IAudit, IPerfData with a new Serilog instance which you can totally customize where/how you save the log.

Calvin
  • 1,153
  • 2
  • 14
  • 25