8
votes

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?

2
Interesting question, would Expression-based event filtering help? - Mark G
Take a look at Christo's answer stackoverflow.com/questions/51213779/… - John81

2 Answers

0
votes

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

0
votes

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.