Home All Groups Group Topic Archive Search About

Exception handling in Enterprise Library 2.0 newbee question

Author
30 Mar 2007 8:32 AM
fsatre
Hi all.

I am new to the Enterprise Library and having a problem with exception
handling in EntLib2.0.
My test application is a console application written in vb.net (.net
framework 2.0) se the code below.

The tracing works just fine but the TestException cases the Entlib2.0
to fail and my test programs goes into the catch where the comment
"'just to fetch any error while performing exception handling" is.
I have defined 2 exception policies the "Wrap Policy" and the "Log
Only Policy", both of them are resulting in the same error.

The exception message I get:
"The type
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=null from
configuration could not be created. (D:\...\MTest\bin\Debug
\MTest.vshost.exe.config line 161)"

Can anyone please help me?

Imports MTest.ExceptionAndTrace

Module Module1
    Sub Main()
        Try
            TestTrace()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        Finally
            Console.WriteLine("FINE")
            Console.ReadLine()
        End Try
    End Sub
    Private Sub TestTrace()
        Dim log As New ExceptionAndTrace
        log.StartTrace("Testing")
        'do some stuff and raise error
        TestException()
        log.StartTrace("End Test")
    End Sub
    Private Sub TestException()
        Try
            Throw New Exception("Dette er en feil")
        Catch ex As Exception
            Dim log As New ExceptionAndTrace
            log.ExceptionHandling(ex)
        End Try
    End Sub
End Module

Option Explicit On
Option Strict On
Option Compare Binary

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.Logging

Public Class ExceptionAndTrace

    Public Sub StartTrace(ByVal Message As String)
        If Logger.IsLoggingEnabled = True Then
            Dim logEntry As New LogEntry
            logEntry.Categories.Add("Trace")
            'defined in app.config/web.config
            logEntry.Message = Message
            Logger.Write(logEntry)
        End If
    End Sub
    Public Function ExceptionHandling(ByVal exInn As Exception) As
Boolean
        Try
            Return ExceptionPolicy.HandleException(exInn, "Log Only
Policy")
        Catch ex As Exception
            'just to fetch any error while performing exception
handling
            Console.Write(ex.ToString)
        End Try

    End Function
End Class
The web.config


<configSections>
<section name="exceptionHandling"

type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings,

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" />
    <section name="loggingConfiguration"

type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings,
                   Microsoft.Practices.EnterpriseLibrary.Logging" /
>
  </configSections>

<exceptionHandling>
    <exceptionPolicies>
      <add name="Wrap Policy">
        <exceptionTypes>
          <add name="Exception"
               type="System.Exception, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"
               postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Wrap Handler"
                   exceptionMessage="A unrecoverable error occurred in
Core Services"
                   wrapExceptionType="System.Exception, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"/>
              <add name="Logging Handler"
                   logCategory="Exception"
                   eventId="1000"
                   severity="Error"
                   title="EntLib2.0 Exception Handling"
                   priority="1"

formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"/>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      <add name="Log Only Policy">
        <exceptionTypes>
          <add name="Exception" type="System.Exception, mscorlib"
postHandlingAction="None">
            <exceptionHandlers>
              <add
                                logCategory="Exception"
                                eventId="100"
                                severity="Error"
                                title="EntLib2.0 Exception Handling"
                                priority="1"

formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                                name="Logging Handler"

type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging"
                            />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

  <loggingConfiguration defaultCategory="General"
tracingEnabled="false">
    <categorySources>
      <add name="General" switchValue="All">
        <listeners>
          <add name="Event Log Destination" />
        </listeners>
      </add>
      <add name="Exception" switchValue="All">
        <listeners>
          <add name="Flat File Destination Exception"/>
        </listeners>
      </add>
      <add name="Trace" switchValue="All">
        <listeners>
          <add name="Flat File Destination Trace"/>
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <errors name="errors" switchValue="All">
        <listeners>
          <add name="Event Log Destination"/>
        </listeners>
      </errors>
    </specialSources>
    <listeners>
      <add name="Flat File Destination Exception"

type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"

listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"
           formatter="Formatter Exception"
           fileName="D:\exp.log"/>
      <add name="Flat File Destination Trace"

type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"

listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"
           formatter="Formatter Trace"
           fileName="D:\trace.log"/>
      <add name="Event Log Destination"

type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"

listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"
           source="Enterprise Library Logging"
           formatter="Default Formatter"/>
    </listeners>
    <formatters>
      <add name="Default Formatter"

type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"
           template=" Timestamp: {timestamp}&#xA;
                      Message: {message}&#xA;
                      Category: {category}&#xA;
                      Priority: {priority}&#xA;
                      EventId: {eventid}&#xA;
                      Severity: {severity}&#xA;
                      Title:{title}&#xA;
                      Machine: {machine}&#xA;
                      Application Domain:
{appDomain}&#xA;
                      Process Id: {processId}&#xA;
                      Process Name:
{processName}&#xA;
                      Win32 Thread Id:
{win32ThreadId}&#xA;
                      Thread Name: {threadName}&#xA;
                      Extended Properties: {dictionary({key} -
{value})}"/>
      <add name="Formatter Exception"

type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"
           template=" Timestamp: {timestamp}&#xA;
                      Message: {message}&#xA;
                      Category: {category}&#xA;
                      Priority: {priority}&#xA;
                      EventId: {eventid}&#xA;
                      Severity: {severity}&#xA;
                      Title:{title}&#xA;
                      Machine: {machine}&#xA;
                      Application Domain:
{appDomain}&#xA;
                      Process Id: {processId}&#xA;
                      Process Name:
{processName}&#xA;
                      Win32 Thread Id:
{win32ThreadId}&#xA;
                      Thread Name: {threadName}&#xA;
                      Extended Properties: {dictionary({key} -
{value})}"/>
      <add name="Formatter Trace"

type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null"
           template=" Timestamp: {timestamp}&#xA;
                      Message: {message}&#xA;
                      Category: {category}&#xA;
                      Title:{title}&#xA;
                      Machine: {machine}&#xA;
                      Extended Properties: {dictionary({key} -
{value})}"/>
    </formatters>
  </loggingConfiguration>

AddThis Social Bookmark Button