-
Notifications
You must be signed in to change notification settings - Fork 0
Request Logger Options
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.
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.
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.
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.
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.
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.
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.
- Serilog Extensions — install, wiring, and what the middleware logs
-
Request lifecycle — the
RequestContextmembers the templates and enrichment read from
Documents Plumber v4.x · Repository · MIT License · Report an issue
Getting Started
Pipeline (core)
Testing
Serilog Extensions
Diagnostics
Recipes
- AWS Lambda — API Gateway
- AWS Lambda — SQS
- Azure Functions — HTTP
- SQS polling console
- ASP.NET Core integration
- BackgroundService worker
- Webhook receiver
- Multi-command CLI
- File watcher
- Configuration reload
Repo · NuGet · NuGet — Testing · NuGet — Serilog · NuGet — Diagnostics