serilog / serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging
Apache License 2.0
313 stars 100 forks source link

State property should be set to destructured object #80

Closed moogle001 closed 7 years ago

moogle001 commented 7 years ago

Hello, I am using Serilog, the ILogger extension, and Exceptionless as a sink. When using the ILogger.Log method, I would expect the provided state object to be serialized and added as a property to the log event. Instead, a state property is being added, but given the value returned by the message formatter. This seems redundant since that is also the value given to the log event message.

After looking at the source code, it seems the only way to get the desired behavior is to create a Dictionary<string, object>, add my object to it with a key name starting with '@', then pass the dictionary as the state to ILogger.Log. This is an odd work around for what I believe should be the standard behavior of this method.

nblumhardt commented 7 years ago

Hi @moogle001 - I think you're looking for:

log.ForContext("Name", someObject, destructureObjects: true).Information("Hello, world!");

This will attach the Name property, without including the object in the message itself.

HTH!

moogle001 commented 7 years ago

Hi @nblumhardt, I am trying to avoid direct use of Serilog throughout my code and instead relying on the Microsoft.Extensions.Logging API. So I am questioning the implementation of ILogger.Log in Serilog.Extensions.Logging.

nblumhardt commented 7 years ago

Ah, right - thanks for the clarification. Unfortunately this is a bit of an annoying design constraint around BeginScope(state) - there's no way to signal whether ToString() is required, e.g. if state is a System.Uri, or if destructuring is desired. Implementation of IEnumerable<KeyValuePair<string,object>> was the best signal I could come up with.

Using a wrapper API like MEL is always going to be a question of compromise. It makes some sense for library code, but for the best application logging experience I personally think you're better off taking a direct dependency on Serilog, NLog or something along those lines :-)