public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var properties = context.ActionDescriptor.Parameters
                             .Select(p => context.ActionArguments.SingleOrDefault(x => x.Key == p.Name))
                             .SelectMany(pv => _loggingDataExtractor.ConvertToDictionary(pv.Value, pv.Key))
                             .ToDictionary(x => x.Key, x => x.Value);

            if (context.ActionDescriptor is ControllerActionDescriptor actionDescriptor)
            {
                properties.Add(nameof(actionDescriptor.ControllerName), actionDescriptor.ControllerName);
                properties.Add(nameof(actionDescriptor.ActionName), actionDescriptor.ActionName);
                properties.Add(nameof(actionDescriptor.DisplayName), actionDescriptor.DisplayName);
            }

            using (_logger.BeginScope(properties))
            {
                _logger.LogDebug("Starting request");
                var sw     = Stopwatch.StartNew();
                var action = await next();

                if (action.Exception != null)
                {
                    var ex = action.Exception;
                    _logger.LogError(ex, ex.Message);
                }

                _logger.LogDebug("Handled request in {ElapsedMilliseconds}ms", sw.ElapsedMilliseconds);
            }
        }
Example #2
0
        public async Task Handle(TCommand command)
        {
            var properties = _loggingDataExtractor.ConvertToDictionary(command);

            properties.Add(nameof(TCommand), typeof(TCommand).Name);
            using (_logger.BeginScope(properties))
            {
                _logger.LogDebug("Handling command");
                var sw = Stopwatch.StartNew();
                await _underlyingHandler.Handle(command);

                _logger.LogDebug("Handled command in {ElapsedMilliseconds}ms", sw.ElapsedMilliseconds);
            }
        }
Example #3
0
        public async Task <TResult> Handle(TQuery query)
        {
            var properties = _loggingDataExtractor.ConvertToDictionary(query);

            properties.Add(nameof(TQuery), typeof(TQuery).Name);
            properties.Add(nameof(TResult), typeof(TResult).Name);
            using (_logger.BeginScope(properties))
            {
                // Unfortunetely this scope wont apply to the underlying handler as its already been resolved from the logger factory.
                _logger.LogDebug("Handling query");
                var sw     = Stopwatch.StartNew();
                var result = await _underlyingHandler.Handle(query);

                _logger.LogDebug("Handled query in {ElapsedMilliseconds}ms", sw.ElapsedMilliseconds);
                return(result);
            }
        }
Example #4
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var properties = context.ActionDescriptor.Parameters.Select(p => context.ActionArguments.SingleOrDefault(x => x.Key == p.Name)).SelectMany(pv => _loggingDataExtractor.ConvertToDictionary(pv.Value, pv.Key)).ToDictionary(x => x.Key, x => x.Value);

            var actionDescriptor = context.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;

            if (actionDescriptor != null)
            {
                properties.Add(nameof(actionDescriptor.ControllerName), actionDescriptor.ControllerName);
                properties.Add(nameof(actionDescriptor.ActionName), actionDescriptor.ActionName);
                properties.Add(nameof(actionDescriptor.DisplayName), actionDescriptor.DisplayName);
            }

            using (_logger.BeginScope(properties))
            {
                await next();
            }
        }