question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Formatters Improvement

See original GitHub issue

If 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:closed
  • Created 8 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
davehuacommented, Jul 10, 2018

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 via Trace.Write(object value). We have now migrated to use TraceSourceLogger.

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:

<Performance>
  <ApplicationName>MyApp</ApplicationName>
  <RequestData>
    <RequestHeaders>
  </RequestHeaders>
  </RequestData> 
</Performance>

after:

<Message>
  <ServerDate>2018-07-06T16:04:22.331</ServerDate>
  <Value>
	<Performance>
	  <ApplicationName>MyApp</ApplicationName>
	  <RequestData>
		<RequestHeaders>
	  </RequestHeaders>
	  </RequestData> 
	</Performance>
  </Value>
</Message>

You can get around this by passing in a null formatter to the overload of Log. The TraceSourceLogger will ignore. the formatter and use the state 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?

0reactions
muratgcommented, Jan 8, 2019

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Swift Foundation Formatter Improvements
Swift Foundation Formatter Improvements ... The Foundation framework has a variety of formatters for working with dates, numbers, lists, ...
Read more >
Using Formatters to Improve Maintainability | NimblePros Blog
Linters and formatters go well together at improving consistency throughout your code. Both analyze code and make suggestions to improve it.
Read more >
{fmt} 10.0 released with more formatters, improved floating ...
So am I right in assuming format_as is now a better option for custom types than specialising fmt::formatter in most cases?
Read more >
Are formatters better than linters? | by Brujo Benavides
Particularly I want to look at the following slide… Formatter > Linter. Formatters are more than Linters. Are Formatters “Linters on Steroids” ...
Read more >
Improve Code Quality With the VSCode Prettier Formatter
VSCode Prettier is a popular code formatter that helps developers format their code automatically. In this blog post, we'll take a closer look ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found