コード例 #1
0
        public override async Task <TResponse> Apply(TRequest request,
                                                     Func <TRequest, Task <TResponse> > next)
        {
            var logger = GetEffectiveLogger();

            Stopwatch stopwatch = null;

            if (logger.IsDebugEnabled || logger.IsErrorEnabled)
            {
                stopwatch = new Stopwatch();
                stopwatch.Start();
            }

            if (logger.IsDebugEnabled)
            {
                logger.DebugFormat("Handling {0}", GetDescription(request));
            }

            try
            {
                var response = await next(request);

                if (logger.IsDebugEnabled)
                {
                    stopwatch?.Stop();
                    logger.DebugFormat("Completed {0}{1} with {2}",
                                       DTO.PrettyName(request.GetType()),
                                       FormatDuration(stopwatch), GetDescription(response));
                }

                return(response);
            }
            catch (Exception ex)
            {
                if (WarningExceptions.Any(wex => wex.IsInstanceOfType(ex)))
                {
                    if (logger.IsWarnEnabled)
                    {
                        stopwatch?.Stop();
                        logger.WarnFormat(ex, "Failed {0}{1}", GetDescription(request),
                                          FormatDuration(stopwatch));
                    }
                }
                else if (logger.IsErrorEnabled)
                {
                    stopwatch?.Stop();
                    logger.ErrorFormat(ex, "Failed {0}{1}", GetDescription(request),
                                       FormatDuration(stopwatch));
                }
                ex.Data[Stage.Logging] = true;
                throw;
            }
        }