Exemplo n.º 1
0
        /// <summary>
        /// Сохранить длительность выполнения запроса
        /// </summary>
        ///
        /// <param name="context">Контекст исполнения запроса</param>
        /// <param name="logMessage">Модель структурированного сообщения для логирования</param>
        public static void SetDuration(HttpContext context, WebApiLogMessage logMessage)
        {
            if (!context.Items.TryGetValue(FilterConstants.Stopwatch, out var stopWatchObject))
            {
                return;
            }

            if (!(stopWatchObject is Stopwatch stopwatch))
            {
                return;
            }

            stopwatch.Stop();
            logMessage.DurationMilliseconds = stopwatch.ElapsedMilliseconds;
        }
Exemplo n.º 2
0
        public static void WriteLogMessage(HttpContext context,
                                           WebApiLogMessage webApiLogMessage,
                                           ILogger logger,
                                           string responseBody)
        {
            webApiLogMessage.StatusCode = context.Response.StatusCode;
            if (context.Response.StatusCode > 399 || logger.IsEnabled(LogLevel.Debug))
            {
                webApiLogMessage.HttpResponce = responseBody;
            }

            SetDuration(context, webApiLogMessage);

            if (context.Items.ContainsKey(FilterConstant.UserName))
            {
                webApiLogMessage.UserName = context.Items[FilterConstant.UserName]?.ToString();
            }

            try
            {
                if (webApiLogMessage.StatusCode < 400)
                {
                    logger.LogInformation("RequestId:{RequestId}\r\nActionName:{ActionName}\r\nControllerName:{ControllerName}\r\nStatusCode:{StatusCode}\r\nUserName:{UserName}\r\nDurationMilliseconds:{DurationMilliseconds}\r\nHttpResponce:{HttpResponce}", new object[]
                    {
                        webApiLogMessage.RequestId,
                        webApiLogMessage.ActionName,
                        webApiLogMessage.ControllerName,
                        webApiLogMessage.StatusCode,
                        webApiLogMessage.UserName,
                        webApiLogMessage.DurationMilliseconds,
                        webApiLogMessage.HttpResponce
                    });
                }
                else
                {
                    logger.LogError("RequestId:{RequestId}\r\nActionName:{ActionName}\r\nControllerName:{ControllerName}\r\nStatusCode:{StatusCode}\r\nUserName:{UserName}\r\nDurationMilliseconds:{DurationMilliseconds}\r\nHttpResponce:{HttpResponce}\r\nErrorMessage:{ErrorMessage}", new object[]
                    {
                        webApiLogMessage.RequestId,
                        webApiLogMessage.ActionName,
                        webApiLogMessage.ControllerName,
                        webApiLogMessage.StatusCode,
                        webApiLogMessage.UserName,
                        webApiLogMessage.DurationMilliseconds,
                        webApiLogMessage.HttpResponce,
                        context.Items.ContainsKey("excpetionReceived") ? context.Items["excpetionReceived"] : null
                    });
                }
            }
            catch (JsonSerializationException)
            {
                if (context.Items.ContainsKey("excpetionReceived"))
                {
                    if (webApiLogMessage is WebApiExceptionLogMessage msg)
                    {
                        msg.ExceptionText     = msg.ExceptionReceived.ToString();
                        msg.ExceptionReceived = null;
                        logger.LogInformation(msg.ExceptionText, webApiLogMessage);
                    }
                }
            }
        }