Search code examples
c#loggingnlogasp.net-core-3.1microsoft-extensions-logging

How do I log a custom field in NLog to database using Microsoft.Extensions.Logging?


I am using Microsoft.Extensions.Logging together with NLog. My app is .NET Core 3.1.

I would like to extend logging with custom fields.

Is it possible or will I need to use NLog directly?

  <parameter name="@custom_guid" layout="${custom_guid}"/>

         var config = new Dictionary<string, object>();
         config.Add("custom_guid", "test"); 
         _logger.LogInformation("Test message", config);

Solution

  • You could do this:

    var config = new Dictionary<string, object>();
    config.Add("custom_guid", "test");
    
    using (_logger.BeginScope(config))
    {
       _logger.LogInformation("Test message");
    }
    

    And use ${mdlc:custom_guid}