4

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\" <noreply@activbase.net>"
}
}

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?

Chris Walsh
  • 3,423
  • 2
  • 42
  • 62
  • On top of what you tried, I'd do these 4 things next: 1. Specifically set permissions for the User the application is running with on the folder level as well 2. Start the console application with administrator mode 3. Check the application log of the windows event viewer => Run, CMD, eventvwr.msc. System sided errors should show up there 4. If some intrusive anti-virus software is running, check if access to the serilog.dll is blocked. – Bruellhusten Aug 12 '21 at 13:29
  • Very similar frustration going on right now and wondering how on earth to figure it out – Yehuda Makarov Oct 19 '21 at 13:57
  • Was there any resolution to this issue? I have tried all the suggestions and still don't have any luck getting the log file to write out. – Rushman Feb 08 '22 at 21:12
  • @Rushman I've moved on since I posted this query but I think you should check that you have an absolute path to the log file and that the IIS user account has read/write permissions as per the suggestions here. – Chris Walsh Feb 09 '22 at 10:20
  • Did you find a solution ? – Taieb Apr 15 '22 at 16:21
  • @YehudaMakarov did you find a solution ? – Taieb Apr 15 '22 at 16:22
  • 1
    @Taieb obviously important to ensure full control permissions (maybe less not sure) to the log folder and the path should be fully qualified. My problem was that I was only logging to a file for my prod environment and I verified the env was always set to dev. Wasn’t sure why. When I was able to verify it was prod in the deployment (I made an api endpoint to send a response of the env variable value) logs were writing to the file. – Yehuda Makarov Apr 18 '22 at 14:17

2 Answers2

3

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...

Meena
  • 685
  • 8
  • 31
2

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

kloarubeek
  • 2,706
  • 20
  • 24