4

I have a .NET Core project using Serilog and JSNLog for client side logging. If I pass a JSON object from the client to the server and log it using Serilog, the logged JSON object is empty.

The very weird thing is that, if I have the debugger attached, the JSON is logged fine.

For example:

While debugging I get:

[11:00:01 FTL] this works
[11:00:02 INF] Request finished in 342.1967ms 200 text/plain
[11:00:02 FTL] "testMessage": "this is an error"
[11:00:02 INF] Request finished in 374.7837ms 200 text/plain

When Crtl+F5 I get:

[10:59:14 FTL] this works
[10:59:14 INF] Request finished in 253.3403ms 200 text/plain
[10:59:15 FTL] [[[]]]
[10:59:15 INF] Request finished in 267.2553ms 200 text/plain

I'm not sure if the problem is with Serilog or JSNLog, but any help would be appreciated.

I've made a very simple sample app to replicate this. Using the default .NET Core Webapp

Dependencies are as shown: Dependencies

in Startup.cs:

public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();

            Log.Logger = new LoggerConfiguration()
                .WriteTo.Console().CreateLogger();
        }

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddSerilog();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseJSNLog(new LoggingAdapter(loggerFactory));

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
}

And in my front end:

<script src="~/lib/jsnlog.js/jsnlog.min.js"></script>
    <script>
            JL().fatal({ testMessage: "this is an error" });
            JL().fatal("this works");
        </script>
Kyle
  • 236
  • 1
  • 6
  • 14

1 Answers1

4

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

tabz100
  • 1,463
  • 11
  • 14