예제 #1
0
        private async Task FormatResponse(HttpContext context, Stopwatch sw, ILogger logger)
        {
            context.Response.Body.Seek(0, SeekOrigin.Begin);

            context.Request.EnableBuffering();

            var response = await new StreamReader(context.Response.Body).ReadToEndAsync();

            context.Response.Body.Seek(0, SeekOrigin.Begin);

            _customLoggerFactory.Write(LogEventLevel.Debug, _settings.MessageTemplateForResponse, // GetCall
                                       context.Request.Path,
                                       context.Response.StatusCode,
                                       response,
                                       sw.Elapsed.TotalMilliseconds);

            if (_settings.IsSqlServerLog)
            {
                MSSqlDbLog.Invoke(_settings,
                                  context,
                                  null,
                                  _settings.MessageTemplateForResponse,
                                  response,
                                  sw.Elapsed.TotalMilliseconds,
                                  (int)Enum.LogLevel.DebugModeResponse);
            }
        }
예제 #2
0
        private void FormatRequest(HttpContext httpContext, Stopwatch sw,
                                   ILogger logger, LoggerSettings appSettings, MemoryStream injectedRequestStream)
        {
            string bodyAsText;

            using (var bodyReader = new StreamReader(httpContext.Request.Body))
            {
                bodyAsText = bodyReader.ReadToEnd();

                var bytesToWrite = System.Text.Encoding.UTF8.GetBytes(bodyAsText);
                injectedRequestStream.Write(bytesToWrite, 0, bytesToWrite.Length);
                injectedRequestStream.Seek(0, SeekOrigin.Begin);
                httpContext.Request.Body = injectedRequestStream;
            }

            if (!string.IsNullOrEmpty(bodyAsText))
            {
                _customLoggerFactory.Debug(_settings.MessageTemplateForPostRequest,
                                           httpContext.Request.Path,
                                           httpContext.Request.Method,
                                           bodyAsText,
                                           sw.Elapsed.TotalMilliseconds);

                if (_settings.IsSqlServerLog)
                {
                    MSSqlDbLog.Invoke(_settings,
                                      httpContext,
                                      null,
                                      _settings.MessageTemplateForPostRequest,
                                      bodyAsText,
                                      sw.Elapsed.TotalMilliseconds,
                                      (int)Enum.LogLevel.DebugModeRequest);
                }
            }
            else
            {
                _customLoggerFactory.Debug(_settings.MessageTemplateForGetRequest,
                                           httpContext.Request.Path,
                                           httpContext.Request.Method,
                                           sw.Elapsed.TotalMilliseconds);

                if (_settings.IsSqlServerLog)
                {
                    MSSqlDbLog.Invoke(appSettings,
                                      httpContext,
                                      null,
                                      _settings.MessageTemplateForGetRequest,
                                      httpContext.Request.QueryString.Value,
                                      sw.Elapsed.TotalMilliseconds,
                                      (int)Enum.LogLevel.DebugModeRequest);
                }
            }
        }
예제 #3
0
        static void LogVerboseException(HttpContext httpContext, Exception ex, LoggerSettings settings)
        {
            LogForErrorContext(httpContext)
            .Error(ex, settings.VerboseMessageTemplate, ex.Message, (int)CustomErrorCodes.Code.VerboseErrorCode);

            if (settings.IsSqlServerLog)
            {
                MSSqlDbLog.Invoke(settings, httpContext, ex, settings.VerboseMessageTemplate, ex.Message, 0, (int)LogEventLevel.Verbose);
            }

            ResponseWriteAsync(httpContext, ex, (int)CustomErrorCodes.Code.VerboseErrorCode);
        }
예제 #4
0
        private void LogException(HttpContext httpContext, Stopwatch sw, Exception ex, LoggerSettings settings)
        {
            sw.Stop();

            LogForErrorContext(httpContext)
            .Error(ex, settings.MessageTemplate, httpContext.Request.Method, httpContext.Request.Path, 500, sw.Elapsed.TotalMilliseconds);

            if (settings.IsSqlServerLog)
            {
                MSSqlDbLog.Invoke(settings, httpContext, ex, settings.MessageTemplate, ex.Message, sw.Elapsed.TotalMilliseconds, (int)LogEventLevel.Error);
            }

            HandleException(httpContext, ex);
        }