C
C#9mo ago
Ikinidae

❔ Serilog: how to write a file for single level

Hi guys! I've been asked to write log levels Information, Warning and Error in development, but each of them in one separated file, so that I have information.txt containing only Information logs, warning.txt containing only Warning logs, Error.txt containing only Error logs. This is my implementation at the moment (it works): In Program.cs:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseSerilog((webHostBuilderContext, loggerConfiguration) =>
{
loggerConfiguration.ReadFrom.Configuration(webHostBuilderContext.Configuration);
});
});
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseSerilog((webHostBuilderContext, loggerConfiguration) =>
{
loggerConfiguration.ReadFrom.Configuration(webHostBuilderContext.Configuration);
});
});
in appsettings:
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "wwwroot/Logs/log.txt",
"rollingInterval": "Day",
"reatinedFileCountLimit": 14,
"restrictedToMinimumLevel": "Information"
}
}
]
},
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "wwwroot/Logs/log.txt",
"rollingInterval": "Day",
"reatinedFileCountLimit": 14,
"restrictedToMinimumLevel": "Information"
}
}
]
},
I have installed Serilog.Expressions package and read the docs, I know I have to use something like this
"Filters": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@l = 'Information'"
}
}
]
"Filters": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@l = 'Information'"
}
}
]
but I can't understand where it should be placed. I've tried placing it in Args or between Args and Name but it doesn't work, can somebody help me? 🥺
3 Replies
Pobiega
Pobiega9mo ago
you can definately do it with filters, which is a serilog plugin ah, there was another section of the post 😄 Yeah, expression filters, thats what you need it should be something like
{
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "wwwroot/Logs/log.txt",
"rollingInterval": "Day",
"reatinedFileCountLimit": 14,
"restrictedToMinimumLevel": "Information",
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@l = 'Information'"
}
}
]
}
}
}
]
}
}
{
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "wwwroot/Logs/log.txt",
"rollingInterval": "Day",
"reatinedFileCountLimit": 14,
"restrictedToMinimumLevel": "Information",
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@l = 'Information'"
}
}
]
}
}
}
]
}
}
Ikinidae
Ikinidae9mo ago
I've tried in this exact way and it didn't work, I'm going crazy beacause it seems to be SO SIMPLE
Accord
Accord9mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.