The Exception/Logging Handling Application Block offers developers to implement strategy to process and handle exceptions at all architectural levels. Here we are not talking about how to extend the application block but, how do we add extra data properties to the exception object when handling/propagating exception.
We will walk through here with a sample code; it is assumed that you know how to configure the Microsoft Enterprise Library Exception Block. This is how our loggingConfigurtion settings look like:
1: <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
2: <listeners>
3: <add
4: name="Formatted EventLog TraceListener"
5: source="MySource"
6: formatter="Text Formatter"
7: log="LogSource"
8: machineName=""
9: traceOutputOptions="None"
10: listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
11: type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
12: <add
13: name="Email Trace Listener"
14: type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
15: listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
16: toAddress="to@company.com"
17: fromAddress="from@company.com"
18: subjectLineStarter="Subject"
19: subjectLineEnder="Hey - "
20: smtpServer="smtpServer"
21: formatter="Text Formatter" />
22: </listeners>
23: <formatters>
24: <add
25: name="Text Formatter"
26: template="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} )}"
27: type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
28: </formatters>
29: <categorySources>
30: <add switchValue="All" name="General">
31: <listeners>
32: <add name="Formatted EventLog TraceListener" />
33: <add name="Email Trace Listener" />
34: </listeners>
35: </add>
36: <add switchValue="Information" name="Information">
37: <listeners>
38: <add name="Formatted EventLog TraceListener" />
39: </listeners>
40: </add>
41: </categorySources>
42: <specialSources>
43: <allEvents switchValue="All" name="All Events" />
44: <notProcessed switchValue="All" name="Unprocessed Category" />
45: <errors switchValue="All" name="Logging Errors & Warnings">
46: <listeners>
47: <add name="Formatted EventLog TraceListener" />
48: </listeners>
49: </errors>
50: </specialSources>
51: </loggingConfiguration>
and sample code:
1: public void DoWork(string employeeId)
2: {
3: try
4: {
5: //Your implementation code goes here
6: }
7: catch (Exception ex)
8: {
9: Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex, "YOUR_EXCEPTION_POLICY_NAME");
10: }
11: }
now if an error occurs in the try block, the “HandleException” code under catch block will be executed and the exception information will be logged as per the format defined in the template key under “formatters”. This is how it would look like:
Timestamp: 9/28/2012 5:32:54 PM
Message: HandlingInstanceID: bc8f2339-42f4-4bd0-af1d-e33206e5478f
An exception of type ‘System.Exception’ occurred and was caught.
—————————————————————-
9/28/2012 13:32:54
Type : System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Exception message details goes here
Source : MySource
Help link :
Data :
Message: HandlingInstanceID: bc8f2339-42f4-4bd0-af1d-e33206e5478f
An exception of type ‘System.Exception’ occurred and was caught.
—————————————————————-
9/28/2012 13:32:54
Type : System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Exception message details goes here
Source : MySource
Help link :
Data :
Additional Info:
MachineName : MachineName
TimeStamp : 9/28/2012 5:32:54 PM
FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
AppDomainName : Host.vshost.exe
ThreadIdentity :
WindowsIdentity : domain\user
TimeStamp : 9/28/2012 5:32:54 PM
FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
AppDomainName : Host.vshost.exe
ThreadIdentity :
WindowsIdentity : domain\user
Category: General
Priority: 0
EventId: 100
Severity: Error
Title:Enterprise Library Exception Handling
Machine: MachineName
Application Domain: Host.exe
Process Id: 4072
Process Name: C:\Projects\Host.exe
Win32 Thread Id: 2320
Thread Name:
Extended Properties:
Priority: 0
EventId: 100
Severity: Error
Title:Enterprise Library Exception Handling
Machine: MachineName
Application Domain: Host.exe
Process Id: 4072
Process Name: C:\Projects\Host.exe
Win32 Thread Id: 2320
Thread Name:
Extended Properties:
So far so good we have handled and logged the exception as expected. Now lets say I need to log some extra information which would provide me some business value in identifying say “for what/whom has this exception occurred”. This is where we have to extend the data propertied when handling the exception.
If you look at the configuration file, under formatters section and check the value of “template” key, the one highlighted below Extended Properties: {dictionary({key} – {value} )} we will use this to log additional information. All it tells is that you can add N number of dictionary values to it.
template=”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} )}“
Here is how we will actually implement it in our code
1: public void DoWork(string employeeId)
2: {
3: try
4: {
5: //Your implementation code goes here
6: }
7: catch (Exception ex)
8: {
9: ex.Data.Add("Employee Id", employeeId);
10: Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex, "YOUR_EXCEPTION_POLICY_NAME");
11: }
12: }
If you see in the code we are taking advantage of Exception.Data property, here we have added Employee Id as Key-Value pair which will provide us more information as to for which Employee Id the exception occur. You can add N number of Key-Value pairs per your requirement. Now if any exception occurs this is how the exception looks:
Timestamp: 9/28/2012 5:32:54 PM
Message: HandlingInstanceID: bc8f2339-42f4-4bd0-af1d-e33206e5478f
An exception of type ‘System.Exception’ occurred and was caught.
—————————————————————-
9/28/2012 13:32:54
Type : System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Exception message details goes here
Source : MySource
Help link :
Data :
Message: HandlingInstanceID: bc8f2339-42f4-4bd0-af1d-e33206e5478f
An exception of type ‘System.Exception’ occurred and was caught.
—————————————————————-
9/28/2012 13:32:54
Type : System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Exception message details goes here
Source : MySource
Help link :
Data :
Additional Info:
MachineName : MachineName
TimeStamp : 9/28/2012 5:32:54 PM
FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
AppDomainName : Host.vshost.exe
ThreadIdentity :
WindowsIdentity : domain\user
TimeStamp : 9/28/2012 5:32:54 PM
FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
AppDomainName : Host.vshost.exe
ThreadIdentity :
WindowsIdentity : domain\user
Category: General
Priority: 0
EventId: 100
Severity: Error
Title:Enterprise Library Exception Handling
Machine: MachineName
Application Domain: Host.exe
Process Id: 4072
Process Name: C:\Projects\Host.exe
Win32 Thread Id: 2320
Thread Name:
Extended Properties: Employee Id – 0123456
Priority: 0
EventId: 100
Severity: Error
Title:Enterprise Library Exception Handling
Machine: MachineName
Application Domain: Host.exe
Process Id: 4072
Process Name: C:\Projects\Host.exe
Win32 Thread Id: 2320
Thread Name:
Extended Properties: Employee Id – 0123456
Now our objective is accomplished, we only have to make sure of 2 things here:
- Our formatters “template” value should have Extended Properties: {dictionary({key} – {value} )}
- Use Exception.Data property to log extra details.
This is really helpful when you want to add custom business related information.
No comments:
Post a Comment