Serilog Not Logging to File

91 views Asked by At

I am attempting to log both errors and information on API requests in my .NET Core 6 project. After setting it up in my Program.cs file it does output a log entry to the desired location. Below is my setup in my Program.cs file.

        var logger = new LoggerConfiguration()
            .ReadFrom.Configuration(builder.Configuration)
            .Enrich.FromLogContext()
            .CreateLogger();
        builder.Logging.AddSerilog(logger);

        try
        {
            logger.Information("Starting logger");
        }
        catch (Exception ex)
        {
            logger.Error(ex, "Unhandled exception");
        }
        finally
        {
            _ = Log.CloseAndFlushAsync();
        }

        builder.Host.UseSerilog();

Here is what I have in my appsettings.json file.

"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": "Information",
"WriteTo": [
  {
    "Name": "Console"
  },
  {
    "Name": "File",
    "Args": {
      "path": "E:\\Logs\\MyApplicationLogs\\log.json",
      "rollingInterval": "Day",
      "restrictedToMinimumLevel": "Verbose"
    }
  }
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]

}

Here is me trying to log both information and errors in one of my controllers. Neither _logger.LogInformation or the _logger.LogError log the information or the error when the error is thrown.

private readonly ILogger<MyController> _logger;

public MyController(ILogger<MyController> logger)
{
    _logger = logger;
}

public void Submit()
{
    _logger.LogInformation("You have submitted an update!");
    try {
        // Do something that causes error
    } catch (Exception ex) {
        _logger.LogError(LogHelper.BuildExceptionMessage(ex, "An error occured loading your form."));
    }
}

I have the following Serilog packages installed:

  • Serilog Version="3.1.1"
  • Serilog.AspNetCore Version="7.0.0"
  • Serilog.Extensions.Hosting Version="7.0.0"

Any feedback as to why my logger is not logging the information or errors in my controller would be much appreciated.

1

There are 1 answers

2
Md Farid Uddin Kiron On

I am attempting to log both errors and information on API requests in my .NET Core 6 project. After setting it up in my Program.cs file it does output a log entry to the desired location

Based on your shared code snippet and description, I have tried to reproduced your issue.

According to my investigation, I founded few reasons why your logger might not be logging information or errors in your controller.

You are currently defining the Submit method with another ILogger<MyController> parameter. This creates a new, local instance of the logger instead of using the injected one. In order to use the injected logger, remove the parameter from the Submit method.

Modify your snippet as following:

public void Submit()
{
  _logger.LogInformation("You have submitted an update!");
  try {
    // Do something that causes error
  } catch (Exception ex) {
    _logger.LogError(LogHelper.BuildExceptionMessage(ex, "An error occured loading your form."));
  }
}

In addition to above modification please also make sure, you have following configuration:

Double check if you have added app.UseSerilogRequestLogging(); middleware reference after other configurations in the program.cs class file.

You can also adjust the minimum level in appsettings.json for the "File" sink to either "Information" or "Error" depending on your desired logging granularity.

Output:

enter image description here

Note: If you would like to know more details about logging in asp.net core you could check the official document here