BeginScope() is generic over TState. This makes sense at one level - a provider that doesn’t implement scoping can ignore BeginScope(42) - that is, TState being a struct - without any boxing overhead.

On another level, this does create some ambiguity. TState can be absolutely anything, so while BeginScope("Checking mail") has a pretty clear interpretation, what about BeginScope(Thread.CurrentThread)? BeginScope(new Dictionary<string, object>{ ["OrderId"] = 54 })? The possible state arguments are completely open-ended, and just blindly adding them to the scope hierarchy using ToString() won’t always record useful information.

I recently worked through this question and updated the Serilog provider (at the time of writing, you’ll need to grab the -Pre version) and new Seq provider based on my conclusions. In the hope that it might help others implement the API cleanly, here are the rules I ended up with.

1. If the state argument is IEnumerable<KeyValuePair<string, object>>, the values are attached to the event directly

This may not be entirely intuitive, if you think of Scope as a trail of breadcrumbs. If you’ve dealt with correlating log events in a distributed application, you’ll recognize that often you’re interested in log events related to a specific transaction, customer, request, thread, machine or endpoint.

In the absence of any other API for the purpose, BeginScope() is how these correlation ids need to be attached in Microsoft.Extensions.Logging:

In this case, events created in the scope will have two additional properties attached: CustomerId and OrderId.

Types that implement IEnumerable<KeyValuePair<string, object>> rarely have any other useful representation; ToString() on a dictionary generally just provides the type name. These values therefore don’t result in a breadcrumb-like Scope property.

2. If state is notIEnumerable<KeyValuePair<string, object>>, it’s added to the Scope property

In other cases, where the state doesn’t carry a list of properties, it adds to an implied Scope property on the event:

3. If the state is created with a message template, the rendered value is added to Scope

The BeginScope() extension method used here creates an instance of the internal FormattedLogValues class, which is IEnumerable<KeyValuePair<string, object>>, so the properties OrderId and CustomerId will be added to the event via rule (1). (The template is detected by the presence of an {OriginalFormat} property attached to the event.)

The message template can also be rendered into the text "Order 54 for customer 12345", so this rendered value is added to the Scope property as well.

How should BeginScope() be used?

Just because you can pass practically anything to BeginScope() doesn’t mean that you necessarily should. It’s important for a provider to capture the most useful information it can, hence this article, but as a diagnostic aid I’m strongly biased towards the judicious use of Dictionary<string, object> scope values, rather than hierarchical scope names.

Structured key/value properties are self-documenting and simpler to query. A log filter like OrderId = 54 is easy to formulate. The Scope array doesn’t attach any meaning to each level in the hierarchy: Scope[?] = 'Downloading messages' might retrieve a meaningful set of events, but Scope[0] = 42 is just nonsense.

Of course, the flexibility is there, so you should use the API in the manner that makes the most sense to you :-).

Implementing or using BeginScope()? In a similar or different manner? It’d be interesting to hear your opinions!