Search code examples
c#asp.net-core-webapiasp.net-core-2.1serilog

Serilog not logging when published ASP.NET core


I have Serilog configured to write to both an MSSQL table and to a File (to try and determine whats happening.) on a WebApi with ASP.NET core (running Core 2.1).

I add the Serilog logging in my startup.cs as follows;

public async void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    var log = new LoggerConfiguration()
        .MinimumLevel.Verbose()
        .WriteTo.MSSqlServer(
            DbGlobals.DevDatabase, "Logs", schemaName:DbGlobals.SchemaName)
        .WriteTo.File("Logs\\Serilog\\log.txt", rollingInterval: RollingInterval.Day)
        .CreateLogger();

    loggerFactory.AddSerilog(log);

where DbGlobals.DevDatbase and Dbglobals.SchemaName are string read from the configuration.

UPDATE For reference these values are;

DbGlobals.Schema = "MySchema";
DbGlobals.DevDatabase = "Data Source=xx.xx.xx.xx;Initial Catalog=DevDatabase;Integrated Security=False;Persist Security Info=False;User ID=LOGIN;Password=PASSWORD;MultipleActiveResultSets=true;";

When running locally with IIS Express and under both debug and release configurations, both logging functions are working correctly. So for example in my controller;

public class ReviewPublicController : Controller
{
    private readonly IReviewService _service;
    private readonly IMapper _mapper;
    private readonly IMailService _mailService;
    private readonly ILogger _logger;

    public ReviewController(
        IReviewService service, 
        ILogger<ReviewController> logger)
    {
        _service = service;
        _logger = logger;
    }

    [HttpPost]
    [ProducesResponseType(typeof(Review), 201)]
    [Route("")]
    public async Task<IActionResult> Post([FromBody]ReviewPostModel model)
    {
        _logger.LogDebug("Posting Review");

        ...

        return Ok();
    }

So locally "Posting Review" is written to both my File and DbSchema.

However, when I publish to my server, the above "Posting Review" entry is not written to either.

Both appear to be working as information entries are being added from EF and routing etc but not when I specifically try to write an entry?

Can anyone direct me here as to what the issue could be please?

The current nuget versions are;

Serilog 2.7.1
Serilog.AspNetCore 2.1.1
Serilog.Sinks.MSSqlServer 5.1.2
Serilog.Sinks.File 4.0.0

Also, I have the following in my appsettings.json;

  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Release": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }

Solution

  • update

    As in the docs:

    Default Level - if no MinimumLevel is specified, then Information level events and higher will be processed.

    So basically, it seems that your configuration is either invalid or overwritten. see: https://github.com/serilog/serilog/wiki/Configuration-Basics


    LogDebug is disabled by default in a Release build, which is probably what your publish is.

    Please try with

    _logger.LogInformation("Posting Review");