Skip to content

Add WithContext() support via TryGetProperties() #36

Description

@Aaronontheweb

Summary

The log4net plugin does not call TryGetProperties() from LogEventExtensions, which means context properties set via the core WithContext() API (added in Akka.NET 1.5.60) are silently dropped.

Current Behavior

Log4net has its own custom context enrichment system (ForContext() / Log4NetPayload / SetContextProperties()), but it only extracts properties from Log4NetPayload. It never calls TryGetProperties(), so properties added via the core WithContext() extension are ignored.

In Log4NetLogger.cs:

var (message, properties) = logEvent.Message is Log4NetPayload log4NetPayload
    ? (log4NetPayload.Message, log4NetPayload.Properties)
    : (logEvent.Message, Log4NetPayload.Empty.Properties);

Expected Behavior

The logger should call logEvent.TryGetProperties(out var properties) to merge both WithContext() properties and message template properties, the same way the NLog and Serilog plugins do.

Reference Implementations

NLog (NLogLogger.cs):

if (logEvent.TryGetProperties(out var properties))
{
    foreach (var prop in properties)
    {
        logEventInfo.Properties[prop.Key] = prop.Value;
    }
}

Serilog (SerilogLogger.cs) - recently fixed in the same release cycle.

Suggested Fix

In Log4NetLogger.CreateLoggingEvent(), add a TryGetProperties() call and merge the resulting properties into the LoggingEventData.Properties. The custom ForContext() API can be deprecated in favor of core WithContext(), matching what was done for Serilog.

Tests should be added following the pattern in the NLog plugin's WithContextSpecs.cs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions