Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog logs Error twice in same file

Tags:

nlog

I am trying to figure out why NLog logs my error twice.

here is my config file:

<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="debug" xsi:type="File" FileName="DebugTestFile.log" layout="${message} ${exception:format=tostring}" />
  </targets>
  <rules>
    <logger name="*" level="Debug" writeTo="debug" />
  </rules>
</nlog>

Now when I call NLog.Debug(myException); the output is my exception gets printed twice.

If I call NLog.Debug(myException, "My Test Message String"); then the error gets printed out once along with my test message.

What exactly am I doing wrong? According to https://github.com/NLog/NLog/wiki/How-to-Log-Exceptions my configuration is correct.

I tried changing my layout to the following: layout="${message} and when I ran NLog.Debug(myException); it only printed the error once.

However now when I ran NLog.Debug(myException, "My Test Message String"); it only printed my message without the error that comes from exception.

Could this be a bug with NLog?

like image 822
Bagzli Avatar asked Nov 09 '15 16:11

Bagzli


1 Answers

If you are using NLog in AspNetCore, then the problem can be caused by calling UseNLog together with AddNLog or ConfigureNLog.

You should only use UseNLog together with NLog.Web.LogBuilder.ConfigureNLog (Stop using AddNLog):

https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-2#4-update-programcs

like image 151
Rolf Kristensen Avatar answered Sep 30 '22 18:09

Rolf Kristensen