![]() |
Web Development »
ASP.NET »
Howto
Intermediate
License: The Code Project Open License (CPOL)
Exception Handling & Logging Application Block: Enterprise Library 1.0By santosh poojariThis article illustrates how to log your Exception into trace.log file using Enterprise Library of .NET Framework. |
C++, C#, .NET, Win2K, WinXP, ASP.NET, Visual Studio, WebForms, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Here I am going to discuss the implementation of the Exception Application block of Enterprise Library 1.0., as I have gone through various sites but found there is no tutorial or any article which has highlights on this topic. Sometimes it may happen we have lots of modules having many layers in it. In that case if any exception is raised, we might not be able to find out from which aspx page or from which module or from which layer this exception has taken place. So here is the solution for this one, you can log the exception into a trace.log file which is a text file. This log file gives details of the exception. We can also trace this logging into a database too. I will cover this in the next article. So here we go.
Install Enterprise library 1.0.
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging;


Note: Exception Policy is a way to categorize the exception. One can have a number of exception types under Exception Policy.


LogCategory=trace, see figure.

TracingEnabled=true'.

DefaultCategory=trace. See figure for this.
Note: This setting enables tracing of exceptions into the Trace.log file.


Everything is set and done. Basically, we have set the configuration for Exception environment such as type of exception, category of exception, logging of exception and its various parameters as a whole.
Now interesting part of the application is invoking this exception layer into application layer. Here is the code-behind ...
Create a button on which I have raised "Divide by Zero Exception":
public void btnException_Click(object sender, System.EventArgs e)
{
try
{
int i=5;
int j=0;
int z=i/j;
}
catch(Exception ex)
{
bool rethrow =
ExceptionPolicy.HandleException(ex, "Business Layer Policy");
if (rethrow)
{
throw;
}
}
finally
{
//Code ---
}
}
}
If the required output is as given in the figure, then there is logging of Exception in Trace.log file as well as on the page. This is due to the setting of PostHandling=NotifyRethrow. If PostHandling=NotifyRethrow is set then we can have exception rethrown on to aspx page as well as on to the Trace.log file or any database. If PostHandling=None is set, then we cannot have exception rethrown on to the aspx page but can skip logging in 'LogCategory=general' or have logging of exception in LogCategory=trace mode.
LogCategory=general' no logging of exception.
LogCategory=Trace' logging of exception into log files or in database etc. Important: If there is Exception as 'Security' and no logging takes place, then right click on your project folder in inetpub/wwwroot/ExceptionconfigurationBlock, select Security and give full right privileges.
Generated output file Trace.log file content:
----------------------------------------
Timestamp: 4/25/2005 2:15:48 PM
Message: HandlingInstanceID: 3030db61-cccb-4b0b-8a3a-3661df5924db
An exception of type 'System.DivideByZeroException' occurred and was caught.
----------------------------------------------------------------------------
04/25/2005 14:15:48
Type : System.DivideByZeroException, mscorlib,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Attempted to divide by zero.
Source : ExceptionConfiguration block
Help link :
TargetSite : Void btnException_Click(System.Object, System.EventArgs)
Stack Trace :
at ExceptionConfiguration_block.WebForm1.btnException_Click(Object sender,
EventArgs e) in c:\inetpub\wwwroot\exceptionconfiguration
block\exceptionapplicationblock.aspx.cs:line 55
Additional Info:
MachineName : ITL232
TimeStamp : 4/25/2005 8:45:48 AM
FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
AppDomainName :
/LM/w3svc/1/root/ExceptionConfiguration block-10-127588725471560100
ThreadIdentity :
WindowsIdentity : ITL232\ASPNET
Category: Trace
Priority: 0
EventId: 100
Severity: Error
Title:Enterprise Library Exception Handling
Machine: ITL232
Application Domain:
/LM/w3svc/1/root/ExceptionConfiguration block-10-127588725471560100
Process Id: 1924
Process Name: C:\WINNT\Microsoft.NET\Framework\v1.1.4322\aspnet_wp.exe
Win32 Thread Id: 1932
Thread Name:
Extended Properties:
----------------------------------------
So here you get actual magic running for you.
I have already written an article on Configuration Application Block of Enterprise Library 1.0.
My objective was to make the learner aware of such a good technology on run. One can use these layers and make things easier and structured. Your suggestions, criticisms, advise are most welcome. Please do let me know how you find this article helpful.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 1 May 2005 Editor: Smitha Vijayan |
Copyright 2005 by santosh poojari Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |