Skip to main content

Overview

The LogEvent class represents a single log event in Serilog. It encapsulates all information about a logging occurrence, including the timestamp, severity level, message template, properties, exception details, and distributed tracing identifiers.

Constructors

LogEvent(DateTimeOffset, LogEventLevel, Exception, MessageTemplate, IEnumerable<LogEventProperty>)

Construct a new log event.
timestamp
DateTimeOffset
required
The time at which the event occurred.
level
LogEventLevel
required
The level of the event.
exception
Exception
An exception associated with the event, or null.
messageTemplate
MessageTemplate
required
The message template describing the event.
properties
IEnumerable<LogEventProperty>
required
Properties associated with the event, including those presented in the message template.
Exceptions:
  • ArgumentNullException - When messageTemplate or properties is null

LogEvent(DateTimeOffset, LogEventLevel, Exception, MessageTemplate, IEnumerable<LogEventProperty>, ActivityTraceId, ActivitySpanId)

Construct a new log event with distributed tracing identifiers.
timestamp
DateTimeOffset
required
The time at which the event occurred.
level
LogEventLevel
required
The level of the event.
exception
Exception
An exception associated with the event, or null.
messageTemplate
MessageTemplate
required
The message template describing the event.
properties
IEnumerable<LogEventProperty>
required
Properties associated with the event, including those presented in the message template.
traceId
ActivityTraceId
required
The id of the trace that was active when the event was created, if any.
spanId
ActivitySpanId
required
The id of the span that was active when the event was created, if any.
Exceptions:
  • ArgumentNullException - When messageTemplate or properties is null

Properties

Timestamp
DateTimeOffset
The time at which the event occurred.
Level
LogEventLevel
The level of the event.
TraceId
ActivityTraceId?
The id of the trace that was active when the event was created, if any. Returns null if no trace was active.
SpanId
ActivitySpanId?
The id of the span that was active when the event was created, if any. Returns null if no span was active.
MessageTemplate
MessageTemplate
The message template describing the event.
Properties
IReadOnlyDictionary<string, LogEventPropertyValue>
Properties associated with the event, including those presented in the message template.
Exception
Exception
An exception associated with the event, or null.

Methods

RenderMessage(TextWriter, IFormatProvider)

Render the message template to the specified output, given the properties associated with the event.
output
TextWriter
required
The output writer to render the message to.
formatProvider
IFormatProvider
Supplies culture-specific formatting information, or null.
var writer = new StringWriter();
logEvent.RenderMessage(writer);
string message = writer.ToString();

RenderMessage(IFormatProvider)

Render the message template given the properties associated with the event, and return the result.
formatProvider
IFormatProvider
Supplies culture-specific formatting information, or null.
Returns: string - The rendered message
string message = logEvent.RenderMessage();

AddOrUpdateProperty(LogEventProperty)

Add a property to the event if not already present, otherwise, update its value.
property
LogEventProperty
required
The property to add or update.
Exceptions:
  • ArgumentNullException - When property is null
var property = new LogEventProperty("UserId", new ScalarValue(123));
logEvent.AddOrUpdateProperty(property);

AddPropertyIfAbsent(LogEventProperty)

Add a property to the event if not already present.
property
LogEventProperty
required
The property to add.
Exceptions:
  • ArgumentNullException - When property is null
var property = new LogEventProperty("RequestId", new ScalarValue("abc123"));
logEvent.AddPropertyIfAbsent(property);

AddPropertyIfAbsent(ILogEventPropertyFactory, string, object, bool)

Add a property to the event if not already present using a factory.
factory
ILogEventPropertyFactory
required
Factory for creating the property to add to the event.
name
string
required
The name of the property.
value
object
The value of the property.
destructureObjects
bool
default:"false"
If 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.
Exceptions:
  • ArgumentNullException - When factory is null
logEvent.AddPropertyIfAbsent(logger, "Context", contextObject, destructureObjects: true);

RemovePropertyIfPresent(string)

Remove a property from the event, if present. Otherwise no action is performed.
propertyName
string
required
The name of the property to remove.
logEvent.RemovePropertyIfPresent("SensitiveData");

Static Methods

UnstableAssembleFromParts

Construct a LogEvent using pre-allocated values for internal fields. This method allows specialized callers to avoid dictionary allocation overhead.
timestamp
DateTimeOffset
required
The time at which the event occurred.
level
LogEventLevel
required
The level of the event.
exception
Exception
An exception associated with the event, or null.
messageTemplate
MessageTemplate
required
The message template describing the event.
properties
Dictionary<string, LogEventPropertyValue>
required
Properties associated with the event, including those presented in the message template.
traceId
ActivityTraceId
required
The id of the trace that was active when the event was created, if any.
spanId
ActivitySpanId
required
The id of the span that was active when the event was created, if any.
Returns: LogEvent - A constructed log event Exceptions:
  • ArgumentNullException - When messageTemplate or properties is null
Because this method exposes parameters that map 1:1 with internal fields of LogEvent, the parameter list may change across major Serilog versions.
var properties = new Dictionary<string, LogEventPropertyValue>();
var logEvent = LogEvent.UnstableAssembleFromParts(
    DateTimeOffset.Now,
    LogEventLevel.Information,
    null,
    messageTemplate,
    properties,
    default,
    default
);

Build docs developers (and LLMs) love