Click here to Skip to main content
Click here to Skip to main content

Exception Handling & Logging Application Block: Enterprise Library 1.0

By , 1 May 2005
 

Introduction

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.

Prerequisites:

Install Enterprise library 1.0.

Step By Step Implementation:

  1. Using Microsoft Visual Studio .NET, create an ASP.NET Web Application project in C# named as 'ExceptionConfigurationBlock'.
  2. Add reference to the project from Solution Explorer Window. Right click on project in Solution Explorer, click on Add Reference, browse through path c/Program File/Microsoft Enterprise Library/bin. Select Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll and then click OK.
  3. Now include these references in the webform 'ExceptionapplicationBlock.aspx.cs':
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling; 
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging;
  4. This is an important step. Once Enterprise Library is installed in your system, go to Program menu, click on ‘Microsoft Patterns and Practices’, there you will find ‘Enterprise library’. Under it open ‘Enterprise Library Configuration’.
  5. Once Enterprise Library configuration is open, click on File menu, select Open Application. Now you will see the file browser window. Browse your application through the path stated as inetpub/wwwroot/Exception Configuration Block and select Web.config file and click OK.
  6. Right click on Application menu, select New and click on Exception Handling application block. On doing this, there appears sub nodes menu section of Exception Handling application block.

    Sample Image

  7. Right click on 'Exception Handling Application Block', select New and click on Exception Policy. Once that is done, one can find that Exception Policy appears. Rename 'Exception Policy' by double clicking on it, to 'Business Layer Policy'.

    Sample Image

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

  8. Right click on 'Business Layer Policy', select New and click on Exception Type. A dialog box appears in which the list of exceptions is displayed. Select 'Exception' under System. Click OK button. See the following figure for this.

    Sample Image

  9. Right click on 'Exception', select New and click on Logging Handler.

    Sample Image

  10. Change the setting for Logging Handler on the right side. Make LogCategory=trace, see figure.

    Sample Image

  11. Now click on 'Client Setting', see on right-hand side that there appears an Attributes section, make 'TracingEnabled=true'.

    Sample Image

  12. Click on 'Distributed Settings', see Attribute settings, make DefaultCategory=trace. See figure for this.

    Note: This setting enables tracing of exceptions into the Trace.log file.

    Sample Image

  13. Settings for filename (Trace.log): one can change filename as well as its path for by specifying its path in 'Flat File Destination' node menu of Trace, listed under Distributed settings. See figure for this.

    Sample Image

  14. Save the file.
  15. On saving the Enterprise configuration file, go to the ASP.NET web project in Solution Explorer window, select 'All files' icon and include all configuration files.

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 ...

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 ---
    }
}
}

Key Scenarios:

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.

  1. 'LogCategory=general' no logging of exception.
  2. '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.

History

I have already written an article on Configuration Application Block of Enterprise Library 1.0.

Conclusion

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

santosh poojari
Technical Lead
India India
Member
He is presently working as tech arch in one of the leading IT company.He has total 10 years of experience in C#.net. He is a B.E graduate in Computers from Bombay University.
 
Most of his experiences are in designing architect for end to end solutions. His interest areas are WCF,Spring.net,Architecture- Model View Presenter,UML,Webservice,Performance Engineering/tuning,Design patterns,Generics,Enterprise Library,Regular expressions,Silverlight and WWF.
www.santoshpoojari.blogspot.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to configure and use Microsoft Enterprise Library 5 for loggingmemberLibish Varghese Jacob17 Jul '12 - 20:22 
GeneralAdd Stack Trace in trace.log file using Enterprise library 5.0memberArvind.Vijay12 May '11 - 22:18 
GeneralLog exception to databasememberjayagandhan1 Dec '10 - 18:57 
GeneralMy vote of 1membershivanshu_22@rediffmail.com17 Oct '10 - 20:22 
GeneralI am getting error message and not able to set LogCategory = TracememberNaresh B Pulipati2 Aug '10 - 8:20 
Generali want to avoid the original exception log informationmemberMember 470874212 Mar '09 - 19:54 
QuestionProcessName of no use in Web App?memberrpeters7430 Oct '07 - 3:09 
Generalsame in EL 2.0membermbmannu27 Nov '06 - 20:34 
GeneralUse CustomTracelistnermembermbmannu27 Nov '06 - 3:52 
Generalchange file namemembermbmannu27 Nov '06 - 3:46 
GeneralDAAB in Enterprise Library 2.0membervenaktzeus21 Nov '06 - 19:48 
QuestionThe type caught or thrown must be derived from System.Exception memberF. Atabert10 Aug '06 - 1:02 
AnswerRe: The type caught or thrown must be derived from System.Exception memberSr.R8 Feb '07 - 11:28 
GeneralRe: The type caught or thrown must be derived from System.Exceptionmemberdnk.nitro1 May '08 - 9:05 
GeneralWorked flawlessly, thanksmemberkannankeril30 Jul '06 - 4:01 
Generalhi mail query from catch blockmemberAnkit Aneja11 May '06 - 21:34 
QuestionNew file for every exceptionmembersam31528 Mar '06 - 10:06 
QuestionException Handling & Logging Application BlockmemberAshishBasran19 Jan '06 - 16:34 
AnswerRe: Exception Handling & Logging Application Blockmembertgrk2 Feb '06 - 2:16 
GeneralNot Logging exception into Flat Filememberagg_rohit29 Dec '05 - 14:31 
GeneralRe: Not Logging exception into Flat Filemembershweta2120 Jul '07 - 0:57 
GeneralYes, BUT: securitymemberJVMFX19 Oct '05 - 10:42 
GeneralRe: Yes, BUT: securitymembersantosh poojari19 Oct '05 - 18:22 
GeneralRe: Yes, BUT: securitymemberNKNRN10 Jul '06 - 5:30 
Generalit works !!memberbhanu12822 Sep '05 - 0:47 
GeneralRe: it works !!membersantosh poojari22 Sep '05 - 18:29 
GeneralRe: it works !!.......some mod neeed :Dmemberbhanu12822 Sep '05 - 22:28 
GeneralLog files to Oracle 8i databasememberBitoo18 Sep '05 - 23:10 
GeneralSecurity Exceptionmemberbri189b17 Sep '05 - 12:56 
GeneralLog files to SQLServer databasesussAbhilash MK19 Aug '05 - 0:43 
GeneralRe: Log files to SQLServer databasemembersantosh poojari19 Aug '05 - 2:50 
GeneralRe: Log files to SQLServer databasesussAbhilashMK21 Aug '05 - 22:51 
GeneralFacing ProblemmemberAustinn29 Jul '05 - 20:22 
GeneralThanks!membertlasuk19 Jul '05 - 8:12 
GeneralRe: Hey gr8membersantosh poojari19 Jul '05 - 20:00 
GeneralStoring Log Values in Sql DatabasememberMinorMitar9 Feb '06 - 17:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 2 May 2005
Article Copyright 2005 by santosh poojari
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid