I have this piece of code on c# which does the job pretty well. Every log that comes from CSPConsumersLayer get logged on a specific file (ConsumerLog.txt), meanwhile, every other log gets logged on another file (Log.txt). This is the C# configuration that I want to achieve in appsettings.json
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(Matching.FromSource(nameof(CSPConsumersLayer))).WriteTo.File(@"..\Logs\ConsumerLog.txt", LogEventLevel.Information, "{Timestamp:G} [{Level}] {ThreadId} {MachineName} {Message} {Exception}{NewLine}", rollOnFileSizeLimit: true, fileSizeLimitBytes: 104857600))
.WriteTo.Logger(lg => lg.Filter.ByExcluding(Matching.FromSource(nameof(CSPConsumersLayer))).WriteTo.File(@"..\Logs\Log.txt", LogEventLevel.Information, "{Timestamp:G} [{Level}] {User} {IP} {ThreadId} {MachineName} {SourceContext} {RequestId} {Message} {Exception}{NewLine}", rollOnFileSizeLimit: true, fileSizeLimitBytes: 104857600))
.CreateLogger();
And when I try to convert it on Json-like it won't work
--This is the JSON I have come up with, which doesn't work properly.
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "StartsWith(SourceContext, 'CSPConsumersLayer')"
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "../Logs/ConsumerLog.log",
"outputTemplate": "{Timestamp:G} [{Level}] {User} {IP} {ThreadId} {MachineName} {SourceContext} {RequestId} {Message} {Exception}{NewLine}",
"rollOnFileSizeLimit": "true",
"fileSizeLimitBytes": 104857600
}
}
]
}
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "StartsWith(SourceContext, 'CSPConsumersLayer')"
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "../Logs/Log.log",
"outputTemplate": "{Timestamp:G} [{Level}] {User} {IP} {ThreadId} {MachineName} {SourceContext} {RequestId} {Message} {Exception}{NewLine}",
"rollOnFileSizeLimit": "true",
"fileSizeLimitBytes": 104857600
}
}
]
}
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName"
]
}
The answer is this simple. If you want to do filtering on the appsettings.json, you should include a packet called 'serilog-extensions-logging'.