A few weeks back, I was trying to configure Enterprise Library 4.0 for Exception handling and Logging exception into event log but was not able to find any help on internet so it took me long time to do all the configurations for handling and logging exceptions using Enterprise. Library 4.0.
Follow following steps to configure Ent. Lib 4.0 Exception Handling and Logging blocks.
Note: web.config changes are required if you are using Enterprise Library with SharePoint for .Net application you can skip these steps.
a. Microsoft.Practices.EnterpriseLibrary.Common.dll b. Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll c. Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll d. Microsoft.Practices.EnterpriseLibrary.Logging.dll e. Microsoft.Practices.ObjectBuilder2.dll f. Microsoft.Practices.Unity.dll
a. <SafeControl Assembly="Microsoft.Practices.EnterpriseLibrary.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="Microsoft.Practices.EnterpriseLibrary.Common" TypeName="*" Safe="True" /> b. <SafeControl Assembly="Microsoft.Practices.ObjectBuilder2, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="Microsoft.Practices.ObjectBuilder2" TypeName="*" Safe="True" /> c. <SafeControl Assembly="Microsoft.Practices.Unity, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="Microsoft.Practices.Unity" TypeName="*" Safe="True" /> d. <SafeControl Assembly="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" TypeName="*" Safe="True" /> e. <SafeControl Assembly="Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="Microsoft.Practices.EnterpriseLibrary.Logging" TypeName="*" Safe="True" /> f. <SafeControl Assembly="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging" TypeName="*" Safe="True" />
Add following references and namespaces in your project
Note: Use Browse tab while adding reference and add above references from “<System Drive>:\Program Files\Microsoft Enterprise Library 4.0 - May 2008\Bin”
User following code snippet for exception handling:
try
{
//CODE
}
catch (Exception ex)
bool rethrow = ExceptionPolicy.HandleException(ex, "My Custom Exception Policy");
// Policy Name should be same as we configured in configuration step 7
if (rethrow)
throw; //Throw Exception if you need to show an error message to end user otherwise no need to throw exception.
finally
// Close/Dispose all objects
You are all done to get benefits of Enterprise Library 4.0 – Enjoy!!!!!
Hey Thanks for your input... i have followed above steps... looks i am getting follwing error.
The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, custompolicyname]) failed: Object reference not set to an instance of an object. (Strategy type ConfiguredObjectStrategy, index 2)
Make sure you are using same Exception Policy Name in following line of code as you set in Configuration Step 7
I am having the most terrible time getting MS Entity Lib logging to work. It works in our dev and local environments but not in production or staging. I read an article about how you have to create sources programatically to so that you can write to the event log. i tried that in one environment and it seemed to work for a few hours but now i am back to where i was. I used the Entity Lib configuration tool to create a simple log app which also worked but now does not. I feel like this whole set up should be a lot simpler and microsoft should have supplied much better doucmentation. If you have any suggestions I would much appreciate it. I am about ready to move on to some other tool.
where to add safecontrol assembly lines as shown in your code above in web.config file?
That code only requires if you are developing an application for SharePoint. If so then your application’s web.config should already have SafeControls tag in it. If not then following should be hierarchy in the web.config...
<configuration>
<SharePoint>
<SafeControls>
<!-- above SafeControl lines should go here -->
</SafeControls>
Hi,
Thanks for your info.
I would like to write a custom error component in SharePoint using Enterprise Library 4.1 (both exception handling & logging application block).
Do you have any points to that? Or any resources on how to implement the above,
Thanks
Chandrasekaran.