Issue
I'm having problems to enable logging when running ASP.NET Core application on Linux. I'm using Serilog.Sinks.File (but also tried with RollingFile) with following configuration defined in appsettings:
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File",
"Args": { "path": "core_log.log", "rollingInterval": "Day" }
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "CORE service"
} }
I also tried with with RollingFile sink using pathFormat but without success. What ever I try, application is not creating the log file.
I also tried multiple variants of path like: /var/test/app/core_log.log or just simply core_log.log but I'm not seeing file anywhere. I tried searching it using:
sudo find / -name '*.log' -print | grep core_log
It is important to mention that when running the same app on the Windows, everything works well and I can see log file created.
Did somebody have similar problem with Serilog on Linux? Can it be something related with privileges?
Solution
Just fixed this same problem with this configuration:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
Log.Logger = new LoggerConfiguration()
.WriteTo
.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs/.log"), rollingInterval: RollingInterval.Day)
.CreateLogger();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddSerilog(dispose: true);
})
.UseStartup<Startup>();
Pay special attention to AppDomain.CurrentDomain.BaseDirectory
. This will point to your Linux deploy folder. I guess it's possible to read the folder from a config file too.
I also had to create the folder manually, as pointed out by Nicholas (thank you!, not only for this), but not the file.
Answered By - DavidC Answer Checked By - Willingham (WPSolving Volunteer)