Closed sdikerdong closed 6 months ago
you could try to create a custom formatter. This package used to use the RawFormatter
from Serilog 2.x.
https://github.com/serilog/serilog/blob/v2.12.0/src/Serilog/Formatting/Raw/RawFormatter.cs
Thanks ArieGato, I will study it.
Coding additional information like below: var json = "{\"Body\":{\"JsonValue\":{\"ia_i9bill\":{\"pk_org\":null,\"cstockorgvid\":null,\"ctrantypeid\":null,\"cstordocid\":null,\"cvendorvid\":null,\"vdef3\":null},\"ia_i9bill_b\":null},\"BodyType\":\"application/json\"}}"; Logger.LogInformation(json) Above code will write following string into text file, but it is less a '}' at end. {\"Body\":{\"JsonValue\":{\"ia_i9bill\":{\"pk_org\":null,\"cstockorgvid\":null,\"ctrantypeid\":null,\"cstordocid\":null,\"cvendorvid\":null,\"vdef3\":null},\"ia_i9bill_b\":null},\"BodyType\":\"application/json\"} This is what I said the issue I met.
a few remarks
outputTemplate
in the Serilog configuration?The code can look something like this:
var json = "{\"Body\":{\"JsonValue\":\"ia_i9bill\"}}";
Logger.LogInformation(json);
Some information about formatting can be found here: https://github.com/serilog/serilog/wiki/Formatting-Output
Not sure if this will work, but maybe you can try something like this:
var json = "{\"Body\":{\"JsonValue\":\"ia_i9bill\"}}";
Logger.LogInformation("{TheJson}", json);
More simple input example like this: {\"Body\":{\"JsonValue\":{}}} after serilog output: {\"Body\":{\"JsonValue\":{}} it lost a '}'
Thanks for your replies, ArieGato. Here is simple code: var json = "{\"Body\":{\"JsonValue\":{\"key1\":\"key1value\"}}}"; Logger.LogInformation(json); it output to console like this: {"Body":{"JsonValue":{"key1":"key1value"}} My outputTemplate is : string outputTemplate = "{NewLine} {Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {SourceContext} {ThreadId} [{Level:u3}] {Message:lj} {NewLine} {Exception}";
logger
.Destructure.JsonNetTypes()
.Enrich.FromLogContext()
.MinimumLevel.ControlledBy(FrameworkSwitch)
.MinimumLevel.Override("Default", FrameworkSwitch)
.MinimumLevel.Override("System", FrameworkSwitch)
.MinimumLevel.Override("Microsoft", FrameworkSwitch)
.WriteTo.Debug(outputTemplate: outputTemplate)
.WriteTo.Console(outputTemplate: outputTemplate)
.WriteToFileAsync(LogEventLevel.Debug, outputTemplate)
.WriteToFileAsync(LogEventLevel.Information, outputTemplate)
.WriteToFileAsync(LogEventLevel.Warning, outputTemplate)
.WriteToFileAsync(LogEventLevel.Error, outputTemplate)
.WriteToFileAsync(LogEventLevel.Fatal, outputTemplate);
});
Hi, ArieGato Your comments is working, thanks! var json = "{\"Body\":{\"JsonValue\":{\"key1\":\"key1value\"}}}"; Logger.LogInformation("{TheJson}", json);
You're welcome.
If you really think this is a bug, then it's probably with the JsonCompactFormatter
. Have you tried to reproduce the issue with another sink? e.g. Serilog.Sinks.File
Yes, I used Serilog.Sinks.File, The result is same as Serilog.Sinks.RabbitMQ. Anyway, it is great of your help.
Happy coding!
When I used this package to write log into RabbitMQ, I find it convert JSON which contains }} to }. This cause the @m of message in RabbitMQ is a illegal JSON string but @mt doesnot. How Could I resolve this problem?