|
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:
- Using Microsoft Visual Studio .NET, create an ASP.NET Web Application project in C# named as 'ExceptionConfigurationBlock'.
- 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.
- Now include these references in the webform 'ExceptionapplicationBlock.aspx.cs':
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging;
- 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’.
- 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.
- 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.

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

Note: Exception Policy is a way to categorize the exception. One can have a number of exception types under Exception Policy.
- 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.

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

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

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

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

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

- Save the file.
- 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
{
}
}
}
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.
- '
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.
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.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 30 (Total in Forum: 30) (Refresh) | FirstPrevNext |
|
 |
|
|
I have the same problem that I see in this example and that is that a web application will always show:
Process Name: C:\WINNT\Microsoft.NET\Framework\v1.1.4322\aspnet_wp.exe
Does anyone know of a formatter element or any way to get the actual ProcessName (the class name) of the object in use?
--
On a related note, I would like to be able to look through the log file and see a specific users or sessions "work path". I have an app that is logging to one file but I have no way to filter out the logs specific to one users or one sessions work process.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
May i use CustomTracelistner instead of FlatFileTraceListner. if possible may u guide me .
Mahendra
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
|
Hi, all is fine but i want to change file name for each and every Exception. so how can i trap file name and path from configuration and make new file name.
Mahendra
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi.. The post is excellent. Could you please provide similar step by step implementation for the Data Access Application Block in the Enterprise Library 2.0. It would be really appreciable.
|
| Sign In·View Thread·PermaLink | 2.00/5 (3 votes) |
|
|
|
 |
