Skip to content

codingbadger/serilog-sinks-buffered

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

serilog-sinks-buffered

A buffered sink for SeriLog

You can use it as you would any other sink:

BufferedSink bufferedSink;

var log = Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .Enrich.FromLogContext()
    .Buffer(
        // The level at which events are always sent (not buffered)
        LogEventLevel.Information,
        // The level at which all events are sent for the request
        LogEventLevel.Error,
        // The id to use to identify a request
        HttpRequestIdEnricher.HttpRequestIdPropertyName,
        // Request timeout in seconds (events are discarded after this time)
        4 * 60,
        // The buffered sink for use elsewhere
        out bufferedSink,
        // The event that is called when the events are triggered
        (lc) => {
            // Wire up your usual Sinks here
            lc.Enrich.FromLogContext()
                .WriteTo.ColoredConsole()
        }
    ).CreateLogger();

Log.Logger = log;

Registering the PerRequestLoggingModule (to support web request buffering)

You'll need to reference the namespaces Microsoft.Web.Infrastructure.DynamicModuleHelper and Serilog.Sinks.Buffered.Web

Autofac

private static void RegisterSerilog(IKernel kernel)
{
    IPerRequestLogger bufferedSink = SeriLogBootstrapper.RegisterSerilog();

    PerRequestLoggingModule.ResolvePerRequestLogger = () => bufferedSink;

    _builder.RegisterInstance(bufferedSink).As<IPerRequestLogger>();
    _builder.RegisterInstance(bufferedSink).As<IFlushPerRequestLogs>();

    DynamicModuleUtility.RegisterModule(typeof(PerRequestLoggingModule));
}

Ninject

private static void RegisterSerilog(IKernel kernel)
{
    var bufferedSink = SerilogBootstrapper.RegisterSerilog();

    PerRequestLoggingModule.ResolvePerRequestLogger = () => bufferedSink;

    kernel.Bind<IPerRequestLogger>().ToMethod(c => bufferedSink);
    kernel.Bind<IFlushPerRequestLogs>().ToMethod(c => bufferedSink);

    DynamicModuleUtility.RegisterModule(typeof(PerRequestLoggingModule));
}

About

A buffered sink for SeriLog

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 96.9%
  • Batchfile 3.1%