rjovic
rjovic

Reputation: 1247

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?

Upvotes: 14

Views: 8537

Answers (3)

Eric Williams
Eric Williams

Reputation: 45

I'm experiencing the same issues. I can hard code my serilog settings in the program.cs file and everything works as expected. Reading the appsettings.json serilog configuration throws no errors and fails to configure the serilog sinks.

working:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .MinimumLevel.ControlledBy(LoggerLevel.Switch)
    .Enrich.WithThreadId()
    .Enrich.FromLogContext()
    .Enrich.WithMachineName()
    .Enrich.WithProcessId()
    .WriteTo.Console()
    .WriteTo.Seq("http://localhost:9091", LogEventLevel.Verbose)
    .CreateLogger();

Not working:

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .MinimumLevel.ControlledBy(LoggerLevel.Switch)
    .CreateLogger();

"Serilog": {
"Using": [],
"MinimumLevel": {
  "Default": "Verbose",
  "Override": {
    "Microsoft": "Verbose",
    "System": "Verbose"
  }
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
  {
    "Name": "Console"
  },
  {
    "Name": "File",
    "Args": {
      "restrictedToMinimumLevel": "Information",
      "path": "logs/application_event_log.json",
      "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
    }
  },
  {
    "Name": "Seq",
    "Args": {
      "serverUrl": "http://localhost:9091"
    }
  }
]

}

Both solutions work fine on windows.

Updated with Resolution: adding using statements to the serilog section in app settings.Json

 "Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Seq" ],
"MinimumLevel": {
  "Default": "Verbose",
  "Override": {
    "Microsoft": "Verbose",
    "System": "Verbose"
  }
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
  { "Name": "Console" },
  {
    "Name": "File",
    "Args": {
      "restrictedToMinimumLevel": "Information",
      "path": "logs/application_event_log.json",
      "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
    }
  },
  {
    "Name": "Seq",
    "Args": {
      "serverUrl": "http://10.0.0.111:9091"
    }
  }
]

}

Upvotes: 1

mack
mack

Reputation: 2965

The accepted answer on this page didn't solve it for me. I'm running .net 6 with Serilog 2.12.0. The Serilog configuration is loaded from appsettings.json.

The problem was that Serilog couldn't create or write to the application's log folder under /var/log.

What solved the problem was:

mkdir /var/log/myapp/
chown www-data:www-data /var/log/myapp/
chmod 755 /var/log/myapp

This is the core of my configuration:

"Serilog": {
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.File",
      "Serilog.Enrichers.ClientInfo"
    ],
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    },
    "Enrich": [
      "FromLogContext",
    ],
    "WriteTo": [
      {
        "Name": "Async",
        "Args": {
          "configure": [
            {
              "Name": "Console"
            },
            {
              "Name": "File",
              "Args": {
                "path": "/var/log/myapp/log-.log",
                "rollingInterval": "Day",
                "retainedFileCountLimit": 7,
                "outputTemplate": "{Timestamp:HH:mm:ss.fff zzz} [{Level}] | {MemoryUsage} bytes | <{ClientIp}> <{ClientAgent}> {Message:lj}{NewLine}{Exception}"
              }
            }
          ]
        }      
      }
    ]
}

Upvotes: 2

DavidC
DavidC

Reputation: 704

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.

Upvotes: 10

Related Questions