I have an app that configures Serilog via appsettings.json file, i develop the app on windows but it is deployed to linux. How can i write logs to a relative directory Logs/logfile.log?
Is just changing the appsettings.json at deployment folder a good option?
right now this is the config i am using:
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.MSSQLServer" ],
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"MinimumLevel": {
"Default": "Warning",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss} {SourceContext} [{Level}] {Message}{NewLine}{Exception}",
"theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Grayscale, Serilog.Sinks.Console"
}
},
{
"Name": "File",
"Args": {
"path": "Logs\\log-.log",
"rollingInterval": "Day",
"retainedFileCountLimit": 5
}
}
]
},
What you can do is the following:
Create two appsettings files one should be the default and the second one appsettings.Linux.json
Read the appsettings file depending on which OS the application is running.
var env = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ?
"Linux" : "Windows";
Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env}.json", true)
.AddEnvironmentVariables()
.Build();
Set different values for the logger in the appsettings.Linux.json. For Windows:
"Args": { "path": "Logs\log-.log", "rollingInterval": "Day", "retainedFileCountLimit": 5 }
For Linux:
"Args": {
"path": "/var/log/appname",
"rollingInterval": "Day",
"retainedFileCountLimit": 5
}