Skip to content
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

Logging latest scope value #141

Open
andreycha opened this issue Apr 3, 2024 · 0 comments
Open

Logging latest scope value #141

andreycha opened this issue Apr 3, 2024 · 0 comments

Comments

@andreycha
Copy link

andreycha commented Apr 3, 2024

Hi,

Currently, the library preserves log4net behavior when it comes to having same property with different values in the log scopes. Values are concatenated with space.

using (_logger.BeginScope(("Prop", 123_)))
{
    _logger.LogInformation("Log 1"); // has "123" in the log property

    using (_logger.BeginScope(("Prop", 456)))
    {
        _logger.LogInformation("Log 2"); // has "123 456" in the log property

        using (_logger.BeginScope(("Prop", 789)))
        {
            _logger.LogInformation("Log 3"); // has "123 456 789" in the log property
        }
    }
}

However, when using Serilog as ILogger implementation, it always adds the latest value to the log properties:

using (_logger.BeginScope(("Prop", 123_)))
{
    _logger.LogInformation("Log 1"); // has "123" in the log property

    using (_logger.BeginScope(("Prop", 456)))
    {
        _logger.LogInformation("Log 2"); // has "456" in the log property

        using (_logger.BeginScope(("Prop", 789)))
        {
            _logger.LogInformation("Log 3"); // has "789" in the log property
        }
    }
}

This behavior better suits our case. We have code paths spread across applications and a shared library where same property might be added multiple times in the library and in the apps. All code uses ILogger abstraction, but different services use different logging frameworks (log4net and Serilog). We'd like to have a consistent behavior in this case. We can plug in custom implementation of ILog4NetLoggingEventFactory but would really like to avoid taking care and maintaining any extra code.

Would it make sense to have a setting which controls whether all values or latest value is written to the logs? Something like:

loggerFactory.AddLog4Net(new Log4NetProviderOptions {
    UseLatestScopeValue = true
});

If this is sensible, I can take care of the PR once we agree on the design and naming.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant