Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nancy bootstrapper that sets request pipeline logging with and Serilog
using Nancy;
using Nancy.Bootstrapper;
using Nancy.TinyIoc;
using Serilog;
using System.Diagnostics;
namespace MyApp.Bootstrappers
{
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
base.ConfigureApplicationContainer(container);
}
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
{
var sw = new Stopwatch();
pipelines.BeforeRequest.AddItemToStartOfPipeline(ctx =>
{
if (ctx != null)
{
sw.Start();
}
return null;
});
pipelines.AfterRequest.AddItemToEndOfPipeline(ctx =>
{
if (ctx != null)
{
sw.Stop();
Log.Information("HTTP {RequestMethod} {RequestPath} from {IpAddress} responded {StatusCode} in {Elapsed:0.0000} ms", context.Request.Method, context.Request.Path, context.Request.UserHostAddress, (int)context.Response.StatusCode, sw.Elapsed.TotalMilliseconds);
}
});
base.RequestStartup(container, pipelines, context);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment