'Serilog Filtering not working in appsettings.json
I implemented Serilogger and put the configuration in the Program.cs and it worked great. I'm trying to move the configuration to appsettings.json and its writing to the logs but I can't get the filters to work now. I know its reading the config because I can change the minimum log level and I see the result in the logs.
I've been reading and rereading the Serilog pages in Github, especially the configuration page
Here is the code that worked in Program.cs
try
{
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
var levelSwitch = new LoggingLevelSwitch();
levelSwitch.MinimumLevel = LogEventLevel.Warning;
return new LoggerConfiguration()
.ReadFrom.Configuration(config, "Serilog");
.MinimumLevel.ControlledBy(levelSwitch)
.Enrich.FromLogContext()
.WriteTo.Debug()
.WriteTo.Logger(l => l
.Filter.ByExcluding("source = 'application'")
.WriteTo.File("Logs\\log-.txt", rollingInterval: RollingInterval.Day))
.WriteTo.Logger(l => l
.Filter.ByIncludingOnly("source='customer'")
.WriteTo.MSSqlServer(
connectionString:
"Server=(localdb)\\Infinity;Database=Infinity;Trusted_Connection=True;MultipleActiveResultSets=True;",
sqlSinkOptions));
}
Here is the new appsettings file
{
"Serilog": {
"Using": [ "Serilog.Enrichers.FromLogContext" ],
"MinimumLevel": "Warning",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=(localdb)\\Infinity;Database=Infinity;Trusted_Connection=True;MultipleActiveResultSets=True;",
"tableName": "Log",
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "source = 'customer'"
}
}
]
}
},
{
"Name": "File",
"Args": {
"path": "Logs\\log-.txt",
"rollingInterval": "Day",
"retainedFileCountLimit": 7,
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "source = 'customer'"
}
}
]
}
}
],
"Enrich": [ "FromLogContext" ]
},
Update: I figured out the custom property that I'm filtering on is not getting inserted into the logger object any longer. I think that has something to do with the "enricher"? But its not the source of the filter problem, because the "IncludeOnly" filter shouldn't write anything if the property isn't there, correct?
Here is a snippet from the method where I'm using PushProperty to insert the property and value. Since I haven't touched this code, I assume the problem is still in the app settings.
public void LogError(LogDestination destination, LogLevel level, string msg)
{
if (destination == LogDestination.Customer)
{
using (LogContext.PushProperty("source", "customer")) {
switch (level)
{
case LogLevel.Debug:
_logger.LogInformation(msg);
break;
I've also tried adding the .Enrich back into the LoggerConfiguration with no difference.
return new LoggerConfiguration()
.ReadFrom.Configuration(config, "Serilog")
.Enrich.FromLogContext();
Update I added a global filter at the top-level. I tried an Include Only and Exclude and both work as expected. I can't figure out why the sink filters aren't working.
{
"Serilog": {
"Using": [ "Serilog.Expressions", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": "Warning",
"Filter": [
{
"Name": "ByExluding",
"Args": {
"expression": "source = 'customer'"
}
}
],
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|