I'm using serilog to log all the Web API trace events in to one file and all the code debug to another file by the below code:
The problem is that the trace.json is also logging Debug events as well which I suspect is because of the minimumLevel filter.
How do I separate the events into two files ?
Tried this Question, but it doesn't write the file at all.
Using latest serilog version.
Log.Logger = new LoggerConfiguration()
.WriteTo.Trace(outputTemplate: "{Timestamp} [{Level}] ({HttpRequestId}|{UserName}) {Message}{NewLine}{Exception}")
.MinimumLevel.Debug()
.WriteTo.Sink(new FileSink(@"E:\log.json", new JsonFormatter(false, null, true), null), LogEventLevel.Debug)
.MinimumLevel.Verbose()
.WriteTo.Sink(new FileSink(@"E:\trace.json", new JsonFormatter(false, null, true), null), LogEventLevel.Verbose)
.Enrich.With<HttpRequestIdEnricher>()
.Enrich.With<UserNameEnricher>()
.Enrich.WithProperty("App", "CarbonFactoryERP")
.CreateLogger();
and below is how I call the logger:
Log.Logger.Debug("Web API Register Method started at {TimeStamp}",DateTime.UtcNow);
Log.Logger.Verbose("{TimeStamp} {Operation} {Operator} {Message} {Category}", rec.Timestamp, rec.Operation, rec.Operator, rec.Message, rec.Category);
This is the expected behavior of Serilog sinks. The minimum level parameter specifies only the minimum, as you'd expect - not an exact level match.
To get around this and write only a specific level to a sink, you can create a separate logging pipeline with the restriction applied and use that:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Logger(config => config
.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Debug)
.WriteTo.Sink(new FileSink(@"E:\trace.json", ...))
// Write to other sinks here
(May need a few typo corrections, working from memory.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With