public static void AfterOnActionExecution(
            this DiagnosticListener diagnosticListener,
            ActionExecutedContext actionExecutedContext,
            IAsyncActionFilter filter)
        {
            Debug.Assert(diagnosticListener != null);
            Debug.Assert(actionExecutedContext != null);
            Debug.Assert(filter != null);

            // Inlinable fast-path check if diagnostic listener is enabled
            if (diagnosticListener.IsEnabled())
            {
                AfterOnActionExecutionImpl(diagnosticListener, actionExecutedContext, filter);
            }
        }
コード例 #2
0
 public static void BeforeOnActionExecution(
     this DiagnosticSource diagnosticSource,
     ActionExecutingContext actionExecutingContext,
     IAsyncActionFilter filter)
 {
     if (diagnosticSource.IsEnabled("Microsoft.AspNetCore.Mvc.BeforeOnActionExecution"))
     {
         diagnosticSource.Write(
             "Microsoft.AspNetCore.Mvc.BeforeOnActionExecution",
             new
         {
             actionDescriptor       = actionExecutingContext.ActionDescriptor,
             actionExecutingContext = actionExecutingContext,
             filter = filter
         });
     }
 }
コード例 #3
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            IFeatureManager featureManager = context.HttpContext.RequestServices.GetRequiredService <IFeatureManagerSnapshot>();

            if (await featureManager.IsEnabledAsync(FeatureName).ConfigureAwait(false))
            {
                var serviceProvider = context.HttpContext.RequestServices.GetRequiredService <IServiceProvider>();

                IAsyncActionFilter filter = ActivatorUtilities.CreateInstance <T>(serviceProvider);

                await filter.OnActionExecutionAsync(context, next).ConfigureAwait(false);
            }
            else
            {
                await next().ConfigureAwait(false);
            }
        }
コード例 #4
0
        public async Task OnActionExecutionAsyncMethodHappyPath2()
        {
            const string   messageFormat          = "My message format: {0}";
            const LogLevel logLevel               = LogLevel.Info;
            const string   exceptionMessageFormat = "My exception message format: {0}.";
            const LogLevel exceptionLogLevel      = LogLevel.Fatal;
            const string   actionName             = "MyAction";
            const string   actionArgumentName     = "foo";
            const int      actionArgument         = 123;

            var mockActionFilter = new Mock <LoggingActionFilter>(messageFormat, null, logLevel);

            mockActionFilter.Object.ExceptionMessageFormat = exceptionMessageFormat;
            mockActionFilter.Object.ExceptionLogLevel      = exceptionLogLevel;

            IAsyncActionFilter loggingActionFilter = mockActionFilter.Object;

            var mockLogger = new MockLogger();

            var httpContext = new DefaultHttpContext()
            {
                RequestServices = GetServiceProvider(mockLogger.Object)
            };
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()
            {
                DisplayName = actionName
            });
            var context = new ActionExecutingContext(actionContext, Array.Empty <IFilterMetadata>(), new Dictionary <string, object>(), null);

            context.ActionArguments.Add(actionArgumentName, actionArgument);

            var actionExecutedContext = new ActionExecutedContext(actionContext, Array.Empty <IFilterMetadata>(), null);
            var exception             = actionExecutedContext.Exception = new Exception();

            ActionExecutionDelegate next = () => Task.FromResult(actionExecutedContext);

            await loggingActionFilter.OnActionExecutionAsync(context, next);

            mockLogger.VerifyFatal(string.Format(exceptionMessageFormat, actionName), exception,
                                   new { foo = 123, ResponseStatusCode = 500 }, Times.Once());
        }
コード例 #5
0
        public static void AfterOnActionExecution(
            this DiagnosticSource diagnosticSource,
            ActionExecutedContext actionExecutedContext,
            IAsyncActionFilter filter)
        {
            Debug.Assert(diagnosticSource != null);
            Debug.Assert(actionExecutedContext != null);
            Debug.Assert(filter != null);

            if (diagnosticSource.IsEnabled("Microsoft.AspNetCore.Mvc.AfterOnActionExecution"))
            {
                diagnosticSource.Write(
                    "Microsoft.AspNetCore.Mvc.AfterOnActionExecution",
                    new
                {
                    actionDescriptor      = actionExecutedContext.ActionDescriptor,
                    actionExecutedContext = actionExecutedContext,
                    filter = filter
                });
            }
        }
 private static void AfterOnActionExecutionImpl(DiagnosticListener diagnosticListener, ActionExecutedContext actionExecutedContext, IAsyncActionFilter filter)
 {
     if (diagnosticListener.IsEnabled(Diagnostics.AfterActionFilterOnActionExecutionEventData.EventName))
     {
         diagnosticListener.Write(
             Diagnostics.AfterActionFilterOnActionExecutionEventData.EventName,
             new AfterActionFilterOnActionExecutionEventData(
                 actionExecutedContext.ActionDescriptor,
                 actionExecutedContext,
                 filter
                 ));
     }
 }
コード例 #7
0
 public static void AfterOnActionExecution(
     this DiagnosticSource diagnosticSource,
     ActionDescriptor actionDescriptor,
     ActionExecutedContext actionExecutedContext,
     IAsyncActionFilter filter)
 {
     if (diagnosticSource.IsEnabled("Microsoft.AspNetCore.Mvc.AfterOnActionExecution"))
     {
         diagnosticSource.Write(
             "Microsoft.AspNetCore.Mvc.AfterOnActionExecution",
             new
             {
                 actionDescriptor = actionDescriptor,
                 actionExecutedContext = actionExecutedContext,
                 filter = filter
             });
     }
 }
コード例 #8
0
        public static void BeforeOnActionExecution(
            this DiagnosticSource diagnosticSource,
            ActionExecutingContext actionExecutingContext,
            IAsyncActionFilter filter)
        {
            Debug.Assert(diagnosticSource != null);
            Debug.Assert(actionExecutingContext != null);
            Debug.Assert(filter != null);

            if (diagnosticSource.IsEnabled("Microsoft.AspNetCore.Mvc.BeforeOnActionExecution"))
            {
                diagnosticSource.Write(
                    "Microsoft.AspNetCore.Mvc.BeforeOnActionExecution",
                    new
                    {
                        actionDescriptor = actionExecutingContext.ActionDescriptor,
                        actionExecutingContext = actionExecutingContext,
                        filter = filter
                    });
            }
        }
コード例 #9
0
 private static void AfterOnActionExecutionImpl(DiagnosticListener diagnosticListener, ActionExecutedContext actionExecutedContext, IAsyncActionFilter filter)
 {
     if (diagnosticListener.IsEnabled("Microsoft.AspNetCore.Mvc.AfterOnActionExecution"))
     {
         diagnosticListener.Write(
             "Microsoft.AspNetCore.Mvc.AfterOnActionExecution",
             new
         {
             actionDescriptor      = actionExecutedContext.ActionDescriptor,
             actionExecutedContext = actionExecutedContext,
             filter = filter
         });
     }
 }
コード例 #10
0
 private static void BeforeOnActionExecutionImpl(DiagnosticListener diagnosticListener, ActionExecutingContext actionExecutingContext, IAsyncActionFilter filter)
 {
     if (diagnosticListener.IsEnabled(Diagnostics.BeforeOnActionExecution.EventName))
     {
         diagnosticListener.Write(
             Diagnostics.BeforeOnActionExecution.EventName,
             new BeforeOnActionExecution(
                 actionExecutingContext.ActionDescriptor,
                 actionExecutingContext,
                 filter
                 ));
     }
 }