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

Extensibility opportunity for IDiagnosticContext? #47

Open
gkinsman opened this issue May 28, 2021 · 1 comment
Open

Extensibility opportunity for IDiagnosticContext? #47

gkinsman opened this issue May 28, 2021 · 1 comment

Comments

@gkinsman
Copy link

Hi there!

Lately I've been thinking about IDiagnosticContext (further from a question I raised on gitter), and how it might be possible to make use of it outside the context of ASP.NET. I thought I'd open this issue to get the conversation started!

It might be worth thinking about this problem in terms of the SerilogTimings library, as it could be a consumer of a more general IDiagnosticContext.

In an ideal world, it might be possible to do this:

var username = "George";
using var operation = Operation.Begin("Updating phone number for {Username}", username);

try {
  // do something
  _diagnosticContext.Set("PhoneNumberValidated", true);

  operation.Complete();
} catch(Exception e) {
  operation.Abandon(e);
}

Which would emit the log:

{
    "@t": "2021-05-28T15:49:51.6627798Z",
    "@mt": "Updating phone number for {Username} {Outcome} in {Elapsed:0.0} ms",
    "@m": "Updating phone number for George completed in 0.0 ms",
    "@i": "c376adfc",
    "@r": ["0.0"],
    "Username": "George",
    "PhoneNumberValidated": true,
    "Outcome": "completed",
    "Elapsed": 0.0001
}

So what would it take to get here? I'm probably going to be pretty far off reality, but from my crude reading of the code:

  • There needs to be some way to initiate an AmbientDiagnosticContextCollector.Begin to start collecting ambient properties. It feels like this mechanism could handle nested situations where a collection is already in progress (e.g. an operation within another, or operation in an MVC controller). Perhaps if there is a collection already in progress this could be a noop, but maybe you'd want to exclude properties added outside the current call context.
  • Could this be implemented as an enricher that pulls from the ambient collector? It's a standard way of adding properties, so maybe there could be a way to get an enricher from an IDiagnosticContext to pass to a log event or Operation.Complete()?

To provide a real use case, I'd like to implement an IDiagnosticContext-like mechanism around our message handlers to reduce our log volume and increase cardinality, but currently there's no way to control the lifecycle of this collector so I'd need to implement it all myself.

I'd be keen to push this forward, but I'm a bit unsure of what the implementation of this might look like. What are peoples thoughts?

Thanks!

@nblumhardt
Copy link
Member

Thanks for writing this up, @gkinsman - sorry about the slow reply, need to get some time free to really think through this one, but it definitely lines up with the general direction I had in mind for IDiagnosticContext.

I think shaping up a strawman proposal could be a good way to get the ball rolling. I'll loop back around to it, if you don't beat me to one :-)

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 a pull request may close this issue.

2 participants