-
Notifications
You must be signed in to change notification settings - Fork 25.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom/logger #18258
Custom/logger #18258
Conversation
* Add a section in the docs aspnetcore/fundamentals/logging/index.md * Add a sample at aspnetcore/fundamentals/logging/loggermessage/samples/3.1/CustomLogger
@JuergenGutsch please review the changed files and the public build. cc @serpent5 |
@Rick-Anderson Awesome. Looks good from my perspective 👍 Many Thanks 😊 |
_logger.LogWarning("The Index action was just started"); | ||
_logger.LogError("Not really an error. Just to show the custom logger"); | ||
_logger.LogDebug("End Index action in the HomeController"); | ||
var routeInfo = ControllerContext.ToCtxString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better 😄
This comment has been minimized.
This comment has been minimized.
@serpent5 I had similar thoughts on the organization, but I just wanted to get it in a more readable state. I think your suggestions would be a nice improvement. I'll ping the PU and see if they can do a quick review. |
@BrennanConroy can you do a quick review or suggest a reviewer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good
|
||
[!code-csharp[](index/samples/3.x/CustomLogger/Startup1.cs?name=snippet)] | ||
|
||
The preceding code is verbose. The logger registration can be encapsulated, resulting in the following: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of this seems unneeded for this doc 🤷
|
||
[!code-csharp[](index/samples/3.x/CustomLogger/ColoredConsoleLogger/ColoredConsoleLoggerProvider.cs?name=snippet)] | ||
|
||
In the preceding code, <xref:Microsoft.Build.Logging.LoggerDescription.CreateLogger*> creates a single instance of the `ColoredConsoleLogger` per category name and stores it in the <xref:System.Collections.Concurrent.ConcurrentDictionary`1>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ConcurrentDictionary xref seems broken
|
||
public bool IsEnabled(LogLevel logLevel) | ||
{ | ||
return logLevel == _config.LogLevel; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to use >=
for the check instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not, if you want to set the color for a specific log level
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuergenGutsch reverted to your original code
|
||
[!code-csharp[](index/samples/3.x/CustomLogger/ColoredConsoleLogger/ColoredConsoleLoggerConfiguration.cs?name=snippet)] | ||
|
||
The preceding code sets the default level to `Warning` and the color to `Yellow`. If the `EventId` is set to 0, we will log all events. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the EventId
being used to limit what is being logged?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just a stupid demo, to show how to add a custom demo. Maybe some things doesn't really make sense. This logger is just coloring the log outputs.
In that case you are able to create an different colored output for a specific event id.
+@JunTaoLuo in case you want to take a look |
Co-authored-by: Brennan <brecon@microsoft.com>
@BrennanConroy can you review @serpent5 outline |
@JuergenGutsch can you review my last commit? |
@@ -22,7 +22,7 @@ public IDisposable BeginScope<TState>(TState state) | |||
|
|||
public bool IsEnabled(LogLevel logLevel) | |||
{ | |||
return logLevel == _config.LogLevel; | |||
return logLevel >= _config.LogLevel; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in a comment of the review by @BrennanConroy this would enable the the output for a log level higher or equal to the configured one, instead of a specific one. This should be reflected in the text.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I call that out in the text.
The preceding code: | ||
|
||
* Creates a logger instance per category name. | ||
* Checks `logLevel == _config.LogLevel` in `IsEnabled`, so each `logLevel` has a unique logger. Generally, loggers should also be enabled for all higher log levels: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checks
logLevel == _config.LogLevel
inIsEnabled
This doesn't match the code and the code snippet anymore, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuergenGutsch I reverted to your original code. The snippets are imported into the doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuergenGutsch oops, forgot to push that change. It's fixed now
Internal review URL
Public review: