private void ConfigureExceptionHandling() { var configurationSource = new DictionaryConfigurationSource(); var builder = new ConfigurationSourceBuilder(); builder .ConfigureExceptionHandling() .GivenPolicyWithName(ValidationPolicy) .ForExceptionType<DeleteConstraintException>() .HandleCustom<BusinessValidationHandler>() .ThenThrowNewException() .ForExceptionType<BusinessValidationException>() .ThenNotifyRethrow() .ForExceptionType<BusinessException>() .HandleCustom<BusinessValidationHandler>() .ThenThrowNewException() .GivenPolicyWithName(DefaultPolicy) .ForExceptionType<Exception>() .LogToCategory("General") .WithSeverity(TraceEventType.Critical) .UsingExceptionFormatter<TextExceptionFormatter>() .WrapWith<Exception>() .UsingMessage("An error has occurred while processing request. Please contact your administrator for more information. [Error ID: {handlingInstanceID}]") .ThenThrowNewException(); builder.UpdateConfigurationWithReplace(configurationSource); var configurator = new UnityContainerConfigurator(this.Container); EnterpriseLibraryContainer.ConfigureContainer(configurator, configurationSource); }
protected override void Arrange() { base.Arrange(); Source = new DictionaryConfigurationSource(); ConfigurationSourceBuilder sourceBuilder = new ConfigurationSourceBuilder(); sourceBuilder.ConfigureExceptionHandling() .GivenPolicyWithName("SomePolicy") .ForExceptionType<Exception>() .ReplaceWith<ApplicationException>() .WrapWith<ArgumentException>() .ThenThrowNewException() .ForExceptionType<ArithmeticException>() .LogToCategory("ArithmicExceptions") .ThenDoNothing() .GivenPolicyWithName("Global Policy") .ForExceptionType<Exception>() .ReplaceWith<ApplicationException>() .UsingMessage("replacement message") .WrapWith<Exception>() .ThenNotifyRethrow() .ForExceptionType<InvalidCastException>() .WrapWith<ApplicationException>() .WrapWith<Exception>() .UsingMessage("yes, thats a known bug") .ThenThrowNewException(); sourceBuilder.UpdateConfigurationWithReplace(Source); Section = (ExceptionHandlingSettings)Source.GetSection(ExceptionHandlingSettings.SectionName); }
protected override void Arrange() { base.Arrange(); var builder = new ConfigurationSourceBuilder(); builder.ConfigureLogging() .LogToCategoryNamed("General") .SendTo.EventLog("Event Log Listener") .FormatWith(new FormatterBuilder().TextFormatterNamed("Text Formatter")) .ToLog("Application"); builder.ConfigureExceptionHandling() .GivenPolicyWithName("AllExceptions") .ForExceptionType<Exception>() .LogToCategory("General") .ThenDoNothing() .GivenPolicyWithName("OtherExceptions") .ForExceptionType<ArgumentNullException>() .LogToCategory("InvalidCategoryName") .ThenDoNothing(); var configuration = new DictionaryConfigurationSource(); builder.UpdateConfigurationWithReplace(configuration); LoggingSection = GetSection(LoggingSettings.SectionName, configuration); ExceptionHandlingSection = GetSection(ExceptionHandlingSettings.SectionName, configuration); }
protected override void Arrange() { base.Arrange(); validator = new LogFormatterValidator(); errors = new List<ValidationResult>(); IConfigurationSource source = new DictionaryConfigurationSource(); ConfigurationSourceBuilder sourceBuiler = new ConfigurationSourceBuilder(); sourceBuiler.ConfigureExceptionHandling() .GivenPolicyWithName("policy") .ForExceptionType<Exception>() .LogToCategory("category") .UsingExceptionFormatter<BadImageFormatException>(); sourceBuiler.UpdateConfigurationWithReplace(source); ExceptionHandlingSettings EhabSettings = (ExceptionHandlingSettings)source.GetSection(ExceptionHandlingSettings.SectionName); var sectionModel = SectionViewModel.CreateSection(Container, "Ehab Section", EhabSettings); properties = sectionModel.GetDescendentsOfType<LoggingExceptionHandlerData>().First().Properties; }
protected override void Arrange() { base.Arrange(); validator = new LogFormatterValidator(); errors = new List <ValidationResult>(); IConfigurationSource source = new DictionaryConfigurationSource(); ConfigurationSourceBuilder sourceBuiler = new ConfigurationSourceBuilder(); sourceBuiler.ConfigureExceptionHandling() .GivenPolicyWithName("policy") .ForExceptionType <Exception>() .LogToCategory("category") .UsingExceptionFormatter <BadImageFormatException>(); sourceBuiler.UpdateConfigurationWithReplace(source); ExceptionHandlingSettings EhabSettings = (ExceptionHandlingSettings)source.GetSection(ExceptionHandlingSettings.SectionName); var sectionModel = SectionViewModel.CreateSection(Container, "Ehab Section", EhabSettings); properties = sectionModel.GetDescendentsOfType <LoggingExceptionHandlerData>().First().Properties; }
protected override void Arrange() { base.Arrange(); File.Delete(MainConfigurationFile); File.Delete(SatelliteConfigurationSourcePath); var builder = new ConfigurationSourceBuilder(); var configurationSettings = new ConfigurationSourceSection(); configurationSettings.SelectedSource = "System Configuration"; configurationSettings.Sources.Add(new FileConfigurationSourceElement(SatelliteSourceName, SatelliteConfigurationSourcePath)); configurationSettings.Sources.Add(new SystemConfigurationSourceElement("System Configuration")); builder.AddSection(ConfigurationSourceSection.SectionName, configurationSettings); builder.ConfigureExceptionHandling() .GivenPolicyWithName("policy") .ForExceptionType <Exception>(); var source = new DesignDictionaryConfigurationSource(); builder.UpdateConfigurationWithReplace(source); ConfigurationSource = Container.Resolve <ConfigurationSourceModel>(); ConfigurationSource.Load(source); }
protected override void Arrange() { base.Arrange(); File.Delete(MainConfigurationFile); File.Delete(SatelliteConfigurationSourcePath); var builder = new ConfigurationSourceBuilder(); var configurationSettings = new ConfigurationSourceSection(); configurationSettings.SelectedSource = "System Configuration"; configurationSettings.Sources.Add(new FileConfigurationSourceElement(SatelliteSourceName, SatelliteConfigurationSourcePath)); configurationSettings.Sources.Add(new SystemConfigurationSourceElement("System Configuration")); builder.AddSection(ConfigurationSourceSection.SectionName, configurationSettings); builder.ConfigureExceptionHandling() .GivenPolicyWithName("policy") .ForExceptionType<Exception>(); var source = new DesignDictionaryConfigurationSource(); builder.UpdateConfigurationWithReplace(source); ConfigurationSource = Container.Resolve<ConfigurationSourceModel>(); ConfigurationSource.Load(source); }
private void ConfigureExceptionHandling() { var configurationSource = new DictionaryConfigurationSource(); var builder = new ConfigurationSourceBuilder(); builder .ConfigureExceptionHandling() .GivenPolicyWithName(DefaultPolicy) .ForExceptionType<DeleteConstraintException>() .HandleCustom<HttpErrorExceptionHandler>() .ThenThrowNewException() .ForExceptionType<BusinessValidationException>() .HandleCustom<BusinessValidationExceptionHandler>() .ThenThrowNewException() .ForExceptionType<BusinessException>() .HandleCustom<HttpErrorExceptionHandler>() .ThenThrowNewException() .ForExceptionType<Exception>() .LogToCategory("General") .WithSeverity(TraceEventType.Critical) .UsingExceptionFormatter<TextExceptionFormatter>() .HandleCustom( typeof(HttpErrorExceptionHandler), new NameValueCollection { { HttpErrorExceptionHandler.StatusCodeKey, HttpStatusCode.InternalServerError.ToString("G") }, { HttpErrorExceptionHandler.MessageKey, "An error has occurred while consuming this service. Please contact your administrator for more information." }, { HttpErrorExceptionHandler.AppendHandlingIdKey, bool.TrueString } }) .ThenThrowNewException(); builder.UpdateConfigurationWithReplace(configurationSource); var configurator = new UnityContainerConfigurator(this.Container); EnterpriseLibraryContainer.ConfigureContainer(configurator, configurationSource); }
protected override void Arrange() { base.Arrange(); Source = new DictionaryConfigurationSource(); ConfigurationSourceBuilder sourceBuilder = new ConfigurationSourceBuilder(); sourceBuilder.ConfigureExceptionHandling() .GivenPolicyWithName("SomePolicy") .ForExceptionType <Exception>() .ReplaceWith <ApplicationException>() .WrapWith <ArgumentException>() .ThenThrowNewException() .ForExceptionType <ArithmeticException>() .LogToCategory("ArithmicExceptions") .ThenDoNothing() .GivenPolicyWithName("Global Policy") .ForExceptionType <Exception>() .ReplaceWith <ApplicationException>() .UsingMessage("replacement message") .WrapWith <Exception>() .ThenNotifyRethrow() .ForExceptionType <InvalidCastException>() .WrapWith <ApplicationException>() .WrapWith <Exception>() .UsingMessage("yes, thats a known bug") .ThenThrowNewException(); sourceBuilder.UpdateConfigurationWithReplace(Source); Section = (ExceptionHandlingSettings)Source.GetSection(ExceptionHandlingSettings.SectionName); }
protected override void Arrange() { base.Arrange(); var builder = new ConfigurationSourceBuilder(); builder.ConfigureLogging() .LogToCategoryNamed("General") .SendTo.EventLog("Event Log Listener") .FormatWith(new FormatterBuilder().TextFormatterNamed("Text Formatter")) .ToLog("Application"); builder.ConfigureExceptionHandling() .GivenPolicyWithName("AllExceptions") .ForExceptionType <Exception>() .LogToCategory("General") .ThenDoNothing() .GivenPolicyWithName("OtherExceptions") .ForExceptionType <ArgumentNullException>() .LogToCategory("InvalidCategoryName") .ThenDoNothing(); var configuration = new DictionaryConfigurationSource(); builder.UpdateConfigurationWithReplace(configuration); LoggingSection = GetSection(LoggingSettings.SectionName, configuration); ExceptionHandlingSection = GetSection(ExceptionHandlingSettings.SectionName, configuration); }
private void ConfigureExceptionHandling() { var configurationSource = new DictionaryConfigurationSource(); var builder = new ConfigurationSourceBuilder(); builder .ConfigureExceptionHandling() .GivenPolicyWithName(ValidationPolicy) .ForExceptionType <DeleteConstraintException>() .HandleCustom <BusinessValidationHandler>() .ThenThrowNewException() .ForExceptionType <BusinessValidationException>() .ThenNotifyRethrow() .ForExceptionType <BusinessException>() .HandleCustom <BusinessValidationHandler>() .ThenThrowNewException() .GivenPolicyWithName(DefaultPolicy) .ForExceptionType <Exception>() .LogToCategory("General") .WithSeverity(TraceEventType.Critical) .UsingExceptionFormatter <TextExceptionFormatter>() .WrapWith <Exception>() .UsingMessage("An error has occurred while processing request. Please contact your administrator for more information. [Error ID: {handlingInstanceID}]") .ThenThrowNewException(); builder.UpdateConfigurationWithReplace(configurationSource); var configurator = new UnityContainerConfigurator(this.Container); EnterpriseLibraryContainer.ConfigureContainer(configurator, configurationSource); }
public void BuildsWcfExceptionShildingWithFluentConfiguration() { DictionaryConfigurationSource emptyConfigSource = new DictionaryConfigurationSource(); ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder(); builder.ConfigureExceptionHandling() .GivenPolicyWithName("policy") .ForExceptionType(typeof(NotFiniteNumberException)) .ShieldExceptionForWcf(typeof(MockFaultContract), "fault message") .MapProperty("Message", "{Message}") .MapProperty("Data", "{Data}") .MapProperty("SomeNumber", "{OffendingNumber}") .ThenThrowNewException(); builder.UpdateConfigurationWithReplace(emptyConfigSource); ExceptionHandlingSettings settings = emptyConfigSource.GetSection(ExceptionHandlingSettings.SectionName) as ExceptionHandlingSettings; Assert.IsNotNull(settings); Assert.AreEqual(1, settings.ExceptionPolicies.Count); var policy = settings.ExceptionPolicies.Get("policy"); Assert.IsNotNull(policy); Assert.AreEqual(1, policy.ExceptionTypes.Count); var configuredException = policy.ExceptionTypes.Get(0); Assert.AreEqual(typeof(NotFiniteNumberException), configuredException.Type); Assert.AreEqual(PostHandlingAction.ThrowNewException, configuredException.PostHandlingAction); Assert.AreEqual(1, configuredException.ExceptionHandlers.Count); var handler = configuredException.ExceptionHandlers.Get(0) as FaultContractExceptionHandlerData; Assert.IsNotNull(handler); Assert.AreEqual(typeof(MockFaultContract).AssemblyQualifiedName, handler.FaultContractType); Assert.AreEqual("fault message", handler.ExceptionMessage); Assert.AreEqual(3, handler.PropertyMappings.Count); Assert.IsNotNull(handler.PropertyMappings.SingleOrDefault(p => p.Name == "Message" && p.Source == "{Message}")); Assert.IsNotNull(handler.PropertyMappings.SingleOrDefault(p => p.Name == "Data" && p.Source == "{Data}")); Assert.IsNotNull(handler.PropertyMappings.SingleOrDefault(p => p.Name == "SomeNumber" && p.Source == "{OffendingNumber}")); var exceptionManager = settings.BuildExceptionManager(); NotFiniteNumberException originalException = new NotFiniteNumberException("MyException", 555); originalException.Data.Add("someKey", "someValue"); try { exceptionManager.HandleException(originalException, "policy"); Assert.Fail("Should have thrown"); } catch (FaultContractWrapperException ex) { MockFaultContract fault = (MockFaultContract)ex.FaultContract; Assert.AreEqual(originalException.Message, fault.Message); Assert.AreEqual(originalException.Data.Count, fault.Data.Count); Assert.AreEqual(originalException.Data["someKey"], fault.Data["someKey"]); Assert.AreEqual(originalException.OffendingNumber, fault.SomeNumber); } }
protected override void Arrange() { configurationSourceBuilder = new ConfigurationSourceBuilder(); policy = configurationSourceBuilder .ConfigureExceptionHandling() .GivenPolicyWithName(policyName); exception = policy.ForExceptionType(exceptionType); }
public void ThrowsArgumentExceptionWhenPolicyNameIsEmpty() { ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder(); ExceptionAssertHelper.Throws <ArgumentException>(() => builder.ConfigureExceptionHandling() .GivenPolicyWithName(string.Empty) .ForExceptionType <Exception>().ThenDoNothing()); }
public void BuildsEmptyExceptionHandlingSettings() { DictionaryConfigurationSource emptyConfigSource = new DictionaryConfigurationSource(); ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder(); builder.ConfigureExceptionHandling(); builder.UpdateConfigurationWithReplace(emptyConfigSource); var settings = emptyConfigSource.GetSection(ExceptionHandlingSettings.SectionName) as ExceptionHandlingSettings; Assert.IsNotNull(settings); Assert.AreEqual(0, settings.ExceptionPolicies.Count); }
public void BuildsExceptionPolicyForNotifyRethrowPostHandlingAction() { ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder(); builder.ConfigureExceptionHandling() .GivenPolicyWithName("EH") .ForExceptionType <Exception>().ThenNotifyRethrow(); DictionaryConfigurationSource source = new DictionaryConfigurationSource(); builder.UpdateConfigurationWithReplace(source); var exceptionManager = ((ExceptionHandlingSettings)source.GetSection("exceptionHandling")).BuildExceptionManager(); bool rethrow = exceptionManager.HandleException(new Exception(), "EH"); Assert.IsTrue(rethrow); }
public TestConfigurationBuilder AddExceptionSettings() { builder.ConfigureExceptionHandling() .GivenPolicyWithName(Policy1Name) .ForExceptionType(Policy1Types[0]) .WrapWith <Exception>() .ReplaceWith <InvalidOperationException>() .ThenThrowNewException() .ForExceptionType(Policy1Types[1]) .ReplaceWith <Exception>() .ThenThrowNewException() .GivenPolicyWithName(Policy2Name) .ForExceptionType(Policy2Types[0]) .WrapWith <InvalidOperationException>() .ThenNotifyRethrow(); return(this); }
private void ConfigureExceptionHandling() { var configurationSource = new DictionaryConfigurationSource(); var builder = new ConfigurationSourceBuilder(); builder.ConfigureExceptionHandling() .GivenPolicyWithName(DeletePolicy) .ForExceptionType <Exception>() .ThenNotifyRethrow() .ForExceptionType <NHibernate.Exceptions.ConstraintViolationException>() .WrapWith <DeleteConstraintException>() .UsingMessage("Cannot delete object.") .ThenThrowNewException(); builder.UpdateConfigurationWithReplace(configurationSource); var configurator = new UnityContainerConfigurator(this.Container); EnterpriseLibraryContainer.ConfigureContainer(configurator, configurationSource); }
public void BuildsCustomExceptionHandlerForThrowNewExceptionPostHandlingAction() { ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder(); builder.ConfigureExceptionHandling() .GivenPolicyWithName("EH") .ForExceptionType <Exception>().HandleCustom <CustomExceptionHandler>().ThenThrowNewException(); DictionaryConfigurationSource source = new DictionaryConfigurationSource(); builder.UpdateConfigurationWithReplace(source); var exceptionManager = ((ExceptionHandlingSettings)source.GetSection("exceptionHandling")).BuildExceptionManager(); ExceptionAssertHelper.Throws <NotImplementedException>(() => exceptionManager.HandleException(new Exception(), "EH")); Exception exception = null; bool rethrow = exceptionManager.HandleException(new Exception(), "EH", out exception); Assert.IsInstanceOfType(exception, typeof(NotImplementedException)); Assert.IsTrue(rethrow); }
public void BuildsReplaceExceptionHandlerWithMessageForThrowNewExceptionPostHandlingAction() { ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder(); builder.ConfigureExceptionHandling() .GivenPolicyWithName("EH") .ForExceptionType <Exception>().ReplaceWith <InvalidCastException>().UsingMessage("string").ThenThrowNewException(); DictionaryConfigurationSource source = new DictionaryConfigurationSource(); builder.UpdateConfigurationWithReplace(source); var exceptionManager = ((ExceptionHandlingSettings)source.GetSection("exceptionHandling")).BuildExceptionManager(); Exception exception = null; bool rethrow = exceptionManager.HandleException(new Exception(), "EH", out exception); Assert.IsInstanceOfType(exception, typeof(InvalidCastException)); Assert.AreEqual("string", exception.Message); Assert.IsNull(exception.InnerException); Assert.IsTrue(rethrow); }
private void ConfigureExceptionHandling() { var configurationSource = new DictionaryConfigurationSource(); var builder = new ConfigurationSourceBuilder(); builder .ConfigureExceptionHandling() .GivenPolicyWithName(DefaultPolicy) .ForExceptionType <DeleteConstraintException>() .HandleCustom <HttpErrorExceptionHandler>() .ThenThrowNewException() .ForExceptionType <BusinessValidationException>() .HandleCustom <BusinessValidationExceptionHandler>() .ThenThrowNewException() .ForExceptionType <BusinessException>() .HandleCustom <HttpErrorExceptionHandler>() .ThenThrowNewException() .ForExceptionType <Exception>() .LogToCategory("General") .WithSeverity(TraceEventType.Critical) .UsingExceptionFormatter <TextExceptionFormatter>() .HandleCustom( typeof(HttpErrorExceptionHandler), new NameValueCollection { { HttpErrorExceptionHandler.StatusCodeKey, HttpStatusCode.InternalServerError.ToString("G") }, { HttpErrorExceptionHandler.MessageKey, "An error has occurred while consuming this service. Please contact your administrator for more information." }, { HttpErrorExceptionHandler.AppendHandlingIdKey, bool.TrueString } }) .ThenThrowNewException(); builder.UpdateConfigurationWithReplace(configurationSource); var configurator = new UnityContainerConfigurator(this.Container); EnterpriseLibraryContainer.ConfigureContainer(configurator, configurationSource); }
public void BuildsWrapExceptionHandlerForThrowNewExceptionPostHandlingAction() { ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder(); builder.ConfigureExceptionHandling() .GivenPolicyWithName("EH") .ForExceptionType <Exception>().WrapWith <InvalidCastException>().ThenThrowNewException(); DictionaryConfigurationSource source = new DictionaryConfigurationSource(); builder.UpdateConfigurationWithReplace(source); var exceptionManager = ((ExceptionHandlingSettings)source.GetSection("exceptionHandling")).BuildExceptionManager(); Exception originalException = new Exception(); ExceptionAssertHelper.Throws <InvalidCastException>(() => exceptionManager.HandleException(originalException, "EH")); Exception exception = null; bool rethrow = exceptionManager.HandleException(originalException, "EH", out exception); Assert.IsInstanceOfType(exception, typeof(InvalidCastException)); Assert.AreSame(originalException, exception.InnerException); Assert.IsTrue(rethrow); }
public void BuildsLoggingExceptionHandlerWithFluentConfiguration() { DictionaryConfigurationSource configSource = new DictionaryConfigurationSource(); ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder(); builder.ConfigureExceptionHandling() .GivenPolicyWithName("Logging Policy") .ForExceptionType(typeof(DivideByZeroException)) .LogToCategory("Sample Category") .UsingEventId(100) .UsingExceptionFormatter(typeof(TextExceptionFormatter)) .UsingTitle("Sample Title") .WithPriority(4) .WithSeverity(TraceEventType.Transfer) .ThenDoNothing(); builder.ConfigureLogging() .LogToCategoryNamed("Sample Category") .WithOptions .SetAsDefaultCategory() .ToSourceLevels(SourceLevels.All) .SendTo .EventLog("Default Listener") .UsingEventLogSource("ExceptionHandling.Bvt.Tests - Fluent") .FormatWith( new FormatterBuilder() .TextFormatterNamed("Default Text Formatter") .UsingTemplate("{message}")); builder.UpdateConfigurationWithReplace(configSource); ExceptionHandlingSettings settings = configSource.GetSection(ExceptionHandlingSettings.SectionName) as ExceptionHandlingSettings; Assert.IsNotNull(settings); Assert.AreEqual(1, settings.ExceptionPolicies.Count); var policy = settings.ExceptionPolicies.Get("Logging Policy"); Assert.IsNotNull(policy); Assert.AreEqual(1, policy.ExceptionTypes.Count); var configuredException = policy.ExceptionTypes.Get(0); Assert.AreEqual(typeof(DivideByZeroException), configuredException.Type); Assert.AreEqual(PostHandlingAction.None, configuredException.PostHandlingAction); Assert.AreEqual(1, configuredException.ExceptionHandlers.Count); var handler = configuredException.ExceptionHandlers.Get(0) as LoggingExceptionHandlerData; Assert.IsNotNull(handler); Assert.AreEqual("Sample Category", handler.LogCategory); Assert.AreEqual(4, handler.Priority); Assert.AreEqual(TraceEventType.Transfer, handler.Severity); Assert.AreEqual("Sample Title", handler.Title); Assert.AreEqual(100, handler.EventId); Assert.AreEqual(typeof(TextExceptionFormatter), handler.FormatterType); var factory = new LogWriterFactory((e) => configSource.GetSection(e)); Logger.SetLogWriter(factory.Create()); var exceptionManager = settings.BuildExceptionManager(); var exceptionTothrow = new DivideByZeroException("error message"); exceptionManager.HandleException(exceptionTothrow, "Logging Policy"); var entry = EventLogEntries.Last; Assert.IsTrue(entry.Message.Contains("Type : " + exceptionTothrow.GetType().FullName)); Assert.IsTrue(entry.Message.Contains("Message : error message")); }
private void ConfigureEntLib() { var builder = new ConfigurationSourceBuilder(); #region Logger builder.ConfigureLogging() .WithOptions .SpecialSources.AllEventsCategory .SendTo .RollingFile("AllEvent Listener") .WithHeader(string.Empty) .WithFooter(string.Empty) .FormatWithSharedFormatter("Text Formatter") .ToFile("%appdata%\\KSYSLOG\\All\\KSYS_all.log") .RollAfterSize(10000) .CleanUpArchivedFilesWhenMoreThan(5) .SendTo .Custom <Log.DebuggerTraceListener>("vs debug output") .FormatWith(new FormatterBuilder() .TextFormatterNamed("Text Formatter") .UsingTemplate("{timestamp(local:yyyy-MM-dd HH:mm:ss.fff)} {win32ThreadId}{tab}{category}{tab}{severity}{tab}{title}{tab}{message}{tab}{threadName}{tab}{property(ActivityId)}{tab}{dictionary([{key} : {value}])}")) .LogToCategoryNamed("Notification.Server") .SendTo .RollingFile("Information Listener") .LogToCategoryNamed("Information") .SendTo .RollingFile("Informaiton Listener") .WithHeader(string.Empty) .WithFooter(string.Empty) .FormatWithSharedFormatter("Text Formatter") .ToFile("%appdata%\\Information\\KSYS_information.log") .RollAfterSize(10000) .CleanUpArchivedFilesWhenMoreThan(5) .LogToCategoryNamed("Debug") .SendTo .RollingFile("Debug Listener") .WithHeader(string.Empty) .WithFooter(string.Empty) .FormatWithSharedFormatter("Text Formatter") .ToFile("%appdata%\\Debug\\KSYS_debug.log") .RollAfterSize(10000) .CleanUpArchivedFilesWhenMoreThan(5) .LogToCategoryNamed("Performance") .SendTo .RollingFile("Performance Listener") .WithHeader(string.Empty) .WithFooter(string.Empty) .FormatWithSharedFormatter("Text Formatter") .ToFile("%appdata%\\Performance\\KSYS_performance.log") .RollAfterSize(10000) .CleanUpArchivedFilesWhenMoreThan(5) .LogToCategoryNamed("Security") .SendTo .RollingFile("Security Listener") .WithHeader(string.Empty) .WithFooter(string.Empty) .FormatWithSharedFormatter("Text Formatter") .ToFile("%appdata%\\Security\\KSYS_security.log") .RollAfterSize(10000) .CleanUpArchivedFilesWhenMoreThan(5) .LogToCategoryNamed("Exception") .SendTo .RollingFile("Exception Listener") .WithHeader(string.Empty) .WithFooter(string.Empty) .FormatWithSharedFormatter("Text Formatter") .ToFile("%appdata%\\Exception\\KSYS_exception.log") .RollAfterSize(10000) .CleanUpArchivedFilesWhenMoreThan(5) ; #endregion //Exception handling builder.ConfigureExceptionHandling() .GivenPolicyWithName(ExceptionPolicyName.TaskManagerPolicy) .ForExceptionType <System.Exception>().LogToCategory("Exception") .ThenDoNothing(); ; //builder.ConfigureExceptionHandling() // .GivenPolicyWithName(ExceptionPolicyName.TaskManagerPolicy) // .ForExceptionType<System.TimeoutException>() // .HandleCustom<SystemTimeoutExceptionHandler>() // .LogToCategory("General") // .WithSeverity(System.Diagnostics.TraceEventType.Error) // .UsingEventId(9000) // .ThenDoNothing() // .ForExceptionType<DataServiceTransportException>() // .HandleCustom<DataServiceTransportExceptionHandler>() // .LogToCategory("General") // .WithSeverity(System.Diagnostics.TraceEventType.Error) // .UsingEventId(9000) // .ThenDoNothing() // .ForExceptionType<Exception>() // .HandleCustom<GeneralExceptionHandler>() // .LogToCategory("General") // .WithSeverity(System.Diagnostics.TraceEventType.Error) // .UsingEventId(9003) // .ThenThrowNewException() // ; #region ConfigSource var configSource = new DictionaryConfigurationSource(); builder.UpdateConfigurationWithReplace(configSource); Logger.SetLogWriter(new LogWriterFactory(configSource).Create()); Log.Log.Debug(TraceEventType.Information, "ConfigureEntLib", "Log manager is started."); ExceptionPolicy.SetExceptionManager(new ExceptionPolicyFactory(configSource).CreateManager()); Log.Log.Debug(TraceEventType.Information, "ConfigureEntLib", "Exception manager is started."); #endregion }
public static void Initialise() { var builder = new ConfigurationSourceBuilder(); builder.ConfigureLogging() .WithOptions .DoNotRevertImpersonation() .LogToCategoryNamed("General") .WithOptions .SetAsDefaultCategory() .SendTo .RollingFile("Rolling Flat File Trace Listener") .ToFile(@"log\trace.log") .WithHeader("----------------------") .RollAfterSize(50) .UseTimeStampPattern("MM_dd_yyyy") .WhenRollFileExists(RollFileExistsBehavior.Increment) .RollEvery(RollInterval.Day) .WithTraceOptions(TraceOptions.Callstack | TraceOptions.DateTime | TraceOptions.LogicalOperationStack | TraceOptions.ProcessId | TraceOptions.ThreadId | TraceOptions.Timestamp) .CleanUpArchivedFilesWhenMoreThan(5); var exceptionhandling = builder.ConfigureExceptionHandling(); //exceptionhandling // .GivenPolicyWithName("PassThroughPolicy") // .ForExceptionType<System.Exception>() // .ReplaceWith<PassThroughException>() // .ThenNotifyRethrow(); //exceptionhandling // .GivenPolicyWithName("BusinessLogicPolicy") // .ForExceptionType<System.Exception>() // .LogToCategory("General") // .WithSeverity(System.Diagnostics.TraceEventType.Error) // .WithPriority(0) // .UsingEventId(100) // .ReplaceWith<UserInterfaceException>() // .UsingMessage("A User Interface Error Happened.") // .ThenThrowNewException(); exceptionhandling .GivenPolicyWithName("UserInterfacePolicy") .ForExceptionType <System.Exception>() .LogToCategory("General") .WithSeverity(System.Diagnostics.TraceEventType.Error) .WithPriority(0) .UsingEventId(100) .ReplaceWith <UserInterfaceException>() .UsingMessage("A User Interface Error Happened.") //Werner: Removed this so that it shows the Dialogue and restarts the system (2014-10-20) //.ThenThrowNewException() ; /* * * ConfigurationSourceBuilder formatBuilder = new ConfigurationSourceBuilder(); * * ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder(); * builder.ConfigureLogging().LogToCategoryNamed("General"). * SendTo. * RollingFile("Rolling Flat File Trace Listener") * .CleanUpArchivedFilesWhenMoreThan(2).WhenRollFileExists(RollFileExistsBehavior.Increment) * .WithTraceOptions(TraceOptions.None) * .RollEvery(RollInterval.Minute) * .RollAfterSize(10) * .UseTimeStampPattern("yyyy-MM-dd") * .ToFile("log\trace.log") * .FormatWith(new FormatterBuilder().TextFormatterNamed("textFormatter")); */ var configSource = new DictionaryConfigurationSource(); builder.UpdateConfigurationWithReplace(configSource); EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource); /* * var writer = EnterpriseLibraryContainer.Current.GetInstance<Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter>(); * * DateTime stopWritingTime = DateTime.Now.AddMinutes(10); * while (DateTime.Now < stopWritingTime) * { * writer.Write("test", "General"); * } */ }
public void BuildsWrapAndReplaceAndCustomExceptionHandlersForAllPostHandlingActions() { DictionaryConfigurationSource emptyConfigSource = new DictionaryConfigurationSource(); ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder(); builder.ConfigureExceptionHandling() .GivenPolicyWithName("Sample Policy") .ForExceptionType(typeof(DivideByZeroException)) .ReplaceWith(typeof(CustomException)) .ThenDoNothing() .GivenPolicyWithName("Sample Policy 2") .ForExceptionType(typeof(InvalidCastException)) .HandleCustom(typeof(CustomExceptionHandler)) .ThenNotifyRethrow() .ForExceptionType(typeof(OutOfMemoryException)) .WrapWith(typeof(OverflowException)) .ThenThrowNewException(); builder.UpdateConfigurationWithReplace(emptyConfigSource); ExceptionHandlingSettings settings = emptyConfigSource.GetSection(ExceptionHandlingSettings.SectionName) as ExceptionHandlingSettings; Assert.IsNotNull(settings); Assert.AreEqual(2, settings.ExceptionPolicies.Count); // Policy one var policy = settings.ExceptionPolicies.Get("Sample Policy"); Assert.IsNotNull(policy); Assert.AreEqual(1, policy.ExceptionTypes.Count); var configuredException = policy.ExceptionTypes.Get(0); Assert.AreEqual(typeof(DivideByZeroException), configuredException.Type); Assert.AreEqual(PostHandlingAction.None, configuredException.PostHandlingAction); Assert.AreEqual(1, configuredException.ExceptionHandlers.Count); var handler = configuredException.ExceptionHandlers.Get(0) as ReplaceHandlerData; Assert.IsNotNull(handler); Assert.AreEqual(typeof(CustomException), handler.ReplaceExceptionType); // Policy 2 policy = settings.ExceptionPolicies.Get("Sample Policy 2"); Assert.IsNotNull(policy); Assert.AreEqual(2, policy.ExceptionTypes.Count); configuredException = policy.ExceptionTypes.Get(0); Assert.AreEqual(typeof(InvalidCastException), configuredException.Type); Assert.AreEqual(PostHandlingAction.NotifyRethrow, configuredException.PostHandlingAction); Assert.AreEqual(1, configuredException.ExceptionHandlers.Count); var customHandler = configuredException.ExceptionHandlers.Get(0) as CustomHandlerData; Assert.IsNotNull(customHandler); Assert.AreEqual(typeof(CustomExceptionHandler), customHandler.Type); configuredException = policy.ExceptionTypes.Get(1); Assert.AreEqual(typeof(OutOfMemoryException), configuredException.Type); Assert.AreEqual(PostHandlingAction.ThrowNewException, configuredException.PostHandlingAction); Assert.AreEqual(1, configuredException.ExceptionHandlers.Count); var wrapHandler = configuredException.ExceptionHandlers.Get(0) as WrapHandlerData; Assert.IsNotNull(wrapHandler); Assert.AreEqual(typeof(OverflowException), wrapHandler.WrapExceptionType); }
public void UsingDefaultExceptionPolicies() { // This is the fluent configuration version DictionaryConfigurationSource emptyConfigSource = new DictionaryConfigurationSource(); ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder(); builder.ConfigureExceptionHandling() .GivenPolicyWithName("BasePolicy") // This is a catch all policy .ForExceptionType <Exception>() // For any type of exception .WrapWith <GeneralException>() // Wrap with a better exception with more info .UsingMessage("A handled exception occured and was processed by the BasePolicy") .HandleCustom <LoggingExceptonHandler>() // Log the exception .ThenThrowNewException() // Rethrow the exception so the appropriate layer can handle it .GivenPolicyWithName("BusinessWrapPolicy") // For wrapping all exceptions that bubble up to business layer .ForExceptionType <Exception>() .WrapWith <BusinessException>() .HandleCustom <LoggingExceptonHandler>() .ThenThrowNewException() .GivenPolicyWithName("BusinessReplacePolicy") // We use this for removing sensitive information from an exception .ForExceptionType <BusinessException>() .ReplaceWith <FriendlyBusinessException>() // So let's simply make this a presentation friendly exception .ThenThrowNewException() .GivenPolicyWithName("ApplicationWrapPolicy") // For wrapping all exceptions that bubble up to application layer .ForExceptionType <Exception>() .WrapWith <ApplicationTierException>() .HandleCustom <LoggingExceptonHandler>() .ThenThrowNewException() .GivenPolicyWithName("ApplicationReplacePolicy") // We use this for removing sensitive information from an exception .ForExceptionType <ApplicationTierException>() .ReplaceWith <FriendlyApplicationException>() // So let's simply make this a presentation friendly exception .ThenThrowNewException() .GivenPolicyWithName("SecurityReplacePolicy") // We use this for removing sensitive information from an exception .ForExceptionType <SecurityException>() // The assumption is that we've already handled the exception at a lower layer .ReplaceWith <FriendlySecurityException>() // So let's simply make this a presentation friendly exception .HandleCustom <LoggingExceptonHandler>() .ThenThrowNewException() // No need to log, just .GivenPolicyWithName("PresentationReplacePolicy") // We use this for removing sensitive information from an exception .ForExceptionType <FriendlyApplicationException>() // The assumption is that we've already handled the exception at a lower layer .ThenDoNothing() // Already handled it .ForExceptionType <FriendlyBusinessException>() .ThenDoNothing() // Already handled it .ForExceptionType <FriendlySecurityException>() .ThenDoNothing(); // Already handled it builder.UpdateConfigurationWithReplace(emptyConfigSource); // This is the configuration file version //IConfigurationSource config = new FileConfigurationSource(@"App.config"); //ConfigurationSourceFactory.Create(); // Requires that configuration file be in place. ExceptionPolicyFactory factory = new ExceptionPolicyFactory(emptyConfigSource); ExceptionManager exManager = factory.CreateManager(); ExceptionPolicy.SetExceptionManager(factory.CreateManager()); }
private void ConfigureExceptionHandling() { var configurationSource = new DictionaryConfigurationSource(); var builder = new ConfigurationSourceBuilder(); builder.ConfigureExceptionHandling() .GivenPolicyWithName(DeletePolicy) .ForExceptionType<Exception>() .ThenNotifyRethrow() .ForExceptionType<NHibernate.Exceptions.ConstraintViolationException>() .WrapWith<DeleteConstraintException>() .UsingMessage("Cannot delete object.") .ThenThrowNewException(); builder.UpdateConfigurationWithReplace(configurationSource); var configurator = new UnityContainerConfigurator(this.Container); EnterpriseLibraryContainer.ConfigureContainer(configurator, configurationSource); }