serilog / serilog-extensions-hosting

Serilog logging for Microsoft.Extensions.Hosting
Apache License 2.0
141 stars 34 forks source link

Add new mechanism to enable creation of DiagnosticContext scopes #49

Closed gkinsman closed 9 months ago

gkinsman commented 3 years ago

This PR is a WIP proposal to fix #47. There's still a few unanswered questions below as I'm not sure this is 100% the right direction :).

It introduces a new method to IDiagnosticContext that allows users to begin a new DiagnosticContextScope that collects properties via IDiagnosticContext:

DiagnosticContextScope Begin(string messageTemplate, params object[] properties);

Open Questions

There's still a number of areas that haven't been fully thought through on the interface method:

Sample Usage

...
var operationId = Guid.NewGuid();
using (_diagnosticContext.Begin("Worker executed operation {OperationId} at time {Time}", operationId, DateTimeOffset.Now)) {
    await _executor.DoWork(operationId);
}
...

public async Task DoWork(Guid operationId)
{
    ...
    _diagnosticContext.Set("OperationName", randomOperation);
    _diagnosticContext.Set("ThisOperationId", operationId);
    ...
}
...

Which produces this event: image

nblumhardt commented 3 years ago

Thanks for sending this; will have a dig in properly as soon as I have a chance (the last few months have been a bit crazy, here :-))

gkinsman commented 3 years ago

Thanks for sending this; will have a dig in properly as soon as I have a chance (the last few months have been a bit crazy, here :-))

No worries Nick! Hope everything's well šŸ™‚

nblumhardt commented 3 years ago

@gkinsman snowed :-D but digging out hehe šŸ‘