private static string FormatRequest(RequestProfilerModel model) { var request = model.Context.Request; return($"Http Request Information: {Environment.NewLine}" + $"Scheme: {request.Scheme} {Environment.NewLine}" + $"Host: {request.Host} {Environment.NewLine}" + $"Path: {request.Path} {Environment.NewLine}" + $"QueryString: {request.QueryString} {Environment.NewLine}" + $"Request Body: {model.RequestBody}"); }
/// <summary> /// Invoke the middleware. /// </summary> /// <param name="context">The <see cref="HttpContext"/>.</param> /// <param name="logger">Represents a type used to perform logging.</param> public async Task Invoke(HttpContext context, ILogger <RequestProfilerModel> logger) { var loggingHandler = _requestResponseHandler ?? DefaultLoggingHandler; var model = new RequestProfilerModel(context); await model.SnapRequestBody(); if (await model.NextAndSnapResponceBody(_next, _contentTypes)) { await loggingHandler(logger, model); } }
/// <summary> /// Invoke the middleware. /// </summary> /// <param name="context">The <see cref="HttpContext"/>.</param> /// <param name="logger">Represents a type used to perform logging.</param> public async Task Invoke(HttpContext context, ILogger <RequestProfilerModel> logger) { // Write request body to Application Insights. var loggingHandler = _options.LogHandler ?? DefaultLoggingHandler; var model = new RequestProfilerModel(context); await model.SnapRequestBody(); if (await model.NextAndSnapResponceBody(_next, _options.ContentTypes)) { await loggingHandler(logger, model); } }
/// <summary> /// Default logging handler implementation. Adds two info log entries to the <see cref="ILogger"/>. One for the request and the other for the response. /// </summary> /// <param name="logger">Represents a type used to perform logging.</param> /// <param name="model">Represents a request and response in order to be logged.</param> public static Task DefaultLoggingHandler(ILogger logger, RequestProfilerModel model) { var statusCode = model.HttpContext.Response.StatusCode; if (statusCode >= 500) { logger.LogError(LOG_MESSAGE_FORMAT, model.Duration, model.HostUri, model.RequestTarget, model.StatusCode, model.RequestBody, model.ResponseBody); } else if (statusCode >= 400) { logger.LogWarning(LOG_MESSAGE_FORMAT, model.Duration, model.HostUri, model.RequestTarget, model.StatusCode, model.RequestBody, model.ResponseBody); } else { logger.LogInformation(LOG_MESSAGE_FORMAT, model.Duration, model.HostUri, model.RequestTarget, model.StatusCode, model.RequestBody, model.ResponseBody); } return(Task.CompletedTask); }
/// <summary> /// Invoke the middleware. /// </summary> /// <param name="context">The <see cref="HttpContext"/>.</param> /// <param name="logger">Represents a type used to perform logging.</param> public async Task Invoke(HttpContext context, ILogger <RequestProfilerModel> logger) { var loggingHandler = _requestResponseHandler ?? DefaultLoggingHandler; var model = new RequestProfilerModel { RequestTime = new DateTimeOffset(), Context = context, Request = await FormatRequest(context) }; var originalBody = context.Response.Body; using var newResponseBody = new MemoryStream(); context.Response.Body = newResponseBody; await _next(context); newResponseBody.Seek(0, SeekOrigin.Begin); await newResponseBody.CopyToAsync(originalBody); newResponseBody.Seek(0, SeekOrigin.Begin); model.Response = FormatResponse(context, newResponseBody); model.ResponseTime = new DateTimeOffset(); loggingHandler(logger, model); }
/// <summary> /// Default logging handler implementation. Adds two info log entries to the <see cref="ILogger"/>. One for the request and the other for the response. /// </summary> /// <param name="logger">Represents a type used to perform logging.</param> /// <param name="model">Represents a request and response in order to be logged.</param> public static Task DefaultLoggingHandler(ILogger <RequestProfilerModel> logger, RequestProfilerModel model) { if (model.Context.Response.StatusCode >= 500) { logger.LogError(FormatRequest(model)); logger.LogError(FormatResponse(model)); } else if (model.Context.Response.StatusCode >= 400) { logger.LogWarning(FormatRequest(model)); logger.LogWarning(FormatResponse(model)); } else { logger.LogInformation(FormatRequest(model)); logger.LogInformation(FormatResponse(model)); } return(Task.CompletedTask); }
/// <summary> /// Default logging handler implementation. Adds two info log entries to the <see cref="ILogger"/>. One for the request and the other for the response. /// </summary> /// <param name="logger">Represents a type used to perform logging.</param> /// <param name="model">Represents a request and response in order to be logged.</param> public static void DefaultLoggingHandler(ILogger <RequestProfilerModel> logger, RequestProfilerModel model) { if (model.Context.Response.StatusCode >= 500) { logger.LogError(model.Request); logger.LogError(model.Response); } else if (model.Context.Response.StatusCode >= 400) { logger.LogWarning(model.Request); logger.LogWarning(model.Response); } else { logger.LogInformation(model.Request); logger.LogInformation(model.Response); } }