2025-08-20 09:10:10 +08:00
|
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
// 作者:Ir0nMax
|
|
|
|
|
|
// 时间:2025/03/05
|
|
|
|
|
|
// 邮箱:ir0nmax@wogof.com
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
namespace Admin.NET.Core;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// http日志处理
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class HttpLoggingHandler : DelegatingHandler, ITransient
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly Dictionary<string, bool> _enabledLogMap;
|
|
|
|
|
|
private readonly SysConfigService _sysConfigService;
|
|
|
|
|
|
private readonly IEventPublisher _eventPublisher;
|
|
|
|
|
|
|
|
|
|
|
|
public HttpLoggingHandler(IEventPublisher eventPublisher, SysConfigService sysConfigService, IOptions<HttpRemotesOptions> options)
|
|
|
|
|
|
{
|
|
|
|
|
|
_eventPublisher = eventPublisher;
|
|
|
|
|
|
HttpRemotesOptions httpRemotesOptions = options.Value;
|
|
|
|
|
|
_sysConfigService = sysConfigService;
|
|
|
|
|
|
_enabledLogMap = typeof(HttpRemotesOptions).GetProperties()
|
|
|
|
|
|
.Where(u => u.PropertyType == typeof(HttpRemoteItem))
|
|
|
|
|
|
.ToDictionary(u => u.GetValue(httpRemotesOptions) is HttpRemoteItem opt ? opt.HttpName : "",
|
|
|
|
|
|
u => u.GetValue(httpRemotesOptions) is HttpRemoteItem { EnabledLog: true });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
|
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 判断全局Http日志开关
|
|
|
|
|
|
var enabledLog = await _sysConfigService.GetConfigValueByCode<bool>(ConfigConst.SysLogHttp);
|
|
|
|
|
|
if (!enabledLog) return await base.SendAsync(request, cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
// 判断当前配置日志开关
|
2025-08-20 19:52:07 +08:00
|
|
|
|
request.Options.TryGetValue<string>(CommonConst.HttpRemoteClientName, out var clientName);
|
2025-08-20 09:10:10 +08:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(clientName)) enabledLog = _enabledLogMap.GetOrDefault(clientName);
|
|
|
|
|
|
if (!enabledLog) return await base.SendAsync(request, cancellationToken);
|
|
|
|
|
|
|
2025-08-24 00:09:42 +08:00
|
|
|
|
var stopWatch = Stopwatch.StartNew();
|
2025-08-20 09:10:10 +08:00
|
|
|
|
var sysLogHttp = new SysLogHttp();
|
2025-08-20 19:52:07 +08:00
|
|
|
|
sysLogHttp.HttpClientName = clientName;
|
|
|
|
|
|
|
|
|
|
|
|
// 获取接口描述,并移除
|
|
|
|
|
|
sysLogHttp.HttpApiDesc = request.Headers.FirstOrDefault(u => u.Key == CommonConst.HttpRemoteApiDescHeaderName).Value?.FirstOrDefault();
|
|
|
|
|
|
request.Headers.Remove(CommonConst.HttpRemoteApiDescHeaderName);
|
|
|
|
|
|
|
2025-08-20 09:10:10 +08:00
|
|
|
|
sysLogHttp.HttpMethod = request.Method.Method;
|
|
|
|
|
|
sysLogHttp.RequestUrl = request.RequestUri?.ToString();
|
|
|
|
|
|
sysLogHttp.RequestHeaders = request.Headers.ToDictionary(u => u.Key, u => u.Value.Join(";")).ToJson();
|
|
|
|
|
|
if (request.Content != null) sysLogHttp.RequestBody = await request.Content.ReadAsStringAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
sysLogHttp.StartTime = DateTime.Now;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var response = await base.SendAsync(request, cancellationToken);
|
|
|
|
|
|
stopWatch.Stop();
|
|
|
|
|
|
sysLogHttp.EndTime = DateTime.Now;
|
2025-08-24 00:09:42 +08:00
|
|
|
|
sysLogHttp.StatusCode = response.StatusCode;
|
2025-08-20 09:10:10 +08:00
|
|
|
|
sysLogHttp.ResponseHeaders = response.Headers.ToDictionary(u => u.Key, u => u.Value.Join(";")).ToJson();
|
|
|
|
|
|
sysLogHttp.IsSuccessStatusCode = response.IsSuccessStatusCode ? YesNoEnum.Y : YesNoEnum.N;
|
|
|
|
|
|
sysLogHttp.ResponseBody = await response.Content.ReadAsStringAsync(cancellationToken);
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
stopWatch.Stop();
|
|
|
|
|
|
sysLogHttp.EndTime = DateTime.Now;
|
|
|
|
|
|
sysLogHttp.IsSuccessStatusCode = YesNoEnum.N;
|
|
|
|
|
|
sysLogHttp.Exception = JSON.Serialize(SerializableException.FromException(ex));
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
sysLogHttp.Elapsed = stopWatch.ElapsedMilliseconds;
|
|
|
|
|
|
await _eventPublisher.PublishAsync(nameof(AppEventSubscriber.CreateHttpLog), sysLogHttp, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-24 00:09:42 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置接口描述相关属性
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="builder"></param>
|
|
|
|
|
|
/// <param name="httpName"></param>
|
|
|
|
|
|
/// <param name="desc"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static HttpRequestBuilder SetRemoteApiAttr(HttpRequestBuilder builder, string httpName, string desc = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
builder.WithHeader(CommonConst.HttpRemoteApiDescHeaderName, desc, replace:true);
|
|
|
|
|
|
builder.SetHttpClientName(httpName);
|
|
|
|
|
|
return builder;
|
|
|
|
|
|
}
|
2025-08-20 09:10:10 +08:00
|
|
|
|
}
|