I have a dotnet app, and it currently stores only 31 log files in log folder, and that should be fine because I want to store only files for a month at max. The problem that I am having is that the server is behind some service that stops the app when it does not have any request and then it runs it again if a user sends a request and that has caused (I think) to change the naming convention of log files in my logs directory.
So according to the configs, the name of a log file should be log-{year}{month}{day}.txt
but when it stops and then the app again, then it becomes like this: log-{year}{month}{day}_001.txt
and next re-run log-{year}{month}{day}_002.txt
etc.
Below I am posting my UseSerilog
config from Program.cs
builder.Host.UseSerilog((context, configuration) =>
configuration.ReadFrom.Configuration(context.Configuration)
.MinimumLevel.Override("Microsoft", LogEventLevel.Error)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Error)
.MinimumLevel.Override("Serilog", LogEventLevel.Error)
.Enrich.FromLogContext()
.Enrich.WithClientIp()
.Enrich.WithClientAgent()
.WriteTo.Console()
);
This is the config from appsettings.json
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Using": [
"Serilog.Enrichers.ClientInfo"
],
"Enrich": [
"WithClientIp",
"WithClientAgent"
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "./logs/log-.txt",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {ClientIp}{NewLine}{Exception}",
"rollingInterval": "Day"
}
},
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {ClientIp} <s:{SourceContext}>{NewLine}{Exception}"
}
}
]
}
and this is my web.config
I added this file to the root directory to enable the server to write, I copied from the web and I am not sure what each line does exactly.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<defaultDocument enabled="false" />
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Demo.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
<handlerSettings>
<handlerSetting name="debugFile" value=".\logs\debug.log" />
<handlerSetting name="debugLevel" value="FILE,TRACE" />
</handlerSettings>
</aspNetCore>
</system.webServer>
</location>
</configuration>
Is there any config that I can change so that the logger will be appending logs to the same daily file, and not creating a new one when the app has stopped and run after again? I was reading the serilog docs, and one way was to increase retainedFileCountLimit
from 31 that is default to some larger number, but that is not exactly a solution to my problem.
For me, it worked once I enabled shared log files.
In Code:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(logfile, shared: true, rollingInterval: RollingInterval.Day)
.CreateLogger();
Or in appsettings.json:
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "./logs/log-.txt",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {ClientIp}{NewLine}{Exception}",
"rollingInterval": "Day",
"shared": true
}
}
]
}