¶Ô±ÈÐÂÎļþ |
| | |
| | | using Microsoft.AspNetCore.Http; |
| | | using Microsoft.Extensions.Logging; |
| | | using Newtonsoft.Json; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.Helper; |
| | | using WIDESEA_Core.LogHelper; |
| | | |
| | | namespace WIDESEA_Core.Middlewares |
| | | { |
| | | /// <summary> |
| | | /// è®°å½è¯·æ±åååºæ°æ® |
| | | /// </summary> |
| | | public class ApiLogMiddleware |
| | | { |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | private readonly RequestDelegate _next; |
| | | private readonly ILogger<ApiLogMiddleware> _logger; |
| | | |
| | | public ApiLogMiddleware(RequestDelegate next, ILogger<ApiLogMiddleware> logger) |
| | | { |
| | | _next = next; |
| | | _logger = logger; |
| | | } |
| | | |
| | | //todo |
| | | public async Task InvokeAsync(HttpContext context) |
| | | { |
| | | //if (AppSettings.app("Middleware", "RequestResponseLog", "Enabled").ObjToBool()) |
| | | { |
| | | // è¿æ»¤ï¼åªææ¥å£ |
| | | if (context.Request.Path.Value.Contains("api")) |
| | | { |
| | | context.Request.EnableBuffering(); |
| | | //Stream originalBody = context.Response.Body; |
| | | |
| | | try |
| | | { |
| | | // åå¨è¯·æ±æ°æ® |
| | | //string requestParam = GetRequestData(context); |
| | | //DateTime beginDate = DateTime.Now; |
| | | |
| | | //using var ms = new MemoryStream(); |
| | | //context.Response.Body = ms; |
| | | |
| | | await _next(context); |
| | | |
| | | // åå¨ååºæ°æ® |
| | | //DateTime endDate = DateTime.Now; |
| | | //string responseParam = GetResponsetData(context); |
| | | |
| | | //context.Response.Body.Position = 0; |
| | | //await context.Response.Body.CopyToAsync(originalBody); |
| | | |
| | | //Logger.WriteApiLog2DB(context,requestParam, beginDate, responseParam, endDate, context.Response.StatusCode == 200 ? LoggerStatus.Success : LoggerStatus.Error); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // è®°å½å¼å¸¸ |
| | | _logger.LogError(ex.Message + "" + ex.InnerException); |
| | | } |
| | | finally |
| | | { |
| | | //context.Response.Body = originalBody; |
| | | } |
| | | } |
| | | //else |
| | | //{ |
| | | // await _next(context); |
| | | //} |
| | | } |
| | | //else |
| | | //{ |
| | | // await _next(context); |
| | | //} |
| | | } |
| | | |
| | | private string GetRequestData(HttpContext context) |
| | | { |
| | | try |
| | | { |
| | | using StreamReader sr = new StreamReader(context.Request.Body); |
| | | string request = JsonConvert.SerializeObject(sr.ReadToEnd()); ; |
| | | context.Request.Body.Position = 0; |
| | | return request; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return $"请æ±åæ°è·åé误,{ex.Message}"; |
| | | } |
| | | } |
| | | |
| | | private string GetResponsetData(HttpContext context) |
| | | { |
| | | try |
| | | { |
| | | using StreamReader sr = new StreamReader(context.Response.Body); |
| | | string response = JsonConvert.SerializeObject(sr.ReadToEnd()); ; |
| | | context.Response.Body.Position = 0; |
| | | return response; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return $"ååºåæ°è·åé误,{ex.Message}"; |
| | | } |
| | | } |
| | | } |
| | | } |