|
|
@ -1,16 +1,11 @@
|
|
|
|
using System;
|
|
|
|
using EntrustSettle.Common;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
using EntrustSettle.Common;
|
|
|
|
|
|
|
|
using EntrustSettle.Common.Extensions;
|
|
|
|
using EntrustSettle.Common.Extensions;
|
|
|
|
using EntrustSettle.Common.LogHelper;
|
|
|
|
using EntrustSettle.Common.LogHelper;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System.IO;
|
|
|
|
using Org.BouncyCastle.Asn1.Ocsp;
|
|
|
|
using System.Linq;
|
|
|
|
using Polly;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace EntrustSettle.Extensions.Middlewares
|
|
|
|
namespace EntrustSettle.Extensions.Middlewares
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -43,33 +38,36 @@ namespace EntrustSettle.Extensions.Middlewares
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 判断如果请求或响应中含有文件上传,不记录日志
|
|
|
|
var path = context.Request.Path.ToString();
|
|
|
|
if (context.Request.HasFormContentType && context.Request.Form.Files.Count > 0)
|
|
|
|
if (path.Contains("index.html") || path.Contains("swagger.json"))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await _next(context);
|
|
|
|
await _next(context);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断如果请求或响应中含有文件上传,不记录日志
|
|
|
|
|
|
|
|
//if (context.Request.HasFormContentType && context.Request.Form.Files.Count > 0)
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// await _next(context);
|
|
|
|
|
|
|
|
// return;
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
// 过滤,只有接口
|
|
|
|
// 过滤,只有接口
|
|
|
|
var ignoreApis = AppSettings.app("Middleware", "RequestResponseLog", "IgnoreApis");
|
|
|
|
var ignoreRequestApis = AppSettings.app("Middleware", "RequestResponseLog", "IgnoreRequestApis").Split(',').ToList();
|
|
|
|
var api = context.Request.Path.ObjToString().TrimEnd('/');
|
|
|
|
var ignoreResponseApis = AppSettings.app("Middleware", "RequestResponseLog", "IgnoreResponseApis").Split(',').ToList();
|
|
|
|
if (!api.Contains("api", StringComparison.OrdinalIgnoreCase) || ignoreApis.Contains(api, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
await _next(context);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
context.Request.EnableBuffering();
|
|
|
|
context.Request.EnableBuffering();
|
|
|
|
|
|
|
|
|
|
|
|
// 存储请求数据
|
|
|
|
// 存储请求数据
|
|
|
|
await RequestDataLog(context);
|
|
|
|
await RequestDataLog(context, ignoreRequestApis.Contains(path));
|
|
|
|
|
|
|
|
|
|
|
|
await _next(context);
|
|
|
|
await _next(context);
|
|
|
|
|
|
|
|
|
|
|
|
// 存储响应数据
|
|
|
|
// 存储响应数据
|
|
|
|
ResponseDataLog(context.Response);
|
|
|
|
ResponseDataLog(context, ignoreResponseApis.Contains(path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task RequestDataLog(HttpContext context)
|
|
|
|
private async Task RequestDataLog(HttpContext context, bool isIgnore)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var request = context.Request;
|
|
|
|
var request = context.Request;
|
|
|
|
var sr = new StreamReader(request.Body);
|
|
|
|
var sr = new StreamReader(request.Body);
|
|
|
@ -77,10 +75,10 @@ namespace EntrustSettle.Extensions.Middlewares
|
|
|
|
|
|
|
|
|
|
|
|
var logContent = new string[]
|
|
|
|
var logContent = new string[]
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$"●请求",
|
|
|
|
$"● 请求",
|
|
|
|
$"[Path]:{request.Path}",
|
|
|
|
$"[Path]:{request.Path}",
|
|
|
|
$"[QueryString]:{request.QueryString}",
|
|
|
|
$"[QueryString]:{(isIgnore ? "略" : request.QueryString)}",
|
|
|
|
$"[Body]:{bodyData}"
|
|
|
|
$"[Body]:{(isIgnore ? "略" : bodyData)}"
|
|
|
|
};
|
|
|
|
};
|
|
|
|
Parallel.For(0, 1, e =>
|
|
|
|
Parallel.For(0, 1, e =>
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -92,50 +90,31 @@ namespace EntrustSettle.Extensions.Middlewares
|
|
|
|
request.Body.Position = 0;
|
|
|
|
request.Body.Position = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ResponseDataLog(HttpResponse response)
|
|
|
|
private void ResponseDataLog(HttpContext context, bool isIgnore)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var responseBody = response.GetResponseBody();
|
|
|
|
var responseBody = context.Response.GetResponseBody();
|
|
|
|
|
|
|
|
|
|
|
|
// 去除 Html
|
|
|
|
// 去除 Html
|
|
|
|
//var reg = "<[^>]+>";
|
|
|
|
//var reg = "<[^>]+>";
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(responseBody))
|
|
|
|
//if (!string.IsNullOrEmpty(responseBody))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//var isHtml = Regex.IsMatch(responseBody, reg);
|
|
|
|
//var isHtml = Regex.IsMatch(responseBody, reg);
|
|
|
|
|
|
|
|
|
|
|
|
var logContent = new string[]
|
|
|
|
var logContent = new string[]
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$"●响应",
|
|
|
|
$"● 响应",
|
|
|
|
$"[StatusCode]:{response.StatusCode}",
|
|
|
|
$"[Path]:{context.Request.Path}",
|
|
|
|
$"[Body]:{responseBody}",
|
|
|
|
$"[StatusCode]:{context.Response.StatusCode}",
|
|
|
|
|
|
|
|
$"[Body]:{(isIgnore? "略" : responseBody )}",
|
|
|
|
};
|
|
|
|
};
|
|
|
|
Parallel.For(0, 1, e =>
|
|
|
|
Parallel.For(0, 1, e =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LogLock.OutLogAOP("RequestResponseLog",
|
|
|
|
LogLock.OutLogAOP("RequestResponseLog",
|
|
|
|
response.HttpContext.TraceIdentifier,
|
|
|
|
context.TraceIdentifier,
|
|
|
|
logContent);
|
|
|
|
logContent);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ResponseDataLog(HttpResponse response, MemoryStream ms)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ms.Position = 0;
|
|
|
|
|
|
|
|
var responseBody = new StreamReader(ms).ReadToEnd();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 去除 Html
|
|
|
|
|
|
|
|
var reg = "<[^>]+>";
|
|
|
|
|
|
|
|
var isHtml = Regex.IsMatch(responseBody, reg);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(responseBody))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Parallel.For(0, 1, e =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//LogLock.OutSql2Log("RequestResponseLog", new string[] { "Response Data:", ResponseBody });
|
|
|
|
|
|
|
|
LogLock.OutLogAOP("RequestResponseLog", response.HttpContext.TraceIdentifier,
|
|
|
|
|
|
|
|
new string[] { "Response Data - ResponseJsonDataType:" + responseBody.GetType().ToString(), responseBody });
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|