I'm tryign to implement a log filter using serilog-expressions in my appsettings, filtering all 'health' related logs. This is my config so far:
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Data Source=XXX; Database=YYY; Integrated Security=True; TrustServerCertificate=true;",
"sinkOptionsSection": {
"tableName": "Logging",
"autoCreateSqlDatabase": false,
"autoCreateSqlTable": true
},
"columnOptionsSection": {
"additionalColumns": [
{
"columnName": "RequestMethod",
"dataLength": "8"
},
{
"columnName": "StatusCode",
"dataType": "Int"
}
]
},
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "RequestPath like '%/health%'"
}
}
]
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Destructure": [
{
"Name": "ToMaximumDepth",
"Args": { "maximumDestructuringDepth": 4 }
},
{
"Name": "ToMaximumStringLength",
"Args": { "maximumStringLength": 100 }
},
{
"Name": "ToMaximumCollectionCount",
"Args": { "maximumCollectionCount": 10 }
}
],
"Properties": {
"Application": "Api"
}
},
However, the messages related to HealthChecks continue to be logging in my database.
How can I write a config that filter by IncludingOnly, Excluding or checks if a colum from Loggin table has a specifc value?
Thanks
I read the documentation about Serilog Expressions, but didn't quite understand how the filter works: Which properties can I use to filter? Where can I find them? Are they the variables in {KEYS} from MessageTemplate column? What about my custom columns: can I filter them?
Ex.: The message in MessageTemplate 'HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms' provides me {RequestPath} variable, but when I try to use it in a filter ( "expression": "RequestPath like '%/health%'"), nothing happens.
The Filter section was in the wrong place, inside the WriteTo tag. Placing it right inside "Serilog" fixed the problem.
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Data Source=XXX; Database=YYY; Integrated Security=True; TrustServerCertificate=true;",
"sinkOptionsSection": {
"tableName": "Logging",
"autoCreateSqlDatabase": false,
"autoCreateSqlTable": true
},
"columnOptionsSection": {
"additionalColumns": [
{
"columnName": "RequestMethod",
"dataType": "nvarchar",
"dataLength": "8"
},
{
"columnName": "StatusCode",
"dataType": "Int"
},
{
"columnName": "MachineName",
"dataType": "nvarchar",
"dataLength": "32"
},
{
"columnName": "EnvironmentName",
"dataType": "nvarchar",
"dataLenght": "32"
},
{
"columnName": "EnvironmentUserName",
"dataType": "nvarchar",
"dataLenght": "32"
},
{
"columnName": "ClientIp",
"dataType": "nvarchar",
"dataLenght": "64"
},
{
"columnName": "ThreadId",
"dataType": "int"
},
{
"columnName": "ThreadName",
"dataType": "nvarchar",
"dataLenght": "32"
},
{
"columnName": "ProcessId",
"dataType": "int"
},
{
"columnName": "ProcessName",
"dataType": "nvarchar",
"dataLenght": "64"
}
]
}
}
}
],
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "Contains(@m, 'health')"
}
},
{
"Name": "ByExcluding",
"Args": {
"expression": "Contains(@m, 'Health_Homol')"
}
},
{
"Name": "ByExcluding",
"Args": {
"expression": "Contains(@m, 'swagger')"
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithEnvironmentName",
"WithEnvironmentUserName",
"WithThreadId",
"WithThreadName",
"WithClientIp",
"WithProcessId",
"WithProcessName"
],
"Properties": {
"Application": "Api"
}