示例#1
0
        public static IExceptionPolicyBuilder BuildEdamosDefault(this IExceptionPolicyBuilder builder)
        {
            builder.For <EdamosApiException>()
            .Log(lo =>
            {
                lo.EventIdFactory = (c, e) => e.EventId;
                lo.Level          = (c, e) => e.LogLevel;
                lo.StateFactory   = (c, e, o) => new FormattedLogValues(
                    $"{e.Message}. Details: {e.DetailsFormat}", e.DetailsParams.ToArray());

                lo.Category = (c, e) => Consts.Logs.EdamosApiCategory;
            })
            .Response(e => (int)e.StatusCode)
            .ClearCacheHeaders()
            .WithObjectResult(
                (r, e) => new EdamosApiError
            {
                StatusCode      = r.HttpContext.Response.StatusCode,
                Message         = e.Message,
                TraceIdentifier = r.HttpContext.TraceIdentifier
            })
            .Handled();

            builder.For <Exception>()
            .Log(lo =>
            {
                lo.Category       = (c, e) => Consts.Logs.EdamosApiCategory;
                lo.Level          = (c, e) => LogLevel.Error;
                lo.EventIdFactory = (c, e) => LogEvents.UnhandledException;
            })
            .Response(null, ResponseAlreadyStartedBehaviour.GoToNextHandler)
            .ClearCacheHeaders()
            .WithObjectResult((r, e) => new EdamosApiError
            {
                StatusCode      = r.HttpContext.Response.StatusCode,
                Message         = "Internal Server Error",
                TraceIdentifier = r.HttpContext.TraceIdentifier
            })
            .Handled();

            return(builder);
        }
示例#2
0
        private static void BuildPolicy(IExceptionPolicyBuilder builder, PolicyConfiguration config, IDictionary <string, IExceptionFilter> filters)
        {
            foreach (var it in config.PreHandlers)
            {
                var handlerConfig    = GetHandlerConfiguration(it.HandlerType);
                var filterableConfig = it as FilterableHandlerConfiguration;
                if (null == filterableConfig)
                {
                    builder.Pre(handlerBuilder => handlerConfig.Use(handlerBuilder, _ => true, it.Arguments.ToDictionary(item => item.Name, item => item.Value)));
                }
                else
                {
                    builder.Pre(handlerBuilder => handlerConfig.Use(handlerBuilder, context => filters[filterableConfig.Filter].Match(context), it.Arguments.ToDictionary(item => item.Name, item => item.Value)));
                }
            }

            foreach (var it in config.PostHandlers)
            {
                var handlerConfig    = GetHandlerConfiguration(it.HandlerType);
                var filterableConfig = it as FilterableHandlerConfiguration;
                if (null == filterableConfig)
                {
                    builder.Post(handlerBuilder => handlerConfig.Use(handlerBuilder, _ => true, it.Arguments.ToDictionary(item => item.Name, item => item.Value)));
                }
                else
                {
                    builder.Post(handlerBuilder => handlerConfig.Use(handlerBuilder, context => filters[filterableConfig.Filter].Match(context), it.Arguments.ToDictionary(item => item.Name, item => item.Value)));
                }
            }

            foreach (var entry in config.PolicyEntries)
            {
                builder.For(entry.ExceptionType, entry.PostHandlingAction, handlerBuilder => {
                    foreach (var it in entry.Handlers)
                    {
                        var handlerConfig    = GetHandlerConfiguration(it.HandlerType);
                        var filterableConfig = it as FilterableHandlerConfiguration;
                        if (null == filterableConfig)
                        {
                            handlerConfig.Use(handlerBuilder, _ => true, it.Arguments.ToDictionary(item => item.Name, item => item.Value));
                        }
                        else
                        {
                            handlerConfig.Use(handlerBuilder, context => filters[filterableConfig.Filter].Match(context), it.Arguments.ToDictionary(item => item.Name, item => item.Value));
                        }
                    }
                });
            }
        }
 /// <summary>
 /// Register exception handler chain for specified exception type.
 /// </summary>
 /// <param name="builder">The <see cref="IExceptionPolicyBuilder"/> to which the type specific hanlder chain is registered.</param>
 /// <typeparam name="TException">The type of exception to handle.</typeparam>
 /// <param name="postHandlingAction">Determining what action should occur after an exception is handled by the configured exception handling chain.</param>
 /// <param name="configure">An <see cref="Action{IExceptionHandlerBuilder}"/> to build the exception handler chain.</param>
 /// <returns>The current <see cref="IExceptionPolicyBuilder"/>.</returns>
 public static IExceptionPolicyBuilder For <TException>(this IExceptionPolicyBuilder builder, PostHandlingAction postHandlingAction, Action <IExceptionHandlerBuilder> configure)
     where TException : Exception
 {
     Guard.ArgumentNotNull(builder, nameof(builder));
     return(builder.For(typeof(TException), postHandlingAction, configure));
 }