I want to store logs in different file based on type.
Let’s say for for
- API Request log – request-.txt;
- EF Core Query – log-.txt
I have installed Serilog packages, and it works fine but file contains all the logs.
<PackageVersion Include="Serilog" Version="4.0.1" />
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.2" />
<PackageVersion Include="Serilog.Expressions" Version="5.0.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
In my appSettings.json file, I tried using Expression.
{
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.File"
],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft.AspNetCore.Mvc": "Warning",
"Microsoft.AspNetCore.Routing": "Warning",
"Microsoft.AspNetCore.Hosting": "Warning"
}
},
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "Logs/log-.txt",
"rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
"fileSizeLimitBytes": 10485760,
"rollOnFileSizeLimit": true,
"shared": true,
"flushToDiskInterval": 1
},
"Filter": [
{
"Name": "ByIncluding",
"Args": {
"expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore.Database')"
}
}
]
}
],
"Enrich": [
"FromLogContext"
]
},
"AllowedHosts": "*"
}
But it didn’t worked. All logs are storing into single log file log-{Date}.txt.
If there is no way to configure this in appSettings.json file, then how to configure in Program.cs file.
6