Logging Exceptions in Logging Application Block

Topics: Logging Application Block
Feb 3, 2010 at 6:59 AM
Edited Feb 3, 2010 at 7:00 AM

Hey, I would like to know that when the .NET framework throws an exception and it is caught and then I log its Message only (Exception.Message), the LAB also logs the call stack with it. But when I throw an exception (not the .NET framework) it only logs the Message and not the call stack. Can you tell me more about it? Can it be disabled/enabled.

Here is one of the log entries

===========
2/2/2010 09:28:41, D:\Projects\ABC\SourceCode\ABC\bin\Debug\ABC.exe, ABC.exe, 656, Critical, Exception, Type: System.IO.FileNotFoundException: Could not find file 'D:\Projects\ABC\SourceCode\ABC\bin\ABCs\XYZ\ABC.xml'.
File name: 'D:\Projects\ABC\SourceCode\ABC\bin\ABCs\XYZ\ABC.xml'
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
at System.Xml.XmlTextReaderImpl.OpenUrl()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.Load(String filename)
at ABC.ABC.ABC.Create(String path) in D:\Projects\ABC\SourceCode\ABCObjects\ABC.cs:line 281, Message: Could not find file 'D:\Projects\ABC\SourceCode\ABC\bin\ABCs\XYZ\ABC.xml'.
===========

 

But when I log the an exception which is thrown by me it looks like

==================
2/3/2010 7:27:42 AM, D:\Projects\ABC\SourceCode\ABC\bin\Release\ABC.exe, ABC.exe, 2020, Error, Failed to schedule ABC: XYZ
==================

Thanks.

Feb 3, 2010 at 7:07 AM

Are you logging in conjuction with EHAB or are you simply calling Logger.Write?  If it's the latter, which overload of the Write method are you using?

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 3, 2010 at 7:48 AM

No I'm only using the Logging Application Block. As for which method of Logger.Write, it is Logger.Write(object message, string category, int priority, int eventId, TraceEventType severity);

And in case there is need for the App.Config file, here is the loggingConfiguration section from the configuration file:

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="..\Logs\ABCObjects.log" footer="=================="
        formatter="ABC Text Formatter" header="=================="
        rollFileExistsBehavior="Increment" rollInterval="Midnight" rollSizeKB="20480"
        timeStampPattern="yyyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="ABCObjects RFF Trace Listener" />
      <add fileName="..\Logs\ABCService.log" footer="=================="
        formatter="ABC Text Formatter" header="=================="
        rollFileExistsBehavior="Increment" rollInterval="Midnight" rollSizeKB="20480"
        timeStampPattern="yyyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="ABCService RFF Trace Listener" />
      <add fileName="..\Logs\Other.log" footer="=================="
        formatter="ABC Text Formatter" header="=================="
        rollFileExistsBehavior="Increment" rollInterval="Midnight" rollSizeKB="20480"
        timeStampPattern="yyyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Other RFF Trace Listener" />
    </listeners>
    <formatters>
      <add template="{timestamp}, {appDomain}, {win32ThreadId}, {severity}, {message}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="ABC Text Formatter" />
    </formatters>
    <logFilters>
      <add enabled="true" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="LogEnabled Filter" />
    </logFilters>
    <categorySources>
      <add switchValue="All" name="ABCObjects">
        <listeners>
          <add name="ABCObjects RFF Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="ABCService">
        <listeners>
          <add name="ABCService RFF Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Other RFF Trace Listener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings" />
    </specialSources>
  </loggingConfiguration>

Feb 3, 2010 at 8:12 AM
Edited Feb 3, 2010 at 8:14 AM

I can't repro it on my end. Could you modify your template so that each part of the information is separated from the others?  Like for the {timeStamp}, place the word "Timestamp" before it: TimeStamp: {timeStamp} and so on.  I think the one you think is stack trace is actually part of the Message property.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 3, 2010 at 9:13 AM
AvanadeSupport wrote:

I can't repro it on my end. Could you modify your template so that each part of the information is separated from the others?  Like for the {timeStamp}, place the word "Timestamp" before it: TimeStamp: {timeStamp} and so on.  I think the one you think is stack trace is actually part of the Message property.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Okay, I'm also unable to reproduce it. The issue was with my code to which I made a change yesterday and then forgot. Actually in the previous code I wanted to log the type of the exception so I used Exception.ToString(), which includes the stack trace (Environment.StackTrace), so the stack trace was coming from there. But then I changed the code and the issue went away. So thanks for helping out.

Anyways is there an option in the LAB with which I can include the stack trace? A configurable such option, so I can enable/disable it when I want with re-compiling.

Feb 3, 2010 at 9:40 AM

You can do this:

There is an overload of the Write method which allows you to pass an IDictionary<string, object>.  If you want the stack trace to be included, create a Dictionary<string, object> and add a corresponding entry in there for the stack trace, e.g.

IDictionary<string, object> properties = new Dictionary<string, object>();

properties.Add("stackTrace", ex.StackTrace);

In your Formatter's Template property, you can then display it by using this token: {keyValue(stackTrace)} - where stackTrace is the key you used in the dictionary.

You also have the option to add those dictionary entries in a LogEntry's ExtendedProperties.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 4, 2010 at 6:38 AM
Edited Feb 4, 2010 at 7:24 AM

One more thing I would like to know is that how may I insert new lines (empty lines) in the header and footer property of the Trace Listeners, I tried entering '\r\n' without quotes but it printed \r\n and not empty lines.

 

Feb 4, 2010 at 7:55 AM

Hi,

Please add &#xD;&#xA; after the header. This take me a bit of time to figure out :) please see my sample:

      <add fileName="..\Logs\Other.log" footer="==================&#xD;&#xA;"
        formatter="ABC Text Formatter" header="==================&#xD;&#xA;"
        rollFileExistsBehavior="Increment" rollInterval="Midnight" rollSizeKB="20480"
        timeStampPattern="yyyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Other RFF Trace Listener" />

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com