private static ExceptionManager CreateExceptionManager(IConfigurationSource config) { var factory = new ExceptionPolicyFactory(config); var manger = factory.CreateManager(); ExceptionPolicy.SetExceptionManager(manger); return manger; }
static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.LoadConfiguration(); Logger.SetLogWriter(new LogWriterFactory().Create()); IConfigurationSource config = ConfigurationSourceFactory.Create(); ExceptionPolicyFactory factory = new ExceptionPolicyFactory(config); ExceptionManager exceptionManager = factory.CreateManager(); ExceptionPolicy.SetExceptionManager(exceptionManager); return container; }
public static void Configure(IUnityContainer container) { // Get Entlib config source (Current is in Web.EnterpriseLibrary.config) IConfigurationSource source = ConfigurationSourceFactory.Create(); // Config container from Policy injection config settings var policyInjectionSettings = (PolicyInjectionSettings)source.GetSection(PolicyInjectionSettings.SectionName); policyInjectionSettings.ConfigureContainer(container); // Config retry policy var retryPolicySettings = RetryPolicyConfigurationSettings.GetRetryPolicySettings(source); // turn off throwIfSet for unit testing RetryPolicyFactory.SetRetryManager(retryPolicySettings.BuildRetryManager(), throwIfSet: false); // get factories from config var policyFactory = new ExceptionPolicyFactory(source); var dbFactory = new DatabaseProviderFactory(source); var validationFactory = ConfigurationValidatorFactory.FromConfigurationSource(source); // Set default locator UnityServiceLocator locator = new UnityServiceLocator(container); ServiceLocator.SetLocatorProvider(() => locator); container .AddNewExtension<Interception>() // register Entlib types with appropiate factory .RegisterType<ExceptionManager>(new InjectionFactory(c => policyFactory.CreateManager())) .RegisterType<Database>(new InjectionFactory(c => dbFactory.CreateDefault())) .RegisterInstance<ValidatorFactory>(validationFactory) // use registration by convention extension for registering app types; IProfileStore, IUserRepository .RegisterTypes(AllClasses.FromAssemblies(Assembly.GetExecutingAssembly()), WithMappings.FromAllInterfacesInSameAssembly, WithName.Default, WithLifetime.ContainerControlled) // register types with interception .RegisterType<AExpense.Model.User>(new Interceptor<VirtualMethodInterceptor>(), new InterceptionBehavior<TracingBehavior>()) .RegisterType<IExpenseRepository, ExpenseRepository>(new Interceptor<VirtualMethodInterceptor>(), new InterceptionBehavior<PolicyInjectionBehavior>()); }
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); IConfigurationSource config = ConfigurationSourceFactory.Create(); Logger.SetLogWriter(new LogWriterFactory(config).Create()); ExceptionPolicyFactory factory = new ExceptionPolicyFactory(config); ExceptionPolicy.SetExceptionManager(factory.CreateManager()); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomainUnhandledException); try { Bootstrapper applicationBootstrapper = new Bootstrapper(); applicationBootstrapper.Run(); } catch (Exception ex) { HandleException(ex); } }
private static ExceptionPolicy GetExceptionPolicy(Exception exception, string policyName, ConfigurationContext configurationContext) { try { ExceptionPolicyFactory factory = new ExceptionPolicyFactory(configurationContext); return(factory.CreateExceptionPolicy(policyName, exception)); } catch (ConfigurationException ex) { ExceptionUtility.LogHandlingException(policyName, ex, null, exception); ExceptionNotHandledEvent.Fire(); throw new ExceptionHandlingException(ex.Message, ex); } catch (InvalidOperationException ex) { ExceptionUtility.LogHandlingException(policyName, ex, null, exception); ExceptionNotHandledEvent.Fire(); throw new ExceptionHandlingException(ex.Message, ex); } }
public void OnException(ExceptionContext filterContext) { if (filterContext == null || filterContext.HttpContext == null) { return; } var exception = HandleException(filterContext.Exception, new Guid()); var exceptionManager = new ExceptionPolicyFactory().CreateManager(); exceptionManager.HandleException(exception, "Global Exception Policy"); filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; var httpException = filterContext.Exception as HttpException; if (httpException == null) { filterContext.ExceptionHandled = true; return; } filterContext.Result = new ViewResult { ViewName = string.Format("Http{0}", new Exception("HTTP error", httpException)) }; filterContext.ExceptionHandled = true; }
private static void DefineExceptionHandling(IConfigurationSourceBuilder builder, IConfigurationSource source) { builder.ConfigureExceptionHandling() .GivenPolicyWithName(Settings.Default.UnityPolicyName) .ForExceptionType<Exception>() .LogToCategory(Settings.Default.LogToCategoryNamed) .UsingExceptionFormatter<TextExceptionFormatter>() .WithSeverity(TraceEventType.Critical) .ThenNotifyRethrow(); var configurationSource = CreateConfigurationSource(builder,source); var exceptionHandlerFactory = new ExceptionPolicyFactory(configurationSource); ExceptionPolicy.SetExceptionManager(exceptionHandlerFactory.CreateManager()); }
private static bool HandleException(Exception exceptionToHandle, string policyName, ExceptionPolicyFactory policyFactory, out Exception exceptionToThrow) { try { bool retrowAdviced = HandleException(exceptionToHandle, policyName, policyFactory); exceptionToThrow = null; return retrowAdviced; } catch (Exception exception) { exceptionToThrow = exception; return true; } }
private static bool HandleException(Exception exceptionToHandle, string policyName, ExceptionPolicyFactory policyFactory) { ExceptionPolicyImpl policy = GetExceptionPolicy(exceptionToHandle, policyName, policyFactory); return policy.HandleException(exceptionToHandle); }
private static ExceptionPolicyImpl GetExceptionPolicy(Exception exception, string policyName, ExceptionPolicyFactory factory) { try { return factory.Create(policyName); } catch (ConfigurationErrorsException configurationException) { try { DefaultExceptionHandlingEventLogger logger = EnterpriseLibraryFactory.BuildUp<DefaultExceptionHandlingEventLogger>(); logger.LogConfigurationError(configurationException, policyName); } catch { } throw; } catch (Exception ex) { try { string exceptionMessage = ExceptionUtility.FormatExceptionHandlingExceptionMessage(policyName, ex, null, exception); DefaultExceptionHandlingEventLogger logger = EnterpriseLibraryFactory.BuildUp<DefaultExceptionHandlingEventLogger>(); logger.LogInternalError(policyName, exceptionMessage); } catch { } throw new ExceptionHandlingException(ex.Message, ex); } }
private static bool HandleException(Exception exceptionToHandle, string policyName, ExceptionPolicyFactory policyFactory) { ExceptionPolicyImpl policy = GetExceptionPolicy(exceptionToHandle, policyName, policyFactory); return(policy.HandleException(exceptionToHandle)); }
private static ExceptionPolicyImpl GetExceptionPolicy(Exception exception, string policyName, ExceptionPolicyFactory factory) { try { return(factory.Create(policyName)); } catch (ConfigurationErrorsException configurationException) { try { DefaultExceptionHandlingEventLogger logger = EnterpriseLibraryFactory.BuildUp <DefaultExceptionHandlingEventLogger>(); logger.LogConfigurationError(configurationException, policyName); } catch { } throw; } catch (Exception ex) { try { string exceptionMessage = ExceptionUtility.FormatExceptionHandlingExceptionMessage(policyName, ex, null, exception); DefaultExceptionHandlingEventLogger logger = EnterpriseLibraryFactory.BuildUp <DefaultExceptionHandlingEventLogger>(); logger.LogInternalError(policyName, exceptionMessage); } catch { } throw new ExceptionHandlingException(ex.Message, ex); } }
private static bool HandleException(Exception exceptionToHandle, string policyName, ExceptionPolicyFactory policyFactory, out Exception exceptionToThrow) { try { bool retrowAdviced = HandleException(exceptionToHandle, policyName, policyFactory); exceptionToThrow = null; return(retrowAdviced); } catch (Exception exception) { exceptionToThrow = exception; return(true); } }
private static ExceptionPolicy GetExceptionPolicy(Exception exception, string policyName, ConfigurationContext configurationContext) { try { ExceptionPolicyFactory factory = new ExceptionPolicyFactory(configurationContext); return factory.CreateExceptionPolicy(policyName, exception); } catch (ConfigurationException ex) { ExceptionUtility.LogHandlingException(policyName, ex, null, exception); ExceptionNotHandledEvent.Fire(); throw new ExceptionHandlingException(ex.Message, ex); } catch (InvalidOperationException ex) { ExceptionUtility.LogHandlingException(policyName, ex, null, exception); ExceptionNotHandledEvent.Fire(); throw new ExceptionHandlingException(ex.Message, ex); } }