Skip to content

Instantly share code, notes, and snippets.

@phsantiago32
Created July 28, 2017 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phsantiago32/107b0a180ba928071d77eac76ad8d6c8 to your computer and use it in GitHub Desktop.
Save phsantiago32/107b0a180ba928071d77eac76ad8d6c8 to your computer and use it in GitHub Desktop.
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