/Nancy.Serilog

Nancy plugin for application-wide logging using Serilog

Primary LanguageC#MIT LicenseMIT

Nancy.Serilog

Nancy plugin for application-wide logging using the Serilog logging framework.

This library is experiemental

Install it from Nuget:

Install-Package Nancy.Serilog

Enable logging from your Bootstrapper at ApplicationStartup, this block is a good place to configure actual logger.

using Nancy.Serilog;
// ...
class CustomBootstrapper : DefaultNancyBootstrapper
{
    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
        pipelines.EnableSerilog();

        Log.Logger = new LoggerConfiguration()
           .MinimumLevel.Information()
           .WriteTo.Console(new JsonFormatter())
           .CreateLogger()
    }
}

Now data from your requests will be logged when receiving requests and when returing responses:

public class Index : NancyModule
{
    public Index()
    {
        Get["/"] = args => "Hello Serilog";
    }
}

Without doing any extra configuration, navigating to / (root) will be logged: In the following screenshot I am using a self-hosted Nancy app (the sample of this repo).

console

Ofcourse, this is not human-readable because of console-json combo we are using as the sink. But Serilog has countless other sinks, one of which is Seq which I am starting to really like for local developement where you can browse log data easily:

seq

Also notice how the two logs are correlated using RequestId property. This is how you find logs coming from a single request. To enable this correlation, you cannot use the globally shared ILogger (i.e. from Log.Something) because you want to use a logger bound to the request context: Nancy.Serilog provides an extension method CreateLogger() you can call from your NancyModule like this:

Post["/hello/{user}"] = args =>
{
    var logger = this.CreateLogger();
    var user = (string)args.user;
    logger.Information("{User} Logged In", user);
    return $"Hello {user}";
};

Then POSTing some data from Postman will give us the following:

post