简体   繁体   中英

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

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");

For me what was the solution is to remove the "restrictedToMinimumLevel": "Information" from appsettings.json .

It was exactly the same in appsettings.Development.json and it was working well in Development environment.

For me, I have configured all above. It logs db in my local environment, where as it does not work in dev environment. The issue is due to authentication. I am using azure ad authentication for my application that restricts serilog to write into server. So, In my program.cs file, I have added the below settings.

Program.cs

var sqlSinkOptions = new MSSqlServerSinkOptions
{
      TableName = "MyLogTableName",
      UseAzureManagedIdentity = true,  // This line
      AzureServiceTokenProviderResource = "https://database.windows.net/" // This line
};

After that, I can see logging is happening in dev environment too. May be this helps some one. Happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM