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
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>
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:
Install-Package Destructurama.JsonNet
) Then I changed the Logger configuration to include the destructuring:
Log.Logger = new LoggerConfiguration() .Destructure.JsonNetTypes() .WriteTo.Console() .CreateLogger();
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.