serilog / serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging
Apache License 2.0
307 stars 97 forks source link

Question: Any way to add deconstruction when no at-sign is in message template? #207

Closed GeraldLx closed 9 months ago

GeraldLx commented 1 year ago

The Microsoft abstractions are currently the standard for combining projects.

So in all our current projects we set up Microsoft Logging with Serilog. From a perspective of our own code there is no real benefit as we have everything in our hands. The advantage is that all 3rd party libraries also automatically log into our Seq.

The problem I have is that some libraries have hand written ToString methods for their classes generating some ugly output (of course personal taste g).

I would like to replace this with my custom deconstruction, but as the library is external, I cannot replace the message template by adding an @ sign.

I think the @ sign system is a nice and perfect solution from a Serilog perspective, but abstractions relying on specific implementations are not really abstractions ;)

I am not sure if this is the right place. Either we would need a way to customize the default behavior (scalar/deconstruction) for specific types in the Serilog base library or a way to transform this in the adapter.

nblumhardt commented 1 year ago

Hi Gerald!

@ is part of the abstraction - it's recognized by the full-featured structured logging back-ends (Serilog and NLog), has been part of the https://messagetemplates.org spec since the beginning, and is accepted by Microsoft.Exensions.Logging pretty much everywhere. Raising issues with the external libraries is the best path forward :-)

Hope this helps, Nick

GeraldLx commented 1 year ago

I know the template system now for a long time and love how it works. As long as I am in full control of the code, there is no problem.

I am just in the situation where we use a 3rd party library. (EasyNetQ, if you are interested)

The library has it´s own logging logic and provides implementations for Microsoft and Serilog.

In the end there is a line similar to this:

logger.Error(exception, "The call failed with {information}", information);

while Information is a custom class from the library with an overwritten ToString.

In Seq I can now see this information: '[ { "Key1": "Value1", "Key2": "Value2" } ]'

So basically the serialized object as string. Of course I would prefer the same data as object in my Seq.

Currently my only chance is to fork the libraries source code and add the @.

Therefore I asked if it is possible to overwrite this behavior from the outside

bartelink commented 1 year ago

Therefore I asked if it is possible to overwrite this behavior from the outside

Have you considered asking whether the lib would consider exposing a mechanism to expose its raw data (e.g. a dictionary of values like ForContext can use) and/or use @ ? Sounds like the sort of thing that lots of people would be interested in?

As a hack until it's done, you might be able to decorate the ILogger with a helper that inserts an @ into the message template that the lib is passing,`

sungam3r commented 1 year ago

https://github.com/EasyNetQ/EasyNetQ/discussions/1484

nblumhardt commented 9 months ago

Closing as stale; looks like @sungam3r has helped find a solution that works with the original source library.