14

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?

4
  • Do you have any exception when you log information ? Commented Jan 14, 2018 at 17:39
  • Nope, nothing.. Commented Jan 14, 2018 at 17:43
  • Almost certain to be filesystem permissions - try creating the file first, giving the user that the web app is running under permission to write it, and then specifying the full path in the config; HTH! Commented Jan 15, 2018 at 5:04
  • You can also take a look at the SelfLog, it probably describe your problem. Commented Jan 15, 2018 at 6:42

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It's not a perfect solution but works. I would prefer to know how to write my logs in /var/logs folder.
2

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}"
              }
            }
          ]
        }      
      }
    ]
}

Comments

1

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"
    }
  }
]

}

1 Comment

Adding using statements to the app settings file resolved my serology issues on Linux

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.