Created
July 28, 2017 23:14
-
-
Save phsantiago32/107b0a180ba928071d77eac76ad8d6c8 to your computer and use it in GitHub Desktop.
Nancy bootstrapper that sets request pipeline logging with and Serilog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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