Common.Logging "void Error( object message, Exception exception );" not logging the "Exception"

427 Views Asked by At

I'm using Common.Logging with the NLog adapter.

http://netcommon.sourceforge.net/docs/1.2.0/reference/html/logging.html

I'm calling this method:

void Error( object message, Exception exception );

The text of the "message" shows up in the Logs (a txt-file and in the EventLog), but the details about the exception do not. Not even the ex.Message.

Am I missing something?

How do I get the details about the Exception to show up. Do I have to just tack it on the "object message"? I guess having the overload'ed method with the exception, I thought there would be some auto-logging.

My NLog.config is fairly simple.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="logfile" xsi:type="File" fileName="MyFile.log.txt"/>
    <target name="console" xsi:type="Console" />
    <target xsi:type="EventLog"
             name="eventLog" 
             source="MySource"
       eventId="555"
             log="Application"
                     />
  </targets>
  <rules>
    <logger name="*" minLevel="Error" writeTo="eventLog" />
    <logger name="*" minLevel="Info" writeTo="console" />
    <logger name="*" minLevel="Info" writeTo="logfile"/>
  </rules>

</nlog>

EDIT:

Ok...based on the Sergey answer, I just roided up my setup:

layout="${longdate}|${level}|${callsite}|${logger}|${threadid}|${windows-identity:domain=false}__${message} ${exception:format=message,stacktrace:separator=*"
1

There are 1 best solutions below

3
On BEST ANSWER

According to documentation, default layout is

${longdate}|${level:uppercase=true}|${logger}|${message}

You should specify ${exception} layout renderer to write exception info. E.g. set following layout for targets which should write exceptions:

${longdate}|${level:uppercase=true}|${logger}|${message}${newline}${exception}

If you want all targets to have same layout, you can move its definition to variable:

<variable name="verbose" 
          value="${longdate}|${level:uppercase=true}|${logger}|${message}${newline}${exception}" />
<targets>
   <target name="logfile" xsi:type="File" layout="${verbose}" fileName="log.txt" />
   <target name="console" xsi:type="Console" layout="${verbose}"/>
   <target name="eventLog" xsi:type="EventLog" layout="${verbose}"
           source="MySource" eventId="555" log="Application" />
</targets>
<rules>
   <logger name="*" minLevel="Error" writeTo="eventLog" />
   <logger name="*" minLevel="Info" writeTo="console,logfile" />
 </rules>