Formatting Exception logs

Topics: Exception Handling Application Block, Logging Application Block
Sep 4, 2009 at 5:14 AM

This is the log I have got written to a flat file using FlatFile TraceListener:

----------------------------------------
Timestamp: 9/4/2009 5:04:41 AM
Message: HandlingInstanceID: da8d933b-5f01-4349-a05b-051eb7cce011
An exception of type 'System.ArgumentException' occurred and was caught.
------------------------------------------------------------------------
09/04/2009 10:34:41
Type : System.ArgumentException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Parameter ex not found
Source : TestEL
Help link :
ParamName :
Data : System.Collections.ListDictionaryInternal
TargetSite : Void ExceptionRaiser()
Stack Trace :    at TestEL.ExceptionHandling.ExceptionRaiser() in D:\Web\TestEL\TestEL\ExceptionHandling.aspx.cs:line 44
   at TestEL.ExceptionHandling.btnRaiseExp_Click(Object sender, EventArgs e) in D:\Web\TestEL\TestEL\ExceptionHandling.aspx.cs:line 31

Additional Info:

MachineName : GEMT-SD-D065
TimeStamp : 9/4/2009 5:04:41 AM
FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
AppDomainName : 55f36c0a-1-128965142784200939
ThreadIdentity : GEMINI-INDIA\tony.m.thomas
WindowsIdentity : GEMINI-INDIA\tony.m.thomas

Category: Trace
Priority: 0
EventId: 100
Severity: Error
Title:Enterprise Library Exception Handling
Machine: GEMT-SD-D065
Application Domain: 55f36c0a-1-128965142784200939
Process Id: 2552
Process Name: C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe
Win32 Thread Id: 3848
Thread Name:
Extended Properties:
----------------------------------------

 

The template in Text Formatter is:

Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}

How can I remove 'Source','Help link','TargetSite' etc which are not there in the template?

Sep 4, 2009 at 6:54 AM

Hi,

You can achieve it by implementing your custom exception formatter.

Here is a sample which does not write Source, helplink and targetsite. This derives from TextExceptionFormatter.

    public class CustomExceptionFormatter : TextExceptionFormatter
    {
        public CustomExceptionFormatter(TextWriter writer, Exception ex, Guid handlingInstanceId) : base(writer, ex, handlingInstanceId) 
        {
            if (writer == null) throw new ArgumentNullException("writer");
        }

        protected override void WriteAdditionalInfo(System.Collections.Specialized.NameValueCollection additionalInformation)
        {
            base.WriteAdditionalInfo(additionalInformation);
        }

        protected override void WriteDateTime(DateTime utcNow)
        {
            base.WriteDateTime(utcNow);
        }

        protected override void WriteDescription()
        {
            base.WriteDescription();
        }

        protected override void WriteExceptionType(Type exceptionType)
        {
            base.WriteExceptionType(exceptionType);
        }

        protected override void WriteFieldInfo(System.Reflection.FieldInfo fieldInfo, object value)
        {
            base.WriteFieldInfo(fieldInfo, value);
        }

        protected override void WriteHelpLink(string helpLink)
        {
            //dont write help link
        }

        protected override void WriteMessage(string message)
        {
            base.WriteMessage(message);
        }

        protected override void WritePropertyInfo(System.Reflection.PropertyInfo propertyInfo, object value)
        {
            //write the property info if its not a TargetSite
            if (propertyInfo.PropertyType != typeof(MethodBase))
            {
                base.WritePropertyInfo(propertyInfo, value);
            }
        }

        protected override void WriteSource(string source)
        {
            //dont write source
        }

        protected override void WriteStackTrace(string stackTrace)
        {
            base.WriteStackTrace(stackTrace);
        }
    }

 

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

Sep 4, 2009 at 7:45 AM

Thanks for the reply Valiant

Could you tell me what do I need to do with the code you ve sent me? Should it be built as a seperate class library and use it in the Enterprise library Configuration tool?

Sep 4, 2009 at 7:54 AM

Yes. you can build it in a separate class library. Then when you configure a exception policy and added a logging handler, for the Formatter Type property of the logging handler, point it to the customexception formatter.

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

Sep 4, 2009 at 8:27 AM

Hello Valiant

I tried it out but this ExceptionHandlingException is occuring

The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, Global Policy]) failed: The type 'CustomExceptionFormatter.CustomExceptionFormatter, CustomExceptionFormatter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' cannot be resolved. Please verify the spelling is correct or that the full type name is provided. (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2)

Sep 4, 2009 at 8:46 AM

Hi,

If your class is in the a separate class library, The hosting app must have a reference to the class library. Try adding a reference to the class library.

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

