Serilog not creating log file on production server

4.8k Views Asked by At

I've created a C# .net5.0 console application and during testing Serilog has been working without incident, logging to Console and File (same folder; path="log.txt"). However, when I run on the application on our server, neither Console nor File logging sinks are working! I assume now that the issue is not the sinks themselves but Serilog not actually working.

I've tried enabling the self log:

Serilog.Debugging.SelfLog.Enable(msg =>
    Console.WriteLine(msg)
);

but even running in the debugger in my dev environment, the Console.WriteLine(msg) line is never called!

My appsettings.json is as follows:

{
"Serilog": {
    "MinimumLevel": {
        "Default": "Debug",
        "Override": {
            "Microsoft": "Information",
            "System": "Information"
        }
    },
    "WriteTo": [
        {
            "Name": "Console",
            "Args": {
                "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
                "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {NewLine}{Exception}"
            }
        },
        {
            "Name": "File",
            "Args": {
                "path": "log.txt",
                "rollingInterval": "Infinite",
                "outputTemplate": "{Timestamp:HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
                "shared": false
            }
        }
    ],
    "Enrich": [ "FromLogContext" ]
},

"Database": {
    "Server": "(local)",
    "Database": "ActivbaseLive"
},

"Email": {
    "SmtpHost": "localhost",
    "SmtpPort": 25,
    "SmtpSecurityOption": "None",
    "SmtpUsername": null,
    "SmtpPassword": null,
    "SmtpSender": "\"Activbase Learning Platform\" <[email protected]>"
}
}

I've tried absolute paths (using double backslashes in appsettings.json). I've tried pre-creating the log file (e.g. log.txt and log200428.txt) and setting permissions to Everyone Full Control but neither of these changes fix the problem and they don't explain why the Console sink doesn't write either.

Here is how Serilog is being configured during start-up which is where I suspect the problem is (even through it works in dev environment):

return Host.CreateDefaultBuilder()
    .ConfigureLogging(logging =>
    {
        logging.ClearProviders();
    })
    .UseSerilog((hostContext, loggerConfiguration) =>
    {
        loggerConfiguration.ReadFrom.Configuration(hostContext.Configuration);
    })
    .ConfigureAppConfiguration((hostContext, builder) =>
    {
        builder.AddEnvironmentVariables();
    })
    .ConfigureServices(services =>
    {
        services.AddHostedService<Worker>();
        ...
    });
}

Any ideas why Serilog isn't working in production?

3

There are 3 best solutions below

1
On

For my api application running in IIS: I had to assign the following permissions to the log folder for the IIS_IUSRS. I didn't need an absolute path!

enter image description here

0
On

For me locally I did not have an issue. However when my application was running as a Windows Service the relative path no longer worked. I solved this issue by adding the following code.

Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
0
On

The Path you provide should be absolute. Some thing like this:

"path": "E:/wwwroot/QA/BackgroundWorkerService/Logs/logFile_.log"

Even I had the same issue, the above fix worked fine for me...