{"action":"created","issue":{"url":"https://api.github.com/repos/serilog/serilog/issues/849","repository_url":"https://api.github.com/repos/serilog/serilog","labels_url":"https://api.github.com/repos/serilog/serilog/issues/849/labels{/name}","comments_url":"https://api.github.com/repos/serilog/serilog/issues/849/comments","events_url":"https://api.github.com/repos/serilog/serilog/issues/849/events","html_url":"https://github.com/serilog/serilog/issues/849","id":175131988,"number":849,"title":"Where is the best place to insert a middleware that adds users username to Seilog?","user":{"login":"gdoron","id":6141009,"avatar_url":"https://avatars.githubusercontent.com/u/6141009?v=3","gravatar_id":"","url":"https://api.github.com/users/gdoron","html_url":"https://github.com/gdoron","followers_url":"https://api.github.com/users/gdoron/followers","following_url":"https://api.github.com/users/gdoron/following{/other_user}","gists_url":"https://api.github.com/users/gdoron/gists{/gist_id}","starred_url":"https://api.github.com/users/gdoron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gdoron/subscriptions","organizations_url":"https://api.github.com/users/gdoron/orgs","repos_url":"https://api.github.com/users/gdoron/repos","events_url":"https://api.github.com/users/gdoron/events{/privacy}","received_events_url":"https://api.github.com/users/gdoron/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-09-05T22:14:50Z","updated_at":"2016-09-06T04:33:24Z","closed_at":null,"body":"Hi,\r\n\r\nI wrote a simple middleware that adds a username property to Serilog in ASP.NET core.\r\n\r\n\tpublic static class LoggerEnricher\r\n    {\r\n        public static IDisposable AddIdentityContext(string username)\r\n        {\r\n            return LogContext.PushProperty(\"username\", username);\r\n        }\r\n\r\n        public static void EnrichLogger(this IApplicationBuilder app)\r\n        {\r\n            app.UseMiddleware<EnrichSerilogContextMiddleware>();\r\n        }\r\n    }\r\n    \r\n        public class EnrichSerilogContextMiddleware\r\n    {\r\n        private readonly RequestDelegate _next;\r\n\r\n        public EnrichSerilogContextMiddleware(RequestDelegate next)\r\n        {\r\n            _next = next;\r\n        }\r\n\r\n        public async Task Invoke(HttpContext httpContext, ApplicationDbContext context)\r\n        {\r\n            var username = httpContext.User.FindFirst(ClaimTypes.Email)?.Value;\r\n            IDisposable property = null;\r\n            if (username != null)\r\n            {\r\n                property = LoggerEnricher.AddIdentityContext(username);\r\n            }\r\n            using (property)\r\n            {\r\n                await _next(httpContext);\r\n            }\r\n        }\r\n    }\r\n\r\nQuestion is, where is it the best place in the Startup class to add that middleware component?\r\n\r\nI added it here:\r\n\r\n    app.UseIdentity();\r\n    app.UseCookieAuthentication();\r\n    app.EnrichLogger();\r\n    app.UseMvc(routes => ...)\r\n\r\nBut there're many entries in the log without the property, for example:\r\n\r\n![image](https://cloud.githubusercontent.com/assets/6141009/18257611/3fc520dc-73cf-11e6-8a7c-8094103e012a.png)\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/serilog/serilog/issues/comments/244847573","html_url":"https://github.com/serilog/serilog/issues/849#issuecomment-244847573","issue_url":"https://api.github.com/repos/serilog/serilog/issues/849","id":244847573,"user":{"login":"gdoron","id":6141009,"avatar_url":"https://avatars.githubusercontent.com/u/6141009?v=3","gravatar_id":"","url":"https://api.github.com/users/gdoron","html_url":"https://github.com/gdoron","followers_url":"https://api.github.com/users/gdoron/followers","following_url":"https://api.github.com/users/gdoron/following{/other_user}","gists_url":"https://api.github.com/users/gdoron/gists{/gist_id}","starred_url":"https://api.github.com/users/gdoron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gdoron/subscriptions","organizations_url":"https://api.github.com/users/gdoron/orgs","repos_url":"https://api.github.com/users/gdoron/repos","events_url":"https://api.github.com/users/gdoron/events{/privacy}","received_events_url":"https://api.github.com/users/gdoron/received_events","type":"User","site_admin":false},"created_at":"2016-09-06T04:33:24Z","updated_at":"2016-09-06T04:33:24Z","body":"That's my question exactly... :) Where do I need to insert it?\r\nSince if I insert it before identity I can't get the username.\r\n\r\nI'll ask it on the asp.net repository and will let you know what they say.\r\n\r\nSorry for bothering you!\r\n\r\nBest,\r\nDoron"}}
{"actor":{"display_login":"gdoron"}}