1

I'm using SerilogMetrics's BeginTimedOperation() in a Web API, and it would be really great to be able to use the HttpRequestNumber or HttpRequestId properties (from the respective Serilog.Extra.Web enrichers) as the identifier, making it super easy to correlate timing-related log entries with others across a request.

Something like:

using (logger.BeginTimedOperation("doing some work", HttpRequestNumberEnricher.CurrentRequestNumber))
{ ... }

Short of poking around in HttpContext.Current for the magically- (i.e. non-public) named properties, is this achievable? Thanks!

David Rubin
  • 1,610
  • 1
  • 17
  • 28

1 Answers1

0

If you begin a timed operation during a web request, the operation's events will already be tagged with the HttpRequestId.

You'll see it when logging to a structured log server like Seq, but if you're writing it out to a text file or trace then the property won't be included in the output message by default. To show it in there use something like:

.WriteTo.File(...,
    outputTemplate: "{Timestamp} [{Level}] ({HttpRequestId}) {Message} ...")

The logging methods use a default template you can draw on for inspiration, and there's some info spread around the wiki though there's no definitive reference.

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101