Search code examples
.netprojectenterprise-libraryclass-library

Enterprise library 5.0 in a c# class library project


I have a c# class library project in VS 2010 which outputs a dll. I added a executable project in my solution that I need to start with so I can debug my dll. I don't have a host application which has the app.config. Can I use Enterprise Libray (especially exception handling) in my class library project? If yes, how can I define the Exception Handling Policies since there is no app.config?

Thanks!


Solution

  • You can use the Fluent Configuration API with enterprise library 5

    With respects to Exception Handling. (from msdn)

    var builder = new ConfigurationSourceBuilder();
    
    builder.ConfigureExceptionHandling()
           .GivenPolicyWithName("MyPolicy")
           .ForExceptionType<NullReferenceException>()
             .LogToCategory("General")
               .WithSeverity(System.Diagnostics.TraceEventType.Warning)
               .UsingEventId(9000)
             .WrapWith<InvalidOperationException>()
               .UsingMessage("MyMessage")
             .ThenNotifyRethrow();
    
    var configSource = new DictionaryConfigurationSource();
    builder.UpdateConfigurationWithReplace(configSource);
    EnterpriseLibraryContainer.Current 
      = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);