I have this serilog conifg
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer", "Serilog.Expressions" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "C:/Temp/logs/my.log",
"rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
}
},
{
"Name": "MSSqlServer",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "LogCategory is not null"
}
}
]
},
"connectionString": "Server=myServer;Database=myDB;User ID=userId;Password=pwd;Encrypt=False;TrustServerCertificate=True",
"tableName": "telemetry_logs",
"autoCreateSqlTable": true,
"columnOptionsSection": {
"removeStandardColumns": [ "MessageTemplate", "Level", "Exception", "Properties" ],
"customColumns": [
{
"ColumnName": "route",
"DataType": "varchar",
"DataLength": 100
},
{
"ColumnName": "caller",
"DataType": "varchar",
"DataLength": 100
},
{
"ColumnName": "username",
"DataType": "varchar",
"DataLength": 100
},
{
"ColumnName": "role",
"DataType": "varchar",
"DataLength": 100
},
{
"ColumnName": "processing_time",
"DataType": "int"
},
{
"ColumnName": "api_response_message",
"DataType": "varchar",
"DataLength": 250
},
{
"ColumnName": "useragent",
"DataType": "varchar",
"DataLength": 250
},
{
"ColumnName": "api_response_code",
"DataType": "char",
"DataLength": 3
},
{
"ColumnName": "http_response_code",
"DataType": "int"
}
]
}
},
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "LogCategory is not null"
}
}
]
}
],
"Enrich": [ "FromLogContext" ]
},
Here is my config and logger setup
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true);
var configuration = configurationBuilder.Build();
// Configure Serilog
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.MinimumLevel.Information()
.CreateLogger();
Here is the code that I want it to log
var dbLogger = Log.ForContext("LogCategory", "Telemetry")
.ForContext("route", path)
.ForContext("caller", caller)
.ForContext("username", username)
.ForContext("role", role)
.ForContext("processing_time", processingTime)
.ForContext("api_response_message", apiResponseMessage)
.ForContext("api_response_code", apiResponseCode)
.ForContext("useragent", userAgent);
dbLogger.Information("API telemetry");
Serilog creates the table, removes standard columns as per config, add new columns, but it logs everything including the code above. I want it to exclude everything but the above in MSSqlServer sink. Please note I have filter in 2 places, I am not sure which one is the correct place to put it.
Filter on sink level only work on sub-logger, this fixed it
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "Contains(@m,'telemetry')"
}
}
],
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {