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

Add an overload to LogContext.PushProperty to accept a Func<T> delegate. #1698

Closed
wants to merge 1 commit into from
Closed

Conversation

mattheys
Copy link

I logged an issue #1697 but didn't realise how easy it would be to add the functionaility and do a pull request.

I'm not sure how to best test this, especially to make sure it's thread safe. I've tried the below which both work.

using Serilog;
using Serilog.Context;

Log.Logger = new LoggerConfiguration()
                    .Enrich.FromLogContext()
                    .WriteTo.Console(formatter: new Serilog.Formatting.Compact.CompactJsonFormatter())
                    .CreateLogger();

var stopWatch = System.Diagnostics.Stopwatch.StartNew();
Func<long> GetMillis = () => stopWatch.ElapsedMilliseconds;

using (LogContext.PushProperty("elapsed", GetMillis))
{
    for (int i = 0; i < 10; i++)
    {
        var t = new System.Threading.Thread(() => {
            Log.Information("Thread attempt {i}", i);
            Thread.Sleep(1000);
        });

        t.Start();
        t.Join();
    }

    for (int i = 0; i < 10; i++)
    {
        await Task.Run(async () =>
        {
            Log.Information("Task attempt {i}", i);
            await Task.Delay(1000);
        });
    }

}

Console.ReadKey();


/// </summary>
/// <param name="name">The name of the property.</param>
/// <param name="value">The delegate to the property.</param>
/// <returns>A handle to later remove the property from the context.</returns>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// <returns>A handle to later remove the property from the context.</returns>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting you flagged this to remove as I took the PropertyEnricher.cs as the template to do this and that line is also in there. However this isn't getting merged anyway but you might want to double check this line to make sure it's right.

/// <returns>A handle to later remove the property from the context.</returns>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nblumhardt
Copy link
Member

Thanks for this @mattheys! As you've discovered, LogContext.Push() enables this use case without needing the additional overload on ILogger etc.

As it's a bit of a niche scenario, and already well-supported without additional code in the core, I think we should leave things as-is.

@nblumhardt nblumhardt closed this Jul 15, 2022
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

Successfully merging this pull request may close these issues.

None yet

4 participants