Search code examples
c#log4netlog4net-configuration

how do I log throwable in Log4Net using XmlLayoutSchemaLog4j layout?


My Log4Net config looks like this:

  <appender name="MainLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="${log4net_log_file_name}"/>
    <param name="AppendToFile" value="true"/>
    <rollingStyle value="Size"/>
    <maxSizeRollBackups value="1"/>
    <maximumFileSize value="1MB"/>
    <staticLogFileName value="true"/>
    <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
      <locationInfo value="true" />
    </layout>
  </appender>

  <appender name="MemoryAppender" type="log4net.Appender.MemoryAppender" >
    <onlyFixPartialEventData value="true" />
  </appender>
</log4net>

We're logging exceptions like so:

 private readonly ILog _log = LogManager.GetLogger(typeof(LastChanceHandler));

        private void LogException(Exception ex)
        {
            _log.Fatal("Unhandled exception", ex);
        }

While we are logging errors I never see the Throwable node in the log. The current layout is used because we convert the XML to DTOs and display them in a UI.

I've not found anything to explain why Throwable is not being set and stored in the log. Any ideas(I'm considering making my dev box throwable. Out the window)?

Here's an error from my log:

<log4j:event logger="Hsbc.Ice.Shell.ViewModels.ShellViewModel" timestamp="1353076754456" level="ERROR" thread="1"><log4j:message>14 modules did not terminated in a timely manner : TradingCockpitModule,AuthenticationModule,ConfigurationModule,EntitlementsModule,Hsbc.Ice.CreditServices,EncryptionModule,ReportsModule,DiagnosticsModule,InterProcessCommunication,Layout,Logging,PersistenceModule,StorageModule,ICEExplorerManagerServiceModule</log4j:message><log4j:properties><log4j:data name="log4net:UserName" value="HBEU\steveget" /><log4j:data name="log4net:Identity" value="HBEU\steveget" /><log4j:data name="log4jmachinename" value="E8262XDZW4LZKEC" /><log4j:data name="log4japp" value="Hsbc.Ice.Shell.exe" /><log4j:data name="log4net:HostName" value="E8262XDZW4LZKEC" /></log4j:properties><log4j:locationInfo class="Hsbc.Ice.Shell.ViewModels.ShellViewModel" method="&lt;.ctor&gt;b__3" file="d:\CreditFlow\Tools\Hudson\data\jobs\Credit Shell\workspace\src\Shell\Hsbc.Ice.Shell\ViewModels\ShellViewModel.cs" line="107" /></log4j:event>

Solution

  • From the log4j documentation:

    throwable: Used to output the Throwable trace that has been bound to the LoggingEvent, by default this will output the full trace as one would normally find by a call to Throwable.printStackTrace().

    Throwable is a Java class which consequently isn't available in .Net: perhaps an exception stack trace could be substituted, but it hasn't been implemented.