5

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"
      }
    }
4
  • please share DbGlobals.DevDatabase Commented Jan 10, 2019 at 9:39
  • Do you have any appsetting.json for serilog? Commented Jan 10, 2019 at 9:42
  • ok I have updated although not sure the relevance as I can confim it connects to the database. It also writes to the file just not my logging output Commented Jan 10, 2019 at 9:42
  • @Stefan There is the default logging sections in the appsettings.json but (unless i am wrong) from the above it doesn't take these into account? (this was my next step once I could confirm the logging worked correctly). I'll add that now Commented Jan 10, 2019 at 9:43

3 Answers 3

5

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");
Sign up to request clarification or add additional context in comments.

5 Comments

sound promising I'll give it a shot now.
That worked, I can confirm that both LogInformation and LogError worked when publishing. The odd thing was that it worked under a release build locally, and I thought that LogDebug would just be part of the logging heirarchy? Do you know why this is not the case? (out of interest)
That's interesting, I was always under the view that this was controlled by the logging configuration settings.
Hmm, you are right about that, 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: github.com/serilog/serilog/wiki/Configuration-Basics
yes, these are the docs I followed. this is why I used .MinimumLevel.Verbose() like you say perhaps WebDeploy causes a change??
1

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.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.