68

When I use the default layout with NLog it only prints the name of the exception. I've been told that the log4jxmlevent layout doesn't prints nothing about the exception. What layout will help me?

Example code:

try
{
    throw new SystemException();
}
catch (Exception ex)
{
    logger.Error("oi", ex);
}

Default layout output:

2011-01-14 09:14:48.0343|ERROR|ConsoleApplication.Program|oi

log4jxmlevent output:

<log4j:event logger="ConsoleApplication.Program"
           level="ERROR"
           timestamp="1295003776872"
           thread="9">
<log4j:message>oi</log4j:message>
<log4j:NDC />
<log4j:locationInfo class="ConsoleApplication.Program"
                    method="Void Main(System.String[])"
                    file="C:\Users\User\Documents\Visual Studio 2010\Projects\ConsoleApplication\ConsoleApplication\Program.cs"
                    line="21" />
<nlog:eventSequenceNumber>3</nlog:eventSequenceNumber>
<nlog:locationInfo assembly="ConsoleApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<log4j:properties>
  <log4j:data name="log4japp"
              value="true" />
  <log4j:data name="log4jmachinename"
              value="MACHINE" />
</log4j:properties>

Jader Dias
  • 88,211
  • 155
  • 421
  • 625

4 Answers4

108

As documented in How to Log Exceptions, starting with NLog 4.0, pass the exception as the first parameter to Error, for example like this:

logger.Error(ex, "ex");

and a custom layout

layout="${exception:format=ToString}${newline}"
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • 4
    Is this in addition to an existing layout? – Shane Oct 07 '11 at 21:00
  • @Shane surely, unless you only need the stack trace – Jader Dias Sep 10 '12 at 14:11
  • 13
    FYI, this approach is deprecated. https://github.com/NLog/NLog/wiki/How-to-Log-Exceptions – Clay Dec 31 '14 at 21:44
  • 7
    Explaining the comment of @Clay: The logging line is now changed to `logger.Error("Got exception.", ex);` but the 'custom layout' is fine – Julian Jan 09 '15 at 15:41
  • 13
    Note that usage of both `ToString` and `StackTrace` cause stacktrace duplication in log output. `${exception:format=ToString}` would be enough – Ilya Serbis Aug 24 '16 at 15:58
  • 1
    @JaderDias this answer is very confusing. Can you please correct the answer or flag the comments for delete (as obsolete). – Jess Mar 15 '22 at 20:01
25

Use the overloads that take an Exception as the second argument:

catch(Exception crap)
{
    log.Error(crap, "Something went horribly wrong.");
}

Then in your layout include the ${exception} layout renderer:

<target ...
    layout="${longdate} ${message} ${exception:format=ToString}" />

Sources:

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
Clay
  • 10,885
  • 5
  • 47
  • 44
22

As documented in How to Log Exceptions, starting with NLog 4.0, pass the exception as the first parameter to Error, for example like this:

logger.Error(ex, "Nickers!");

In the NLog configuration (e.g. in web.config or app.config), include ${exception:format=tostring} in the layout, for example like this:

<target name="f" type="File" layout="${longdate} ${message} ${exception:format=tostring}"/> 
Edward Brey
  • 40,302
  • 20
  • 199
  • 253
  • 6
    Can use [Conditional Formatting](http://nlog-project.org/2011/04/20/exception-logging-enhancements.html) in the layout for exceptions. The following is close to the default layout ``. – mcdon Dec 18 '17 at 22:53
12

As of NLog 4.5 you can now use:

logger.Error(exception, message);

and layout as follows:

"${longdate} ${level} ${message} ${exception:format=@}"

The @ means serialize all Exception-properties into Json-format

Michael Armitage
  • 1,502
  • 19
  • 17
  • This is the answer I was looking for - gets full Json version of the Exception in the logs rather than just Exception type and message – Ryan Gaudion Aug 05 '21 at 13:12