Reputation: 1325
I am trying to implement serilog in a little sample-project. I´ve did the following so far:
My question:
Is there a possibility to configure two different loggers, based on CategoryName (in my case "MyLogger")? I want this logger to use a different file, than my default-logger. In my happy-world-fantasy it would look like:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.File(new CompactJsonFormatter(), @"C:\temp\standardLog.txt")
.WriteTo.File(new CompactJsonFormatter(), @"C:\temp\specialLog.txt").Filter.ByIncludingOnly(x => x.SourceContext("MySpecialLogger")
.CreateLogger();
And if I create a new Logger var logger = _loggerFactory.CreateLogger("MySpecialLogger");
the logs will be safed in my specialLog.txt-file.
Any ideas?
Upvotes: 2
Views: 4831
Reputation: 31797
You need Serilog.Sinks.Map for this.
dotnet add <PROJECT> package Serilog.Sinks.Map -v 1.0.0-dev-00012
Then:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Map("SourceContext", null, (sc, wt) =>
wt.File(new CompactJsonFormatter(), sc == "MySpecialLogger" ?
@"C:\temp\specialLog.txt" :
@"C:\temp\standardLog.txt"))
.CreateLogger();
Upvotes: 1