Reputation: 739
I'd like to produce a log file that contains only log output from a particular EventId (or EventIds). Is such functionality supported?
Upvotes: 4
Views: 1777
Reputation: 3607
If you want to config Serilog using the appsettings file you can use something like this:
"Serilog": {
"Using": ["Serilog.Sinks.File", "Serilog.Settings.Configuration", "Serilog.Expressions"],
"MinimumLevel": {
"Default": "Debug"
},
"WriteTo": [{
"Name": "File",
"Args": {
"path": "log.txt",
"rollingInterval": "Day",
"retainedFileCountLimit": "120",
"rollOnFileSizeLimit": "true",
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
}
}
],
"Filter": [{
"Name": "ByIncludingOnly",
"Args": {
"expression": "EventId.Id = 101"
}
}
]
}
In the using field you have to put all Serilog package that you are importing, then config a sink for writing the logs and finally define a filter.
I almost spent a whole morning trying to achive that so I hope it helps you
Upvotes: 0
Reputation: 31822
If you plug in Serilog as the provider, you can continue logging through Microsoft.Extensions.Logging but apply Serilog's filtering to limit what is sent to the log file.
To do that you'd use the following Serilog configuration:
Log.Logger = new LoggerConfiguration()
.Filter.ByIncludingOnly("EventId.Id = 9")
.WriteTo.RollingFile("logs/log-{Date}.txt")
.CreateLogger();
(Where 9
is whatever event id you want to include.)
You can plug in Serilog with https://github.com/serilog/serilog-aspnetcore, and to compile this example you'll also need to install the Serilog.Sinks.RollingFile and Serilog.Filters.Expressions packages.
Upvotes: 2