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;
You'll need to reference the namespaces Microsoft.Web.Infrastructure.DynamicModuleHelper
and Serilog.Sinks.Buffered.Web
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));
}
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));
}