0

I have a question on using Enterprise Library Exception Handling Application Block.

First is the "ExceptionManager.Process" method. what I understood from the documentation is that you can execute your desired method and if it has an exception the ExceptionManager handles it, and it does not need any try-catch block. but when I use it it throws the exception.

builder.ConfigureExceptionHandling()
                .GivenPolicyWithName("Data Access Policy")
                .ForExceptionType<Exception>()
                .LogToCategory("General")
                .WithSeverity(System.Diagnostics.TraceEventType.Error)
                .UsingEventId(9000)
                .WrapWith<InvalidOperationException>()
                .UsingMessage("MyMessage").ThenDoNothing();
        #endregion

        var configSource = new DictionaryConfigurationSource();
        builder.UpdateConfigurationWithReplace(configSource);
        EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

        #endregion

        var exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
        exManager.Process(GenerateException, "Data Access Policy");
    }

    private static void GenerateException()
    {
        throw new NullReferenceException();
    }

I have used fluent-API for configuring application block. I want to log the exception to database and I have configured it well. The ExeptionManager.HandleException works well but it needs to be in an try-catch block.

how can I use "Process" method that handles exception without interruption and without a try-catch block ?

Randy Levy
  • 22,566
  • 4
  • 68
  • 94
Pouyan
  • 2,849
  • 8
  • 33
  • 39

1 Answers1

2

You need to configure the logging block. You didn't post the exception you were getting but if you check the InnerException property you will find the message:

The type LogWriter does not have an accessible constructor.

To log to a database using the Exception Handling Block you will need to configure the Exception Handling Block, the Logging Block, and the Data Access Block. In your case it would look something like this:

var builder = new ConfigurationSourceBuilder();

builder.ConfigureExceptionHandling()
    .GivenPolicyWithName("Data Access Policy")
    .ForExceptionType<Exception>()
    .LogToCategory("General")
    .WithSeverity(System.Diagnostics.TraceEventType.Error)
    .UsingEventId(9000)
    .WrapWith<InvalidOperationException>()
    .UsingMessage("MyMessage").ThenDoNothing();

builder.ConfigureLogging()
    .WithOptions
        .DoNotRevertImpersonation()
    .LogToCategoryNamed("General")
        .WithOptions.SetAsDefaultCategory()
        .SendTo.Database("Database Trace Listener")
        .UseDatabase("Logging")
        .WithAddCategoryStoredProcedure("AddCategory")
        .WithWriteLogStoredProcedure("WriteLog");

builder.ConfigureData()
    .ForDatabaseNamed("Logging")
    .ThatIs.ASqlDatabase()
    .WithConnectionString(@"Data Source=.\SQLEXPRESS;DataBase=LoggingDefault;Integrated Security=True;")
    .AsDefault();

var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);


var exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
exManager.Process(GenerateException, "Data Access Policy");

Also, note that you don't need the Wrap<> in the configuration since you are swallowing the exception and not throwing or rethrowing.

Randy Levy
  • 22,566
  • 4
  • 68
  • 94