Hi @all,
I 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.
I registered a global Exception handler on Startup:
public class Startup { private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); // ... public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); app.UseExceptionHandler( options => { options.Run( async context => { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; context.Response.ContentType = "text/html"; var ex = context.Features.Get<IExceptionHandlerFeature>(); if (ex != null) { var err = $"<h1>Error: {ex.Error.Message}</h1>{ex.Error.StackTrace }"; await context.Response.WriteAsync(err).ConfigureAwait(false); logger.Fatal(ex); } }); } ); } }
In Program I register NLog as logger:
public class Program { public static void Main(string[] args) { // NLog: setup the logger first to catch all errors var logger = LogManager.LoadConfiguration("nlog.config").GetCurrentClassLogger(); try { logger.Debug("init main"); BuildWebHost(args).Run(); } catch (Exception ex) { //NLog: catch setup errors logger.Error(ex, "Stopped program because of exception"); throw; } finally { // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux) NLog.LogManager.Shutdown(); } } public static IWebHost BuildWebHost(string[] args) { return WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseKestrel(options => { options.Listen(IPAddress.Any, 55555) .UseNLog() // NLog: setup NLog for Dependency injection .Build(); } }
My nlog.config:
<?xml version="1.0" encoding="utf-8" ?><nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="info" internalLogFile="c:\temp\internal-nlog.txt"><!-- the targets to write to --><targets><!-- write logs to file --><target xsi:type="File" name="allfile" fileName="QlcLog-All-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" /><!-- another file log, only own logs. Uses some ASP.NET core renderers --><target xsi:type="File" name="ownFile-web" fileName="QlcLog-Own-${shortdate}.log" 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}" /></targets><!-- rules to map from logger name to target --><rules><!--All logs, including from Microsoft--><logger name="*" minlevel="Error" writeTo="allfile" /><!--Skip non-critical Microsoft logs and so log only own logs--><logger name="Microsoft.*" maxLevel="Error" final="true" /><!-- Own logs --><logger name="*" minlevel="Warn" writeTo="ownFile-web" /></rules><extensions><add assembly="NLog.Web.AspNetCore"/></extensions></nlog>
When 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.
I speculate, that this might be caused by unmanaged code which I use indirectly (SQLite encryption lib).
Is there any way to get the stack trace?
Thanks in advance!