{"id":16485443,"url":"https://github.com/zaid-ajaj/nancy.serilog","last_synced_at":"2025-10-27T18:31:11.962Z","repository":{"id":38008504,"uuid":"114053378","full_name":"Zaid-Ajaj/Nancy.Serilog","owner":"Zaid-Ajaj","description":"Nancy plugin for application-wide logging using Serilog","archived":false,"fork":false,"pushed_at":"2023-01-04T22:05:11.000Z","size":108,"stargazers_count":10,"open_issues_count":0,"forks_count":5,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-30T08:49:32.195Z","etag":null,"topics":["correlation","logging","nancy","seq","serilog"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Zaid-Ajaj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-13T00:23:42.000Z","updated_at":"2024-07-16T01:48:48.000Z","dependencies_parsed_at":"2023-02-02T21:30:48.824Z","dependency_job_id":null,"html_url":"https://github.com/Zaid-Ajaj/Nancy.Serilog","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zaid-Ajaj%2FNancy.Serilog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zaid-Ajaj%2FNancy.Serilog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zaid-Ajaj%2FNancy.Serilog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zaid-Ajaj%2FNancy.Serilog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Zaid-Ajaj","download_url":"https://codeload.github.com/Zaid-Ajaj/Nancy.Serilog/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238536175,"owners_count":19488669,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["correlation","logging","nancy","seq","serilog"],"created_at":"2024-10-11T13:25:55.294Z","updated_at":"2025-10-27T18:31:06.614Z","avatar_url":"https://github.com/Zaid-Ajaj.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nancy.Serilog [![Build Status](https://travis-ci.org/Zaid-Ajaj/Nancy.Serilog.svg?branch=master)](https://travis-ci.org/Zaid-Ajaj/Nancy.Serilog) [![Nuget](https://img.shields.io/nuget/v/Nancy.Serilog.svg?colorB=green)](https://www.nuget.org/packages/Nancy.Serilog)\n\n[Nancy](https://github.com/NancyFx/Nancy) plugin for application-wide logging using the [Serilog](https://github.com/serilog/serilog) logging framework.\n\n## Available Packages:\n\n| Package | Nancy  | Version |\n| --------- | ------------- | ------------- |\n| Nancy.Serilog | 2.0+ (dotnet core stable)  | [![Nuget](https://img.shields.io/nuget/v/Nancy.Serilog.svg?colorB=green)](https://www.nuget.org/packages/Nancy.Serilog) |\n\n## Getting Started\nInstall it from Nuget:\n```bash\n# Dotnet core\ndotnet add package Nancy.Serilog\n```\nEnable logging from your Bootstrapper at `ApplicationStartup`, this block is a good place to configure the actual logger. \n```cs\nusing Nancy.Serilog;\n// ...\nclass CustomBootstrapper : DefaultNancyBootstrapper\n{\n    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)\n    {\n        pipelines.EnableSerilog();\n\n        // Configure logger to output json-formatted logs to the console\n        // or use the sink of your choice \n        Log.Logger = new LoggerConfiguration()\n           .MinimumLevel.Information()\n           .WriteTo.Console(new JsonFormatter())\n           .CreateLogger()\n    }\n    \n    protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)\n    {\n        // Register request based dependency\n        container.Register((tinyIoc, namedParams) =\u003e context.GetContextualLogger());\n        \n        // other dependencies using ILogger should be registered here as well\n        container.Register\u003cIThirdPartyService, ThirdPartyService\u003e(); \n    }\n    \n    \n}\n```\nNow data from your requests will be logged when receiving requests and when returing responses:\n```cs\npublic class Index : NancyModule\n{\n    public Index()\n    {\n        Get(\"/\", args =\u003e \"Hello From Home\");\n    }\n}\n```\nWithout 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) with some ignored fields.\n\n![console](https://user-images.githubusercontent.com/13316248/33915081-af7128e2-dfa1-11e7-8d58-1dd6b191e86a.png)\n\n\n## Log Correlation\n Notice how the two logs are correlated with the same `RequestId` property. This property is attached to requests and responses so that you can find logs coming from a single roundtrip. When you write custom log messages, you want to include this `RequestId` property to your custom logs so that these too will be correlated to the same requests and responses. To do that, you want to use a logger that is *bound* to the request context: Nancy.Serilog provides an extension method called `GetContexualLogger()` you can register as the request container level from your `Bootstrapper` as in the following snippet.\n\n```csharp\nusing Nancy;\nusing Serilog;\n\npublic class Users : NancyModule\n{\n    // have ILogger as a dependency\n    public Users(ILogger logger)\n    {\n        Post(\"/hello/{user}\", args =\u003e\n        {\n            var user = (string)args.user;\n            logger.Information(\"{User} Logged In\", user);\n            return $\"Hello {user}\";\n        };\n    }\n}\n\npublic class Bootstrapper : DefaultNancyBootstrapper\n{\n    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)\n    {\n        pipelines.EnableSerilog();\n        StaticConfiguration.DisableErrorTraces = false;\n    }\n    \n    protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)\n    {\n        // Register request based dependency\n        container.Register((tinyIoc, namedParams) =\u003e context.GetContextualLogger());\n        \n        // other dependencies using ILogger should be registered here as well\n        container.Register\u003cIThirdPartyService, ThirdPartyService\u003e(); \n    }\n}\n```\n\nThen `POST`ing some data from Postman will give us the following (using [Seq](https://getseq.net/) to browse log data), see how the second log message also has `RequestId`:  \n\n![post](https://user-images.githubusercontent.com/13316248/33915879-287f96ac-dfa6-11e7-9d59-d176909f9a1f.png)\n\n## Ignoring Fields\nNancy.Serilog will try to retrieve all the information it can get from requests, responses and errors. However, you can still tell the library what fields to ignore *fluently* from the logs, it goes like this: \n```cs\nclass CustomBootstrapper : DefaultNancyBootstrapper\n{\n    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)\n    {\n        pipelines.EnableSerilog(new NancySerilogOptions\n        {\n            IgnoredResponseLogFields = \n                Ignore.FromResponse()\n                      .Field(res =\u003e res.RawResponseCookies)\n                      .Field(res =\u003e res.ReponseHeaders)\n                      .Field(res =\u003e res.ResponseCookies),\n            IgnoredRequestLogFields = \n                Ignore.FromRequest()\n                      .Field(req =\u003e req.Method)\n                      .Field(req =\u003e req.RequestHeaders),\n            IgnoredErrorLogFields = \n                Ignore.FromError() \n                      .Field(error =\u003e error.ResolvedRouteParameters)\n                      .Field(error =\u003e error.StatusCode)\n        });\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzaid-ajaj%2Fnancy.serilog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzaid-ajaj%2Fnancy.serilog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzaid-ajaj%2Fnancy.serilog/lists"}