ArieGato / serilog-sinks-rabbitmq

Serilog Sink for RabbitMq
Apache License 2.0
53 stars 51 forks source link

convert }} to } #208

Closed sdikerdong closed 2 months ago

sdikerdong commented 2 months ago

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?

ArieGato commented 2 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

sdikerdong commented 2 months ago

Thanks ArieGato, I will study it.

sdikerdong commented 2 months ago

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.

ArieGato commented 2 months ago

a few remarks

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);
sdikerdong commented 2 months ago

More simple input example like this: {\"Body\":{\"JsonValue\":{}}} after serilog output: {\"Body\":{\"JsonValue\":{}} it lost a '}'

sdikerdong commented 2 months ago

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

});

sdikerdong commented 2 months ago

Hi, ArieGato Your comments is working, thanks! var json = "{\"Body\":{\"JsonValue\":{\"key1\":\"key1value\"}}}"; Logger.LogInformation("{TheJson}", json);

ArieGato commented 2 months ago

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

sdikerdong commented 2 months ago

Yes, I used Serilog.Sinks.File, The result is same as Serilog.Sinks.RabbitMQ. Anyway, it is great of your help.

ArieGato commented 2 months ago

Happy coding!