Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog not creating log file when running on Linux

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?

like image 807
rjovic Avatar asked Jan 14 '18 16:01

rjovic


People also ask

How do you use Serilog logging?

Create a Console Application project in Visual Studio. Install Serilog and its dependencies. Create and configure the Serilog global logger. Integrate the logger into the C# Console Application.

Can Serilog log database?

In order to proceed with database logging with Serilog in ASP.NET Core, the first step required is to install Serilog. Sinks. MSSqlServer package from NuGet. In the “Write To” subsection, we can set up the database connection string, along with the name of the table we want to create for logs.

What is sink in Serilog?

Serilog provides sinks for writing log events to storage in various formats. Many of the sinks listed below are developed and supported by the wider Serilog community; please direct questions and issues to the relevant repository. More sinks can be found by searching within the serilog tag on NuGet.


1 Answers

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.

like image 62
DavidC Avatar answered Oct 02 '22 10:10

DavidC