2017-08-17 60 views
2

我有一個使用Serilog和JSNLog進行客戶端日誌記錄的.NET Core項目。如果我將JSON對象從客戶端傳遞到服務器並使用Serilog進行登錄,則記錄的JSON對象爲空。Serilog/JSNLog .NET Core日誌空JSON

很奇怪的是,如果我有附加調試器時,JSON記錄的罰款。

例如:

在調試,我得到:

[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 

當鍵Crtl + F5,我得到:

[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 

我不知道問題是否與Serilog或JSNLog ,但任何幫助將不勝感激。

我已經做了一個非常簡單的示例應用程序來複制這個。使用默認的.NET核心的webapp

依賴關係,如下所示: Dependencies

在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?}"); 
      }); 
} 

在我的前端:

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

回答

0

我有一個類似的問題。我看了一下JSNLog,看起來問題在於記錄從日誌消息中刪除對象時創建的JSON .NET對象。

我做了以下解決方法:

  1. 我安裝NuGet包Destructurama.JsonNetInstall-Package Destructurama.JsonNet
  2. 然後,我改變記錄儀配置以包括解構:

    Log.Logger = new LoggerConfiguration() 
         .Destructure.JsonNetTypes() 
         .WriteTo.Console() 
         .CreateLogger(); 
    
  3. 然後我創建了這樣一個CustomLoggingAdapter類:

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

    和改變日誌以具有以下格式{@logMessage}

注:LogMessageHelpers.DeserializeIfPossible可以在JSONLog GitHub回購找到

  • 然後我改變了JSNLog的配置,在我的CustomLoggingAdapter這樣做:

    app.UseJSNLog(new CustomLoggingAdapter(loggerFactory), jsnlogConfiguration); 
    
  • 並且出現日誌消息。

    讓我知道是否有幫助