Add structured logging support#142
Open
andreycha wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds structured logging support to the log4net provider by capturing message templates and template arguments as LoggingEvent properties, and exposes an additional enrichment hook for customization.
Changes:
- Add
{OriginalFormat}/message template and template arguments fromILoggerstate intoLoggingEvent.Properties(including mapping toMessageTemplate). - Introduce
EnrichProperties(...)as an overridable extension point (and adjust enrichment ordering vs scopes). - Update tests and documentation to reflect new customization approach.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Unit.Tests/Mocks/OverriddenScopesLoggingEventFactory.cs | Test helper factory overriding EnrichWithScopes for custom property injection. |
| tests/Unit.Tests/Mocks/OverriddenPropertiesLoggingEventFactory.cs | Test helper factory overriding EnrichProperties for custom property injection. |
| tests/Unit.Tests/Log4NetLoggerTests.cs | Replace Moq.Protected approach with concrete test factories; add tests for overridden enrichment paths. |
| src/Microsoft.Extensions.Logging.Log4Net.AspNetCore/Log4NetLoggingEventFactory.cs | Add structured logging property enrichment (MessageTemplate + args) and new EnrichProperties hook. |
| doc/ModifyingLoggingBehaviour.md | Document customization via implementing the factory interface and by inheriting the default factory. |
| README.md | Document structured logging behavior at a high level. |
Comments suppressed due to low confidence (1)
doc/ModifyingLoggingBehaviour.md:96
- The documentation sample now declares
CustomLoggingEventFactory : ILog4NetLoggingEventFactory, but the shownCreateLoggingEventmethod has no access modifier (so it would beprivate) and the signature shown below no longer matches the actualILog4NetLoggingEventFactory.CreateLoggingEventsignature (notably theinparameter andIExternalScopeProvider scopeProvider). Please update the snippet so it would compile against the current interface.
public class CustomLoggingEventFactory : ILog4NetLoggingEventFactory
{
LoggingEvent CreateLoggingEvent<TState>(
MessageCandidate<TState> messageCandidate,
ILogger logger,
Log4NetProviderOptions options
)
{
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+178
to
+196
| protected virtual void EnrichProperties<TState>(LoggingEvent loggingEvent, in MessageCandidate<TState> messageCandidate) | ||
| { | ||
| loggingEvent.Properties[EventIdProperty] = messageCandidate.EventId; | ||
|
|
||
| // State is always passed as Microsoft.Extensions.Logging.FormattedLogValues which is internal | ||
| // but implements IReadOnlyCollection<KeyValuePair<string, object>> | ||
| if (messageCandidate.State is IReadOnlyCollection<KeyValuePair<string, object>> stateProperties) | ||
| { | ||
| foreach (var kvp in stateProperties) | ||
| { | ||
| var key = kvp.Key; | ||
| if (kvp.Key == OriginalFormatProperty && kvp.Value is string) | ||
| { | ||
| // change property name to match Serilog | ||
| key = "MessageTemplate"; | ||
| } | ||
| loggingEvent.Properties[key] = ConvertValue(kvp.Value); | ||
| } | ||
| } |
Comment on lines
+153
to
+163
| It is also possible to inherit default implementation `LoggingEventFactory` and override `EnrichWithScope()` and/or `EnrichProperties` methods: | ||
|
|
||
| ```csharp | ||
| public class CustomLoggingEventFactory : LoggingEventFactory | ||
| { | ||
| protected override void EnrichProperties<TState>(LoggingEvent loggingEvent, in MessageCandidate<TState> messageCandidate) | ||
| { | ||
| base.EnrichProperties(loggingEvent, in messageCandidate); | ||
| loggingEvent.Properties["ApplicationName"] = Assembly.GetEntryAssembly().GetName().Name; | ||
| loggingEvent.Properties["ApplicationVersion"] = Assembly.GetEntryAssembly().GetName().Version.ToString(); | ||
| } |
Comment on lines
299
to
300
| LoggerExternalScopeProvider scopeProvider = new LoggerExternalScopeProvider(); | ||
|
|
| } | ||
|
|
||
| [Fact] | ||
| public void Log_Should_Emit_Custom_Scope_Operations_Created_By_Overriden_Enrich_Properties_Method() |
Comment on lines
+5
to
+7
| #if NET6_0 | ||
| using Microsoft.Extensions.Logging.Log4Net.AspNetCore.Entities; | ||
| using Microsoft.Extensions.Logging.Log4Net.AspNetCore.Scope; | ||
| #endif |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Owner
|
@copilot apply changes based on the comments in this thread |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Structured logging is a standard practice nowadays. When using
ILoggerand co .NET analyzers even encourage to use it. To comply with the best practice and other logger implementations, this PR adds support for structured logging.Changes
Things to discuss
LoggingEventFactorymock was replaced with actual custom implementations in few tests. Main reason is that Moq doesn't allow to mock protected generic method. It also makes test setup easier to understand.