Skip to content

Export/Import contents of LogContext #773

@IanYates

Description

@IanYates

Hi,

Following on from #529 , I've got my own wrapped up timings methods where, if the Action or Func takes longer than expected I log out messages repeatedly to Seq via Serilog. It's helpful to see how much activity is spent waiting on some shared mutex and I can track back to the source (although my Seq filtering skills in 3.x are not well exercised yet).

These timing messages are done via a timer - on its first trigger it'll log a warning and subsequent triggers it bumps things up to error. This is different from simply logging start & finish + duration since I'd like insight into some process that's longer-running than expected whilst it's still running. (example: it came in handy when I accidentally had an async void Action<T>, rather than an async task Func<T,Task>, lambda leading to some weird deadlock behaviour).

Unfortunately this all happens on a background timer thread so the contents of LogContext that I'd like to include is effectively lost.

A suggested LogContext API in from #529 was

var parent = LogContext.Export();

Task.Run(() =>
{
   LogContext.Import(parent);

   Log.Information("Here 'tis");
});

Is this still a good idea? Would a PR along these lines be accepted, or is something like it already possible today and I just haven't noticed?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions