Class LogContext
Holds ambient properties that can be attached to log events. To configure, use the FromLogContext() method.
public static class LogContext
- Inheritance
-
LogContext
- Inherited Members
Examples
Configuration:
var log = new LoggerConfiguration()
.Enrich.FromLogContext()
...
Usage:
using (LogContext.PushProperty("MessageId", message.Id))
{
Log.Information("The MessageId property will be attached to this event");
}
Remarks
The scope of the context is the current logical thread, using AsyncLocal (and so is preserved across async/await calls).
Methods
Clone()
Obtain an enricher that represents the current contents of the LogContext. This can be pushed back onto the context in a different location/thread when required.
public static ILogEventEnricher Clone()
Returns
- ILogEventEnricher
An enricher that represents the current contents of the LogContext.
Push(ILogEventEnricher)
Push an enricher onto the context, returning an IDisposable that must later be used to remove the property, along with any others that may have been pushed on top of it and not yet popped. The property must be popped from the same thread/logical call context.
public static IDisposable Push(ILogEventEnricher enricher)
Parameters
enricher
ILogEventEnricherAn enricher to push onto the log context
Returns
- IDisposable
A token that must be disposed, in order, to pop properties back off the stack.
Exceptions
- ArgumentNullException
When
enricher
isnull
Push(params ILogEventEnricher[])
Push multiple enrichers onto the context, returning an IDisposable that must later be used to remove the property, along with any others that may have been pushed on top of it and not yet popped. The property must be popped from the same thread/logical call context.
public static IDisposable Push(params ILogEventEnricher[] enrichers)
Parameters
enrichers
ILogEventEnricher[]Enrichers to push onto the log context
Returns
- IDisposable
A token that must be disposed, in order, to pop properties back off the stack.
Exceptions
- ArgumentNullException
When
enrichers
isnull
- See Also
PushProperty(string, object?, bool)
Push a property onto the context, returning an IDisposable that must later be used to remove the property, along with any others that may have been pushed on top of it and not yet popped. The property must be popped from the same thread/logical call context.
public static IDisposable PushProperty(string name, object? value, bool destructureObjects = false)
Parameters
name
stringThe name of the property.
value
objectThe value of the property.
destructureObjects
boolIf true, and the value is a non-primitive, non-array type, then the value will be converted to a structure; otherwise, unknown types will be converted to scalars, which are generally stored as strings.
Returns
- IDisposable
A handle to later remove the property from the context.
Reset()
Remove all enrichers from LogContext for the current async scope.
public static void Reset()
Suspend()
Remove all enrichers from the LogContext, returning an IDisposable that must later be used to restore enrichers that were on the stack before Suspend() was called.
public static IDisposable Suspend()
Returns
- IDisposable
A token that must be disposed, in order, to restore properties back to the stack.