Sep 4, 2009 at 9:38 AM

Now the exception has changed. Now it is 'Unable to handle exception: 'LoggingExceptionHandler'. Is there any section to be added in Web.Config? Below is the content in my web.config file:

 

<?xml version="1.0"?>

<configuration>

<configSections>

<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

<section name="exceptionPolicy" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionPolicyData, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl,Version=4.0.0.0" />

<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />

<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />

<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />

<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />

<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />

</sectionGroup>

</sectionGroup>

</sectionGroup>

</configSections>

<loggingConfiguration name="Logging Application Block" tracingEnabled="false"

defaultCategory="" logWarningsWhenNoCategoriesMatch="false">

<listeners>

<add fileName="D:\Web\TestEL\TestEL\ErrorLog.txt" header="----------------------------------------"

footer="----------------------------------------" formatter="Text Formatter"

listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

name="FlatFile TraceListener" />

</listeners>

<formatters>

<add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"

type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

name="Text Formatter" />

</formatters>

<categorySources>

<add switchValue="All" name="Trace">

<listeners>

<add name="FlatFile TraceListener" />

</listeners>

</add>

</categorySources>

<specialSources>

<allEvents switchValue="All" name="All Events" />

<notProcessed switchValue="All" name="Unprocessed Category" />

<errors switchValue="All" name="Logging Errors &amp; Warnings" />

</specialSources>

</loggingConfiguration>

<exceptionHandling>

<exceptionPolicies>

<add name="Global Policy">

<exceptionTypes>

<add type="System.Exception, mscorlib, Version=2.0.0.0" postHandlingAction="NotifyRethrow"

name="Exception">

<exceptionHandlers>

<add logCategory="Trace" eventId="100" severity="Error" title="Enterprise Library Exception Handling"

formatterType="CustomExceptionFormatter.CustomExceptionFormatter, CustomExceptionFormatter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

name="Test Logging Handler" />

</exceptionHandlers>

</add>

</exceptionTypes>

</add>

</exceptionPolicies>

</exceptionHandling>

<dataConfiguration defaultDatabase="ConnectAN" />

<connectionStrings>

<add name="ConnectAN" connectionString="Data Source=GSS-FS;Initial Catalog=RandD; UID=gem;PWD=gem"

providerName="System.Data.SqlClient" />

</connectionStrings>

<appSettings />

<system.web>

<!--

Set compilation debug="true" to insert debugging

symbols into the compiled page. Because this

affects performance, set this value to true only

during development.

-->

<compilation debug="true">

<assemblies>

<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />

<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />

<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />

</assemblies>

</compilation>

<!--

The <authentication> section enables configuration

of the security authentication mode used by

ASP.NET to identify an incoming user.

-->

<authentication mode="Windows" />

<!--

The <customErrors> section enables configuration

of what to do if/when an unhandled error occurs

during the execution of a request. Specifically,

it enables developers to configure html error pages

to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

<error statusCode="403" redirect="NoAccess.htm" />

<error statusCode="404" redirect="FileNotFound.htm" />

</customErrors>

-->

<pages>

<controls>

<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

</controls>

</pages>

<httpHandlers>

<remove verb="*" path="*.asmx" />

<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />

</httpHandlers>

<httpModules>

<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

</httpModules>

</system.web>

<system.codedom>

<compilers>

<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

<providerOption name="CompilerVersion" value="v3.5" />

<providerOption name="WarnAsError" value="false" />

</compiler>

</compilers>

</system.codedom>

<!--

The system.webServer section is required for running ASP.NET AJAX under Internet

Information Services 7.0. It is not necessary for previous version of IIS.

-->

<system.webServer>

<validation validateIntegratedModeConfiguration="false" />

<modules>

<remove name="ScriptModule" />

<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

</modules>

<handlers>

<remove name="WebServiceHandlerFactory-Integrated" />

<remove name="ScriptHandlerFactory" />

<remove name="ScriptHandlerFactoryAppServices" />

<remove name="ScriptResource" />

<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

</handlers>

</system.webServer>

<runtime>

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

<dependentAssembly>

<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35" />

<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0" />

</dependentAssembly>

<dependentAssembly>

<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35" />

<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0" />

</dependentAssembly>

</assemblyBinding>

</runtime>

</configuration>

Sep 4, 2009 at 10:10 AM

Hi,

Any details on 'Unable to handle exception: 'LoggingExceptionHandler'? if possible please include the inner exception. The config you've posted is just the same as mine. Also I can send you the sample application that I've created. Just send us a mail and we'll send you back the solution.