Enterprise Library 4.1 Exception Handling Not Working

Topics: Exception Handling Application Block, Logging Application Block
Oct 13, 2009 at 7:39 AM

<script type="text/javascript">// <![CDATA[ document.domain = "microsoft.com"; // ]]></script> Hi All,

I've created a very simple application to test the exception handling and logging application blocks for a thrown exception, the application works correctly on my PC(Windows 7), however when i move the binaries to the VM(windows server 2003 enterprise edition SP2) the application runs(does not give an indication that an error occured) but does not log the exception in the event viewer.

My application is a simple windows application with the below code for a button click:

private void btnThrow_Click(object sender, EventArgs e)
        {
            try
            {
                throw new Exception("Test Exception");
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex,"UI");
                if (rethrow)
                {
                    throw;
                }
            }
        }



I am using enterprise library 4.1 and the configuration file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add source="Enterprise Library Logging" formatter="Text Formatter"
        log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Formatted EventLog TraceListener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="UI">
        <exceptionTypes>
          <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="None" name="Exception">
            <exceptionHandlers>
              <add logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Logging Handler" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
</configuration>


i appreciate any help

thanks in advance

Oct 13, 2009 at 7:58 AM

You may not have permission to write to the event log, this is the default setting in Windows 2003.  Here's a link on how to configure event log security - http://support.microsoft.com/kb/323076

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Oct 13, 2009 at 10:24 AM

Thanks for your reply, i've checked it but it still not working

Besides, i am logged in as an administrator and using a windows application so i don't think it is a security issue. Also, i used my old way of logging using the EventLog class and it works fine and able to write to the event viewer.

Oct 13, 2009 at 11:06 AM

The "Enterprise Library Logging" source probably does not exists on your Windows 2003 machine. To check if it exists, try out this code:

bool exists = System.Diagnostics.EventLog.SourceExists("Enterprise Library Logging");

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Oct 13, 2009 at 11:40 AM

You are right, it is returning false. But, how can i solve this.

 

Oct 13, 2009 at 1:25 PM

Create it by executing this code:

System.Diagnostics.EventLog.CreateEventSource("Enterprise Library Logging", "Application");

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Oct 13, 2009 at 3:49 PM

Instead of creating the event log source manually, I recommend running installutil.exe over the required assemblies.
This will also register other things, such as performance counters.  On your development machine where you installed Ent Lib 4.1, see C:\Program Files\Microsoft Enterprise Library 4.1 - October 2008\InstallServices.bat

Note: to undo the install use 'installutil.exe /u'

installutil Microsoft.Practices.EnterpriseLibrary.Common.dll
installutil Microsoft.Practices.EnterpriseLibrary.Caching.dll
installutil Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography.dll
installutil Microsoft.Practices.EnterpriseLibrary.Caching.Database.dll
installutil Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll
installutil Microsoft.Practices.EnterpriseLibrary.Data.dll
installutil Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
installutil Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll
installutil Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.dll
installutil Microsoft.Practices.EnterpriseLibrary.Logging.dll
installutil Microsoft.Practices.EnterpriseLibrary.Logging.Database.dll
installutil Microsoft.Practices.EnterpriseLibrary.Security.dll
installutil Microsoft.Practices.EnterpriseLibrary.Security.Cache.CachingStore.dll
installutil Microsoft.Practices.EnterpriseLibrary.Validation.dll

 

 

Oct 14, 2009 at 6:08 AM

Thanks a lot for the help, i've run the batch file InstallServices.bat and the application is logging the exception successfully in the Application event log.

But there is still a problem, if i change the event log from Application to some none existing custom one like MyApplication the enterprise library is not creating it automatically and does not log the exception anywhere. I've tested it on my local machine(windows 7) and on the development machine(windows 2003)

 

Oct 14, 2009 at 7:22 AM

I think its your responsibility to create it. see this thread: http://entlib.codeplex.com/Thread/View.aspx?ThreadId=45365

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Oct 14, 2009 at 8:09 AM

Are you sure? because i've used the enterprise library before and it was creating the custom event log automatically.

Note that i am using the exception handling block with the logging application block, the above thread is only using the logging application block, i am not sure if that makes a difference

 

 

Thanks,

 

Oct 14, 2009 at 2:52 PM

Hi,

Yes you are right. It creates a custom source automatically. Just tried it. But my initial attempt fails, the reason is that, I just provided another non-existent log name and leave the Source property the value 'Enterprise Library Logging', which is already registered to the Application Log. I tried putting another log source, and see if it works. Also, add a listener to Logging Errors & Warnings, so that you can capture the exact exception in the failure.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Oct 15, 2009 at 5:35 AM

It worked perfectly, thanks.