{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/2437","repository_url":"https://api.github.com/repos/aspnet/KestrelHttpServer","labels_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/2437/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/2437/comments","events_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/2437/events","html_url":"https://github.com/aspnet/KestrelHttpServer/issues/2437","id":309254141,"number":2437,"title":"Hard crash which doesn't get logged","user":{"login":"StefanoD","id":1634862,"avatar_url":"https://avatars1.githubusercontent.com/u/1634862?v=4","gravatar_id":"","url":"https://api.github.com/users/StefanoD","html_url":"https://github.com/StefanoD","followers_url":"https://api.github.com/users/StefanoD/followers","following_url":"https://api.github.com/users/StefanoD/following{/other_user}","gists_url":"https://api.github.com/users/StefanoD/gists{/gist_id}","starred_url":"https://api.github.com/users/StefanoD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/StefanoD/subscriptions","organizations_url":"https://api.github.com/users/StefanoD/orgs","repos_url":"https://api.github.com/users/StefanoD/repos","events_url":"https://api.github.com/users/StefanoD/events{/privacy}","received_events_url":"https://api.github.com/users/StefanoD/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-03-28T07:41:48Z","updated_at":"2018-03-28T08:20:14Z","closed_at":null,"author_association":"NONE","body":"Hi @all,\r\n\r\nI have an ASP.net Core application which crashes only on release mode. Despite having installed and configured NLog, in this special case no log information gets written to a file.\r\n\r\nI registered a global Exception handler on Startup:\r\n```C#\r\npublic class Startup\r\n{\r\n\tprivate static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();\r\n\t\r\n\t// ...\r\n\t\r\n\tpublic void Configure(IApplicationBuilder app, IHostingEnvironment env)\r\n\t{\r\n\t\tif (env.IsDevelopment())\r\n\t\t{\r\n\t\t\tapp.UseDeveloperExceptionPage();\r\n\t\t}\r\n\r\n\t\tapp.UseMvc();\r\n\r\n\t\tapp.UseExceptionHandler(\r\n\t\t options => {\r\n\t\t\t options.Run(\r\n\t\t\t async context =>\r\n\t\t\t {\r\n\t\t\t\t context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;\r\n\t\t\t\t context.Response.ContentType = \"text/html\";\r\n\t\t\t\t var ex = context.Features.Get<IExceptionHandlerFeature>();\r\n\t\t\t\t if (ex != null)\r\n\t\t\t\t {\r\n\t\t\t\t\t var err = $\"<h1>Error: {ex.Error.Message}</h1>{ex.Error.StackTrace }\";\r\n\t\t\t\t\t await context.Response.WriteAsync(err).ConfigureAwait(false);\r\n\t\t\t\t\t logger.Fatal(ex);\r\n\t\t\t\t }\r\n\t\t\t });\r\n\t\t }\r\n\t\t);\r\n\t}\r\n}\r\n```\r\nIn Program I register NLog as logger:\r\n```C#\r\npublic class Program\r\n{\r\n\tpublic static void Main(string[] args)\r\n\t{\r\n\t\t// NLog: setup the logger first to catch all errors\r\n\t\tvar logger = LogManager.LoadConfiguration(\"nlog.config\").GetCurrentClassLogger();\r\n\t\ttry\r\n\t\t{\r\n\t\t\tlogger.Debug(\"init main\");\r\n\t\t\tBuildWebHost(args).Run();\r\n\t\t}\r\n\t\tcatch (Exception ex)\r\n\t\t{\r\n\t\t\t//NLog: catch setup errors\r\n\t\t\tlogger.Error(ex, \"Stopped program because of exception\");\r\n\t\t\tthrow;\r\n\t\t}\r\n\t\tfinally\r\n\t\t{\r\n\t\t\t// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)\r\n\t\t\tNLog.LogManager.Shutdown();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static IWebHost BuildWebHost(string[] args)\r\n\t{\r\n\t\treturn WebHost.CreateDefaultBuilder(args)\r\n\t\t\t.UseStartup<Startup>()\r\n\t\t\t.UseKestrel(options =>\r\n\t\t\t{\r\n\t\t\t\toptions.Listen(IPAddress.Any, 55555)\r\n\t\t\t.UseNLog()  // NLog: setup NLog for Dependency injection\r\n\t\t\t.Build();\r\n\t}\r\n}\r\n```\r\nMy nlog.config:\r\n```XML\r\n<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<nlog xmlns=\"http://www.nlog-project.org/schemas/NLog.xsd\"\r\n      xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n      autoReload=\"true\"\r\n      internalLogLevel=\"info\"\r\n      internalLogFile=\"c:\\temp\\internal-nlog.txt\">\r\n\r\n\r\n  <!-- the targets to write to -->\r\n  <targets>\r\n    <!-- write logs to file  -->\r\n    <target xsi:type=\"File\" name=\"allfile\" fileName=\"QlcLog-All-${shortdate}.log\"\r\n            layout=\"${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}\" />\r\n\r\n    <!-- another file log, only own logs. Uses some ASP.NET core renderers -->\r\n    <target xsi:type=\"File\" name=\"ownFile-web\" fileName=\"QlcLog-Own-${shortdate}.log\"\r\n            layout=\"${longdate}|${event-properties:item=EventId_Id}|${assembly-version}|${uppercase:${level}}|${logger}|${callsite}|${callsite-linenumber}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}\" />\r\n  </targets>\r\n\r\n  <!-- rules to map from logger name to target -->\r\n  <rules>\r\n    <!--All logs, including from Microsoft-->\r\n    <logger name=\"*\" minlevel=\"Error\" writeTo=\"allfile\" />\r\n\r\n    <!--Skip non-critical Microsoft logs and so log only own logs-->\r\n    <logger name=\"Microsoft.*\" maxLevel=\"Error\" final=\"true\" />\r\n    <!-- Own logs -->\r\n    <logger name=\"*\" minlevel=\"Warn\" writeTo=\"ownFile-web\" />\r\n  </rules>\r\n\r\n  <extensions>\r\n    <add assembly=\"NLog.Web.AspNetCore\"/>\r\n  </extensions>\r\n</nlog>\r\n```\r\nWhen an Exception occurs, normally I see them in the console and in the log file, but as I said: Sometimes I get a hard crash in release mode and nothing gets written.\r\n\r\nI speculate, that this might be caused by unmanaged code which I use indirectly (SQLite encryption lib).\r\nNever the less: I think that Kestrel shouldn't crash without logs. Is there any way to get the stack trace?\r\n\r\nThese are the relevant package versions which I use.\r\n```XML\r\n<PackageReference Include=\"Microsoft.AspNetCore.All\" Version=\"2.0.6\" />\r\n<PackageReference Include=\"Microsoft.AspNetCore.Server.Kestrel\" Version=\"2.0.2\" />\r\n<PackageReference Include=\"Microsoft.AspNetCore.Server.Kestrel.Https\" Version=\"2.0.2\" />\r\n```\r\n\r\nThanks in advance!"},"comment":{"url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/comments/376802333","html_url":"https://github.com/aspnet/KestrelHttpServer/issues/2437#issuecomment-376802333","issue_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/2437","id":376802333,"user":{"login":"davidfowl","id":95136,"avatar_url":"https://avatars3.githubusercontent.com/u/95136?v=4","gravatar_id":"","url":"https://api.github.com/users/davidfowl","html_url":"https://github.com/davidfowl","followers_url":"https://api.github.com/users/davidfowl/followers","following_url":"https://api.github.com/users/davidfowl/following{/other_user}","gists_url":"https://api.github.com/users/davidfowl/gists{/gist_id}","starred_url":"https://api.github.com/users/davidfowl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidfowl/subscriptions","organizations_url":"https://api.github.com/users/davidfowl/orgs","repos_url":"https://api.github.com/users/davidfowl/repos","events_url":"https://api.github.com/users/davidfowl/events{/privacy}","received_events_url":"https://api.github.com/users/davidfowl/received_events","type":"User","site_admin":false},"created_at":"2018-03-28T08:20:14Z","updated_at":"2018-03-28T08:20:14Z","author_association":"OWNER","body":"> Never the less: I think that Kestrel shouldn't crash without logs. Is there any way to get the stack trace?\r\n\r\nKestrel is a library in your application, it doesn't crash, your application is crashing. If you crash on a background thread (non request thread) there's nothing kestrel can do to save you.\r\n\r\nThe usual suspects are async void methods or fire and forget style operations being started from request threads then crashing."}}
{"actor":{"display_login":"davidfowl"}}