{"id":1185,"date":"2017-07-09T10:10:34","date_gmt":"2017-07-09T10:10:34","guid":{"rendered":"http:\/\/tpodolak.com\/blog\/?p=1185"},"modified":"2017-07-09T10:10:34","modified_gmt":"2017-07-09T10:10:34","slug":"nlog-web-aspnetcore-make-sure-add-error-message-logging-exception","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2017\/07\/09\/nlog-web-aspnetcore-make-sure-add-error-message-logging-exception\/","title":{"rendered":"NLog.Web.AspNetCore &#8211; make sure you add error message for logging exception"},"content":{"rendered":"<p>I&#8217;ve been using <i>NLog<\/i> for quite some time already, and it was a natural choice to use it in the <i>ASP.NET Core<\/i> projects as well. I&#8217;ve configured the logger using <a href=\"https:\/\/github.com\/NLog\/NLog.Web\/wiki\/Getting-started-with-ASP.NET-Core-(csproj---vs2017)\">this<\/a> tutorial and everything seemed to be working fine. Unfortunately, after some time we&#8217;ve noticed that for some reasons some of the errors are not logged. We&#8217;ve taken a deeper look at our code base and we&#8217;ve found couple logs being written as follows<\/p>\n<pre lang=\"csharp\">\r\n_logger.LogError(exception.HResult, exception, string.Empty); \r\n_logger.LogCritical(exception.HResult, exception, string.Empty);\r\n<\/pre>\n<p>This piece of code works perfectly fine with <i>ASP.NET Core<\/i> console logger but it seems it doesn\u2019t for <i>NLog<\/i>.<br \/>\nWe double checked that with following piece of code<\/p>\n<pre lang=\"csharp\">\r\n[Route(\"api\/[controller]\")]\r\npublic class ValuesController : Controller\r\n{\r\n    private readonly ILogger<ValuesController> _logger;\r\n        \r\n    public ValuesController(ILogger<ValuesController> logger)\r\n    {\r\n        _logger = logger;\r\n    }\r\n\r\n    \/\/ GET api\/values\r\n    [HttpGet]\r\n    public IEnumerable<string> Get()\r\n    {\r\n        var exception = new Exception();\r\n        _logger.LogTrace(\"Log trace\");\r\n        _logger.LogDebug(\"Log debug\");\r\n        _logger.LogInformation(\"Log information\");\r\n        _logger.LogWarning(\"Log warning\");\r\n        _logger.LogError(exception.HResult, exception, string.Empty); \/\/ this one will not be logged\r\n        _logger.LogCritical(exception.HResult, exception, string.Empty); \/\/ this one will not be logged as well            \r\n\r\n        return new string[] {\"value1\", \"value2\"};\r\n    }\r\n}\r\n<\/pre>\n<p>And as you can see in the picture below, no errors have been reported<br \/>\n<a href=\"\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/07\/auto-draft\/NoErrorsLogged.png\"><img decoding=\"async\" src=\"\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/07\/auto-draft\/NoErrorsLogged.png\" alt=\"\" width=\"978\" class=\"aligncenter size-full wp-image-1187\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/07\/auto-draft\/NoErrorsLogged.png 978w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/07\/auto-draft\/NoErrorsLogged-150x38.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/07\/auto-draft\/NoErrorsLogged-300x76.png 300w\" sizes=\"(max-width: 978px) 100vw, 978px\" \/><\/a><br \/>\nChanging the error message to any message (even the one containing only white spaces)<\/p>\n<pre lang=\"csharp\">\r\n _logger.LogError(exception.HResult, exception, \" \"); \r\n _logger.LogCritical(exception.HResult, exception, \" \" ); \r\n<\/pre>\n<p>fixed the problem.<br \/>\n<a href=\"\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/07\/auto-draft\/ErrorsLoggedWithMessage.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/07\/auto-draft\/ErrorsLoggedWithMessage.png\" alt=\"\" width=\"978\" height=\"249\" class=\"aligncenter size-full wp-image-1186\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/07\/auto-draft\/ErrorsLoggedWithMessage.png 978w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/07\/auto-draft\/ErrorsLoggedWithMessage-150x38.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2017\/07\/auto-draft\/ErrorsLoggedWithMessage-300x76.png 300w\" sizes=\"auto, (max-width: 978px) 100vw, 978px\" \/><\/a><br \/>\nThat looks like a bug for me, so I will create a PR with the fix and see if that will make its way to the &#8220;master&#8221;<\/p>\n<p>As usual, the source code for this post can be found on my <a href=\"https:\/\/github.com\/tpodolak\/Blog\/tree\/master\/NLogWebAspNetCoreExceptionLoggingWithoutMessage\">GitHub<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been using NLog for quite some time already, and it was a natural choice to use it in the ASP.NET Core projects as well. I&#8217;ve configured the logger using this tutorial and everything seemed to be working fine. Unfortunately, after some time we&#8217;ve noticed that for some reasons some of the errors are not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[317,300],"tags":[297],"class_list":["post-1185","post","type-post","status-publish","format-standard","hentry","category-asp-net-core","category-nlog","tag-nlog"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/1185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=1185"}],"version-history":[{"count":3,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/1185\/revisions"}],"predecessor-version":[{"id":1190,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/1185\/revisions\/1190"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=1185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=1185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=1185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}