Formatters Improvement
See original GitHub issueIf we have look to the ILogger
definition here https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Framework.Logging.Abstractions/ILogger.cs#L22 we will notice that Log
method contains Func<object, Exception, string> formatter
a formatter that allow us to format the logged message, but what I have notice from many Logger Provider implementations that Formatters should give the ability to format the entire LogEntry
not the message.
Have a look to the following lines:
TraceSourceLogger
https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Framework.Logging.Abstractions/ILogger.cs#L22
DebugLogger
https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Framework.Logging.Debug/DebugLogger.cs#L85
ConsoleLogger
https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Framework.Logging.Console/ConsoleLogger.cs#L79
we will notice that the devs don’t have a control for the entire LogEntry
, they have ability to format the logged message, i think our APIs lack in this point, because the actual message is surrounded with some metadata out of the box, furthermore there’s no why to subclass the actual loggers to provide such things. For instance if i wanna add TimeStamp
into the message using DebugLogger
I can use the formatter
parameter to achieve that, but I will see myself repeating this in places, someone may say “You can create your own method extension”, that’s true but what if wanna use the following format [LogLevel] TimeStamp - Message
I think this is impossible in the current implementation not only in DebugLogger
but in all logger implementation!! i suggest to provide some way to achieve that - doesn’t matter the implementation - either using virtual method or delegate … etc.
Also this will open up the possibility to provide differnt kind of formmaters such as TextFormatter
, JsonFormatter
, XmlFormatter
and so on …
Let me know your feedback, I can provide code sample if you are interest 😄
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
Just wondering if there has been an update on this?
We are migrating an enterprise .net classic framework to .net core that was previously using
TraceListeners
viaTrace.Write(object value)
. We have now migrated to useTraceSourceLogger
.However, the abstraction
Microsoft.Framework.Logging.Abstractions.Ilogger.Log
that it uses only formats the Message, not allow us to write a specific LogEntry. We need the output to look the same as before the migration for backwards compatability.before:
after:
You can get around this by passing in a null formatter to the overload of
Log
. The TraceSourceLogger will ignore. the formatter and use thestate
as the message. This will format the message as expected.However, if there are other listeners attached such as DebugLogger, EventSourceLogger, ConsoleLogger, etc then they will throw with either ArgumentException or NullReferenceException.
Please advise of any known workarounds/solutions?
@davehua Are you able to handle this scenario with Serilog (or some other provider)?
I don’t expect we’ll make more changes in this area in 3.0.