Table of Contents

Class LogEvent

Namespace
Serilog.Events
Assembly
Serilog.dll

A log event.

public class LogEvent
Inheritance
LogEvent
Inherited Members

Constructors

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

Construct a new LogEvent.

public LogEvent(DateTimeOffset timestamp, LogEventLevel level, Exception? exception, MessageTemplate messageTemplate, IEnumerable<LogEventProperty> properties)

Parameters

timestamp DateTimeOffset

The time at which the event occurred.

level LogEventLevel

The level of the event.

exception Exception

An exception associated with the event, or null.

messageTemplate MessageTemplate

The message template describing the event.

properties IEnumerable<LogEventProperty>

Properties associated with the event, including those presented in messageTemplate.

Exceptions

ArgumentNullException

When messageTemplate is

null
ArgumentNullException

When properties is

null

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

Construct a new LogEvent.

[CLSCompliant(false)]
public LogEvent(DateTimeOffset timestamp, LogEventLevel level, Exception? exception, MessageTemplate messageTemplate, IEnumerable<LogEventProperty> properties, ActivityTraceId traceId, ActivitySpanId spanId)

Parameters

timestamp DateTimeOffset

The time at which the event occurred.

level LogEventLevel

The level of the event.

exception Exception

An exception associated with the event, or null.

messageTemplate MessageTemplate

The message template describing the event.

properties IEnumerable<LogEventProperty>

Properties associated with the event, including those presented in messageTemplate.

traceId ActivityTraceId

The id of the trace that was active when the event was created, if any.

spanId ActivitySpanId

The id of the span that was active when the event was created, if any.

Exceptions

ArgumentNullException

When messageTemplate is

null
ArgumentNullException

When properties is

null

Properties

Exception

An exception associated with the event, or null.

public Exception? Exception { get; }

Property Value

Exception

Level

The level of the event.

public LogEventLevel Level { get; }

Property Value

LogEventLevel

MessageTemplate

The message template describing the event.

public MessageTemplate MessageTemplate { get; }

Property Value

MessageTemplate

Properties

Properties associated with the event, including those presented in MessageTemplate.

public IReadOnlyDictionary<string, LogEventPropertyValue> Properties { get; }

Property Value

IReadOnlyDictionary<string, LogEventPropertyValue>

SpanId

The id of the span that was active when the event was created, if any.

[CLSCompliant(false)]
public ActivitySpanId? SpanId { get; }

Property Value

ActivitySpanId?

Timestamp

The time at which the event occurred.

public DateTimeOffset Timestamp { get; }

Property Value

DateTimeOffset

TraceId

The id of the trace that was active when the event was created, if any.

[CLSCompliant(false)]
public ActivityTraceId? TraceId { get; }

Property Value

ActivityTraceId?

Methods

AddOrUpdateProperty(LogEventProperty)

Add a property to the event if not already present, otherwise, update its value.

public void AddOrUpdateProperty(LogEventProperty property)

Parameters

property LogEventProperty

The property to add or update.

Exceptions

ArgumentNullException

When property is

null

AddPropertyIfAbsent(LogEventProperty)

Add a property to the event if not already present.

public void AddPropertyIfAbsent(LogEventProperty property)

Parameters

property LogEventProperty

The property to add.

Exceptions

ArgumentNullException

When property is

null

RemovePropertyIfPresent(string)

Remove a property from the event, if present. Otherwise no action is performed.

public void RemovePropertyIfPresent(string propertyName)

Parameters

propertyName string

The name of the property to remove.

RenderMessage(IFormatProvider?)

Render the message template given the properties associated with the event, and return the result.

public string RenderMessage(IFormatProvider? formatProvider = null)

Parameters

formatProvider IFormatProvider

Supplies culture-specific formatting information, or null.

Returns

string

RenderMessage(TextWriter, IFormatProvider?)

Render the message template to the specified output, given the properties associated with the event.

public void RenderMessage(TextWriter output, IFormatProvider? formatProvider = null)

Parameters

output TextWriter

The output.

formatProvider IFormatProvider

Supplies culture-specific formatting information, or null.

UnstableAssembleFromParts(DateTimeOffset, LogEventLevel, Exception?, MessageTemplate, Dictionary<string, LogEventPropertyValue>, ActivityTraceId, ActivitySpanId)

Construct a LogEvent using pre-allocated values for internal fields. Normally, the LogEvent constructor allocates a dictionary to back Properties, so that this is not unexpectedly shared. This is unnecessary in many integration scenarios, leading to an additional nontrivial Dictionary<TKey, TValue> allocation. This method allows specialized callers to avoid that overhead.

[CLSCompliant(false)]
public static LogEvent UnstableAssembleFromParts(DateTimeOffset timestamp, LogEventLevel level, Exception? exception, MessageTemplate messageTemplate, Dictionary<string, LogEventPropertyValue> properties, ActivityTraceId traceId, ActivitySpanId spanId)

Parameters

timestamp DateTimeOffset

The time at which the event occurred.

level LogEventLevel

The level of the event.

exception Exception

An exception associated with the event, or null.

messageTemplate MessageTemplate

The message template describing the event.

properties Dictionary<string, LogEventPropertyValue>

Properties associated with the event, including those presented in messageTemplate.

traceId ActivityTraceId

The id of the trace that was active when the event was created, if any.

spanId ActivitySpanId

The id of the span that was active when the event was created, if any.

Returns

LogEvent

A constructed LogEvent.

Remarks

Because this method exposes parameters that essentially map 1:1 with internal fields of LogEvent, the parameter list may change across major Serilog versions.

Exceptions

ArgumentNullException

When messageTemplate is

null
ArgumentNullException

When properties is

null