Using operation context

This page lists preparation steps required to begin using operation context in application logs.

Prerequisites: install context module, read operation context section.

Step 1: enable operation context on a log instance using WithOperationContext() extension:

log = log.WithOperationContext();

Step 2: define context scopes by constructing and disposing OperationContextTokens:

using (new OperationContextToken("op1"))
{
// log something
using (new OperationContextToken("op2")
{
// log something
}
}

There's also an ILog extension named ForOperationContext which is just a convenient shortcut for creating OperationContextTokens and does not really depend on an ILog instance:

using (log.ForOperationContext("op")) { }