-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Fix up code snippets and descriptions #51524
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,58 +1,53 @@ | ||||||
| --- | ||||||
| title: Implement a custom logging provider | ||||||
| description: Discover how to implement a custom logging provider with colorized logs, writing custom C# ILogger and ILoggerProvider implementations. | ||||||
| ms.date: 10/20/2025 | ||||||
| ms.date: 02/04/2026 | ||||||
| ms.topic: how-to | ||||||
| --- | ||||||
|
Comment on lines
1
to
6
|
||||||
|
|
||||||
| # Implement a custom logging provider in .NET | ||||||
|
|
||||||
| There are many [logging providers](providers.md) available for common logging needs. You might need to implement a custom <xref:Microsoft.Extensions.Logging.ILoggerProvider> when one of the available providers doesn't suit your application needs. In this article, you learn how to implement a custom logging provider that can be used to colorize logs in the console. | ||||||
| There are many [logging providers](providers.md) available for common logging needs. But you might need to implement a custom <xref:Microsoft.Extensions.Logging.ILoggerProvider> when one of the available providers doesn't suit your application needs. In this article, you learn how to implement a custom logging provider that can be used to colorize logs in the console. | ||||||
|
|
||||||
| > [!TIP] | ||||||
| > The custom logging provider example source code is available in the **Docs GitHub repo**. For more information, see [GitHub: .NET Docs - Console Custom Logging](https://github.com/dotnet/docs/tree/main/docs/core/extensions/snippets/configuration/console-custom-logging). | ||||||
| > The custom logging provider example source code is available in the [docs GitHub repo](https://github.com/dotnet/docs/tree/main/docs/core/extensions/snippets/configuration/console-custom-logging). | ||||||
|
|
||||||
| ### Sample custom logger configuration | ||||||
| ## Sample custom logger configuration | ||||||
|
|
||||||
| The sample creates different color console entries per log level and event ID using the following configuration type: | ||||||
| The sample logger creates different color console entries per log level and event ID using the following configuration type: | ||||||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| :::code language="csharp" source="../snippets/configuration/console-custom-logging/ColorConsoleLoggerConfiguration.cs"::: | ||||||
|
|
||||||
| The preceding code sets the default level to `Information`, the color to `Green`, and the `EventId` is implicitly `0`. | ||||||
| The preceding code sets the default color for the `Information` level to `Green`. The `EventId` is implicitly 0. | ||||||
|
|
||||||
| ### Create the custom logger | ||||||
| ## Create the custom logger | ||||||
|
|
||||||
| The `ILogger` implementation category name is typically the logging source. For example, the type where the logger is created: | ||||||
| The following code snippet shows the `ILogger` implementation: | ||||||
|
|
||||||
| :::code language="csharp" source="../snippets/configuration/console-custom-logging/ColorConsoleLogger.cs"::: | ||||||
|
|
||||||
| The preceding code: | ||||||
| Each logger instance is instantiated by passing a category name, which is typically the type where the logger is created. The `IsEnabled` method checks `_getCurrentConfig().LogLevelToColorMap.ContainsKey(logLevel)` to see if the requested log level is enabled (that is, in the configuration's dictionary of log levels). | ||||||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| - Creates a logger instance per category name. | ||||||
| - Checks `_getCurrentConfig().LogLevelToColorMap.ContainsKey(logLevel)` in `IsEnabled`, so each `logLevel` has a unique logger. In this implementation, each log level requires an explicit configuration entry to log. | ||||||
| It's a good practice to call <xref:Microsoft.Extensions.Logging.ILogger.IsEnabled*?displayProperty=nameWithType> within <xref:Microsoft.Extensions.Logging.ILogger.Log*?displayProperty=nameWithType> implementations since `Log` can be called by any consumer, and there are no guarantees that it was previously checked. The `IsEnabled` method should be very fast in most implementations. | ||||||
|
|
||||||
| It's a good practice to call <xref:Microsoft.Extensions.Logging.ILogger.IsEnabled%2A?displayProperty=nameWithType> within <xref:Microsoft.Extensions.Logging.ILogger.Log%2A?displayProperty=nameWithType> implementations since `Log` can be called by any consumer, and there are no guarantees that it was previously checked. The `IsEnabled` method should be very fast in most implementations. | ||||||
| :::code language="csharp" source="../snippets/configuration/console-custom-logging/ColorConsoleLogger.cs" range="20-23"::: | ||||||
|
||||||
| :::code language="csharp" source="../snippets/configuration/console-custom-logging/ColorConsoleLogger.cs" range="20-23"::: | |
| :::code language="csharp" source="../snippets/configuration/console-custom-logging/ColorConsoleLogger.cs"::: |
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Copilot
AI
Feb 5, 2026
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 highlight range "8-14" appears to be incorrect. Looking at the Program.cs file, line 8 is the call to AddColorConsoleLogger, which starts the configuration block, but the closing bracket is on line 16, not line 14. The highlight should be "8-16" to include the complete AddColorConsoleLogger call.
Uh oh!
There was an error while loading. Please reload this page.