Skip to content

Request Logger Options

Mark Lauter edited this page Jun 13, 2026 · 2 revisions

RequestLoggerOptions

RequestLoggerOptions<TRequest, TResponse> configures the Serilog request-logging middleware — the level it logs at, the message template, the properties that fill the template, per-request enrichment, and whether downstream exceptions rethrow. It's generic over the pipeline's request and response types, with TRequest : class.

For wiring and logging behavior, see Serilog Extensions. This page is the property reference.

Supplying options

Three paths, covered on Serilog Extensions: the configureOptions parameter on AddSerilogRequestLogging, an inline Action<RequestLoggerOptions<TRequest, TResponse>> passed to UseSerilogRequestLogging (takes precedence), or a Configure<RequestLoggerOptions<TRequest, TResponse>>(...) registration resolved from the container as IOptions<T>. With none, the middleware runs on the defaults below.

Properties

LogLevel

LogEventLevel — default Information.

The level a successful request logs at. A failed request always logs at Error regardless of this setting, so LogLevel controls only the happy path.

MessageTemplate

string — default "Request {RequestId} completed in {Elapsed:0.0000} ms".

The Serilog message template for the completion event. Every token in the template must be supplied by GetMessageTemplateProperties, or Serilog renders the token literally instead of a value.

GetMessageTemplateProperties

Func<RequestContext<TRequest, TResponse>, IEnumerable<LogEventProperty>> — default returns RequestId (from context.Id) and Elapsed (from context.Elapsed.TotalMilliseconds).

Produces the properties that fill MessageTemplate. Override it when you change the template to reference different values:

options.MessageTemplate = "Request {RequestId} for {Tenant} took {Elapsed:0.0} ms";
options.GetMessageTemplateProperties = context =>
[
    new LogEventProperty("RequestId", new ScalarValue(context.Id)),
    new LogEventProperty("Tenant", new ScalarValue(context.Request.TenantId)),
    new LogEventProperty("Elapsed", new ScalarValue(context.Elapsed.TotalMilliseconds)),
];

MessageTemplate and GetMessageTemplateProperties are a matched pair — change one and change the other to keep the tokens and properties aligned. A literal {RequestId} in the rendered output means the two drifted apart.

EnrichDiagnosticContext

Action<IDiagnosticContext, RequestContext<TRequest, TResponse>>? — default null.

Runs once per request before the completion event is written. Properties set through the IDiagnosticContext attach to that event, which is how request- and response-derived values reach the log without a separate statement:

options.EnrichDiagnosticContext = (diagnosticContext, context) =>
    diagnosticContext.Set("Response", context.Response);

These enrichment properties are additive to the message-template properties — they decorate the event without needing a matching token in MessageTemplate.

ThrowOnException

bool — default true.

When true, a downstream exception is logged at Error and rethrown, so callers still see the failure. Set it to false to log the failure and swallow it — the pipeline completes as though it succeeded, which suits fire-and-forget consumers that record errors rather than surface them.

See also

Clone this wiki locally