Table of Contents

Class MessageTemplate

Namespace
Serilog.Events
Assembly
Serilog.dll

Represents a message template passed to a log method. The template can subsequently render the template in textual form given the list of properties.

public class MessageTemplate
Inheritance
MessageTemplate
Inherited Members

Constructors

MessageTemplate(IEnumerable<MessageTemplateToken>)

Construct a message template using manually-defined text and property tokens.

public MessageTemplate(IEnumerable<MessageTemplateToken> tokens)

Parameters

tokens IEnumerable<MessageTemplateToken>

The text and property tokens defining the template.

MessageTemplate(string, IEnumerable<MessageTemplateToken>)

Construct a message template using manually-defined text and property tokens.

public MessageTemplate(string text, IEnumerable<MessageTemplateToken> tokens)

Parameters

text string

The full text of the template; used by Serilog internally to avoid unneeded string concatenation.

tokens IEnumerable<MessageTemplateToken>

The text and property tokens defining the template.

Exceptions

ArgumentNullException

When text is

null
ArgumentNullException

When tokens is

null

Properties

Empty

Represents the empty message template.

public static MessageTemplate Empty { get; }

Property Value

MessageTemplate

Text

The raw text describing the template.

public string Text { get; }

Property Value

string

Tokens

The tokens parsed from the template.

public IEnumerable<MessageTemplateToken> Tokens { get; }

Property Value

IEnumerable<MessageTemplateToken>

Methods

Render(IReadOnlyDictionary<string, LogEventPropertyValue>, IFormatProvider?)

Convert the message template into a textual message, given the properties matching the tokens in the message template.

public string Render(IReadOnlyDictionary<string, LogEventPropertyValue> properties, IFormatProvider? formatProvider = null)

Parameters

properties IReadOnlyDictionary<string, LogEventPropertyValue>

Properties matching template tokens.

formatProvider IFormatProvider

Supplies culture-specific formatting information, or null.

Returns

string

The message created from the template and properties. If the properties are mismatched with the template, the template will be returned with incomplete substitution.

Exceptions

ArgumentNullException

When properties is

null

Render(IReadOnlyDictionary<string, LogEventPropertyValue>, TextWriter, IFormatProvider?)

Convert the message template into a textual message, given the properties matching the tokens in the message template.

public void Render(IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output, IFormatProvider? formatProvider = null)

Parameters

properties IReadOnlyDictionary<string, LogEventPropertyValue>

Properties matching template tokens.

output TextWriter

The message created from the template and properties. If the properties are mismatched with the template, the template will be returned with incomplete substitution.

formatProvider IFormatProvider

Supplies culture-specific formatting information, or null.

Exceptions

ArgumentNullException

When properties is

null
ArgumentNullException

When output is

null

ToString()

Render the template as a string.

public override string ToString()

Returns

string

The string representation of the template.