Skip to content

Latest commit

 

History

History
69 lines (56 loc) · 2.64 KB

README.md

File metadata and controls

69 lines (56 loc) · 2.64 KB

Build Status

.NET Tests .NET Publish Nuget Nuget

Plumber Logo

Plumber Serilog Extensions

Plumber: Pipelines for host-free projects like AWS Lambda, Console, etc.

The Plumber.Serilog.Extensions package provides Serilog middleware extensions for the Plumber pipeline library.

IRequestHandler Extension Methods

public static IRequestHandler<TRequest, TResponse> UseSerilogRequestLogging<TRequest, TResponse>(
    this IRequestHandler<TRequest, TResponse> handler)
    where TRequest : class
public static IRequestHandler<TRequest, TResponse> UseSerilogRequestLogging<TRequest, TResponse>(
    this IRequestHandler<TRequest, TResponse> handler,
    Action<RequestLoggerOptions<TRequest, TResponse>> configureOptions)
    where TRequest : class

Sample Usage

using Microsoft.Extensions.DependencyInjection;
using Plumber;
using Plumber.Serilog.Extensions;
using Serilog;
using Serilog.Events;
using Serilog.Exceptions;
using Serilog.Formatting.Compact;

var request = "Hello, World!";

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .Enrich.FromLogContext()
    .Enrich.WithExceptionDetails()
    .WriteTo.Console(new CompactJsonFormatter())
    .CreateLogger();

var handlerBuilder = RequestHandlerBuilder.Create<string, string>();

_ = handlerBuilder.Services
    .AddSerilog()
    .AddLogging(loggingBuilder => loggingBuilder.AddSerilog());

var handler = handlerBuilder.Build();

_ = handler
    .UseSerilogRequestLogging(options =>
    {
        options.LogLevel = LogEventLevel.Information;
        options.EnrichDiagnosticContext = (diagnosticContext, context) =>
        {
            diagnosticContext.Set(nameof(context.Request), context.Request);
            diagnosticContext.Set(nameof(context.Response), context.Response);
        };
    })
    .Use<ToLowerMiddleware>();

var response = await handler.InvokeAsync(request);