|
|
After following the tutorial and doing
throw ExceptionPolicy.HandleException(ex, "Business Layer Policy");
I get "The type caught or thrown must be derived from System.Exception". I can't see how I can do this since I can't modify the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy class. What can I do? Have I forgotten something?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I don't know if this is helpful at this time, but I think the problem is that ExceptionPolicy.HandleException(ex,"Business Layer Policy") returns a boolean and not an Exception, and you can't use throw with booleans. You get the same error if you write something like:
bool mybol = true; throw mybol;
you're trying to "throw a boolean value"
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
try { // Run code. } catch(Exception ex) { bool rethrow = ExceptionPolicy.HandleException(ex, " Data Access Policy"); if (rethrow) throw; }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I am new to ASP.NET and was recently introduced to DAAP & then the Enterprise Library.
I started using the Data Application Block almost immediately and was looking to learn how to use the other stuff included in the Ent. Lib'ry. This was an excellent start to learning about Exception Handling. Thanks.
Another mumbaiwalla & cricket lover.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
hi like log file is made if code enters in catch block is there something inbuilt also so that if code enters catch block a mail should also be sent to administrator in addtion of writing in a log file or will i have to send mail myself from it
Ankit Aneja "Nothing is impossible. The word itself says - I M possible"
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Great article. One question, In its current demo all exceptions get logged to the same file. Is it possible to configure it in such a way that each of the exeption gets written to its own file?
Thanks.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
I was trying to include Exception Handling & Logging Application Block for ASP.NET 2.0 web service project. Can anyone help me. It is giving me errors, though it the same configuration is working fine with 1.0 framework
- ashish
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hi Santosh, I have done exactly what you have told.My application runs fine but it is not logging error into FlatFile Error.log.
Please help me out.
Thanks, Rohit
|
| Sign In·View Thread·PermaLink | 2.33/5 (3 votes) |
|
|
|
 |
|
|
hi rohit
check enterprise library cofigurations check in all tabs that u have selected flat file logging or not.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Your article is a great little introduction, thanks! But, at the end of your article you have a quick little comment:
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.
Actually, I would not recommend this. You have sort of skimmed over a much bigger but very important topic of security. At a minimum I would change this to say something like "grant the ASPNET account on your server all rights except "Full Control".
More detail for anyone interested:
By default, an ASP.NET web application runs under a user account with very limited privileges on the machine. It is a local machine account called "ASPNET". Being a local account it has no access rights on any other parts of your network domain (if your PC is on a network), and it will also have no rights to access files on your disk except within your web application folder.
If you, like me, don't care to place the log file in the web application folder where PERHAPS some clever hacker could get access to it (doubtful, but why risk it?), you need to configure the file to be written to another path. Therefore you need to grant the ASPNET account read and write privileges on the folder where you want the file written. However, you don't necessarily want to grant ALL privileges. If you see a checkbox called "FULL CONTROL", that means that the ASPNET account would have enough rights to grant and revoke rights on that folder. Obviously that should not be needed for your application and only offers a potential security risk.
Since you are developing ASP.NET web applications, if any of the above seems confusing to you I would strongly recommend that you do some reading on microsoft.com and learn about file access security on your operating system before messing around with it.
--JV
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, Its really nice that someone talked about security issue in this article.Security is very crucial task one has to take care. you said right on this.Thats why in order to access the network files we have to include tags in web.confiq file <identity impersonate="true">.Which is why File.exist does not work for files on network.One has to set anonymous access user rights of iis with same username & password. Thanks ones again for contributing ur knowledge on this.
Happy Coding "San"
-- modified at 0:23 Thursday 20th October, 2005
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Followed above steps to log exception, I am able to log exception msg to trace.log file but i still receive this error, which looks to be while writing in event log file. For this i tried creating a entry in registry too with this command.
[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eventlog\Application\Enterprise Library Logging]
Error Message: StackTrace " at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) at System.Diagnostics.EventLog.FindSourceRegistration(String source, String machineName, Boolean readOnly) at System.Diagnostics.EventLog.SourceExists(String source, String machineName) at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) at System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) at System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type, Int32 eventID, Int16 category) at System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type, Int32 eventID) at System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type) at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionUtility.LogHandlingException(String policyName, Exception offendingException, Exception chainException, Exception originalException) at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry.ExecuteHandlerChain(Exception ex, Guid handlingInstanceID) at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry.Handle(Exception ex) at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception ex) at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception ex, String policyName, ConfigurationContext configurationContext) at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception ex, String policyName) at Inco.Crild.BLL.EquipmentTypes.UpdateEquipmentType(EquipmentType aEquipmentType) in D:\CrildNew\CrildBLL\EquipmentTypes.vb:line 90 at crild.MaintEquipmentTypes.dgrdEquipmentTypes_UpdateCommand(Object source, DataGridCommandEventArgs e) in D:\CrildNew\crild\UserControls\MaintEquipmentTypes.ascx.vb:line 86" String Comma, ')', or a valid expression continuation expected.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
it works !! how ever would it be better to declare a exception class and pass it rather than put (ex) stack trace into the log file. btw thanks for a great artical
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Np....any way i was wondering how to modify the message that get writen in to the log file so it would be more user friendly...is there an easy method...
or should i modify the EntLib logging ..if so where ?? Plz get back to me soon
thanks Maithree
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I want to know how can I write exception messages into Oracle 8i database table ? If possible can u send me an example. (ie. required changes in the config file, and how to call it from the *.cs files)
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Though my Exception handler shouldn't need to access the registery on the shared remote server I'm using it is because it keeps throwing a security exception. I have parsed those XML files for anything that would require registry access - the only thing it should do is send a simple email. Do you have any ideas?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I understood how to write into trace.log.But how can I write Seperate Trace.log files per day ? Also I want to know how can I write exception messages into SQLServer Database table ?
|
| Sign In·View Thread·PermaLink | 1.00/5 (4 votes) |
|
|
|
 |
|
|
Hi,
U can find Datasink option in it. As enterprise Library has provided stored procedure and .bat file to create table in sql server.Just run a bat file table for excetion logging gets created.Then run stored proc script .Just try it you will find the way.
Happy Coding "San"
|
| Sign In·View Thread·PermaLink | 2.67/5 (3 votes) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|