Serilog / JSNLog .NET Core logging empty JSON Serilog / JSNLog .NET Core logging empty JSON json json

Serilog / JSNLog .NET Core logging empty JSON


I had a similar issue. I took a look at JSNLog and what seemed to be the issue was the logging of the JSON .NET object that was being created when desearializing an object from the log message.

I did the following workaround:

  1. I installed the Nuget package Destructurama.JsonNet (Install-Package Destructurama.JsonNet)
  2. Then I changed the Logger configuration to include the destructuring:

    Log.Logger = new LoggerConfiguration()        .Destructure.JsonNetTypes()        .WriteTo.Console()        .CreateLogger();
  3. I then created a CustomLoggingAdapter class like this:

    public class CustomLoggingAdapter: ILoggingAdapter{    private ILoggerFactory _loggerFactory;    public CustomLoggingAdapter(ILoggerFactory loggerFactory)    {        _loggerFactory = loggerFactory;    }    public void Log(FinalLogData finalLogData)    {        ILogger logger = _loggerFactory.CreateLogger(finalLogData.FinalLogger);        Object message = LogMessageHelpers.DeserializeIfPossible(finalLogData.FinalMessage);        switch (finalLogData.FinalLevel)        {            case Level.TRACE: logger.LogTrace("{@logMessage}", message); break;            case Level.DEBUG: logger.LogDebug("{@logMessage}", message); break;            case Level.INFO: logger.LogInformation("{@logMessage}", message); break;            case Level.WARN: logger.LogWarning("{@logMessage}", message); break;            case Level.ERROR: logger.LogError("{@logMessage}", message); break;            case Level.FATAL: logger.LogCritical("{@logMessage}", message); break;        }    }}

    and changed the log to have the following format {@logMessage}

Note: LogMessageHelpers.DeserializeIfPossible can be found in the JSONLog GitHub repo

  1. Then I changed the JSNLog configuration to take in my CustomLoggingAdapter like this:

     app.UseJSNLog(new CustomLoggingAdapter(loggerFactory), jsnlogConfiguration);

and the log messages appeared.

Let me know if that helps