This commit is contained in:
xiaolipro 2026-02-26 15:04:06 +08:00
parent e4de1ba222
commit 15e562e65c
8 changed files with 33 additions and 47 deletions

View File

@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<!-- All Fake packages -->
<FakePackageVersion>8.0.5.3</FakePackageVersion>
<FakePackageVersion>8.0.5.6</FakePackageVersion>
</PropertyGroup>
</Project>

View File

@ -10,19 +10,11 @@
<ItemGroup>
<ProjectReference Include="..\InterfaceForward.Application\InterfaceForward.Application.csproj" />
<ProjectReference Include="..\InterfaceForward.ServiceDefaults\InterfaceForward.ServiceDefaults.csproj"/>
<PackageReference Include="Fake.Autofac" Version="$(FakePackageVersion)" />
<PackageReference Include="Fake.Consul" Version="$(FakePackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.21" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.18" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="10.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
</ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<ItemGroup>
<Content Include="..\..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
<PackageReference Include="Fake.Consul" Version="$(FakePackageVersion)" />
<PackageReference Include="Fake.Autofac" Version="$(FakePackageVersion)" />
</ItemGroup>
</Project>

View File

@ -6,7 +6,7 @@ using Fake.Autofac;
using Fake.FeiShu;
using Fake.Modularity;
using Fake.Security.Claims;
using Fake.SyncEx;
using Fake.Serilog.Sink.FeiShu;
using InterfaceForward.Application;
using InterfaceForward.Domain.Shared;
using Microsoft.AspNetCore.Authorization;
@ -19,6 +19,7 @@ namespace InterfaceForward.Api;
[DependsOn(typeof(FakeAuthorizationModule))]
[DependsOn(typeof(FakeAutofacModule))]
[DependsOn(typeof(InterfaceForwardApplicationModule))]
[DependsOn(typeof(FakeSerilogSinkFeiShuModule))]
public class InterfaceForwardApiModule : FakeModule
{
private const string DefaultCorsPolicyName = "default";
@ -123,9 +124,4 @@ public class InterfaceForwardApiModule : FakeModule
app.Services.GetRequiredService<IFeiShuNotificationService>().Send("Application started");
}
public override void Shutdown(ApplicationShutdownContext context)
{
context.ServiceProvider.GetRequiredService<IFeiShuNotificationService>().Send("Application stoped");
}
}

View File

@ -17,9 +17,9 @@ public class ForwardExceptionFilter(
{
private readonly InterfaceRelayOptions _options = options.Value;
public async Task OnExceptionAsync(ExceptionContext context)
public Task OnExceptionAsync(ExceptionContext context)
{
if (context.ExceptionHandled) return;
if (context.ExceptionHandled) return Task.CompletedTask;
/*
* ServiceProviderRequestLog设计的意义是
@ -50,7 +50,7 @@ public class ForwardExceptionFilter(
}
else //未处理异常
{
await logRepository.SendFeiShuAsync(context.Exception);
logRepository.SendFeiShu(context.Exception);
res.Code = StatusCodes.Status500InternalServerError.ToString();
res.Msg = "服务器发生未处理异常";
context.Result = new JsonResult(res);
@ -65,6 +65,7 @@ public class ForwardExceptionFilter(
logRepository.AppRequestLog.Response = JsonConvert.SerializeObject(res);
_ = logRepository.WriteAppLog(context.Exception);
context.ExceptionHandled = true;
return Task.CompletedTask;
}
private bool NeedPush(ExceptionContext context, out string subscribeName)

View File

@ -17,9 +17,9 @@ public class ShopeeForwardExceptionFilter(
{
private readonly InterfaceRelayOptions _options = options.Value;
public async Task OnExceptionAsync(ExceptionContext context)
public Task OnExceptionAsync(ExceptionContext context)
{
if (context.ExceptionHandled) return;
if (context.ExceptionHandled) return Task.CompletedTask;
/*
* ServiceProviderRequestLog设计的意义是
@ -50,7 +50,7 @@ public class ShopeeForwardExceptionFilter(
}
else //未处理异常
{
await logRepository.SendFeiShuAsync(context.Exception);
logRepository.SendFeiShu(context.Exception);
res.Code = StatusCodes.Status500InternalServerError.ToString();
res.Msg = "服务器发生未处理异常";
context.Result = new JsonResult(res);
@ -65,6 +65,7 @@ public class ShopeeForwardExceptionFilter(
logRepository.AppRequestLog.Response = JsonConvert.SerializeObject(res);
_ = logRepository.WriteAppLog(context.Exception);
context.ExceptionHandled = true;
return Task.CompletedTask;
}
private bool NeedPush(ExceptionContext context, out string subscribeName)

View File

@ -6,11 +6,11 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="Serilog" Version="4.2.1-dev-02337" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Fake.Serilog" Version="$(FakePackageVersion)" />
<PackageReference Include="Fake.DomainDrivenDesign" Version="$(FakePackageVersion)" />
</ItemGroup>
</Project>

View File

@ -40,7 +40,7 @@ namespace InterfaceForward.Repositories.Log.Services
/// 发飞书
/// </summary>
/// <param name="contextException"></param>
Task SendFeiShuAsync(Exception contextException);
void SendFeiShu(Exception contextException);
/// <summary>
/// 添加日志

View File

@ -1,5 +1,4 @@
using Fake.FeiShu;
using InterfaceForward.Domain.Shared.Dtos;
using InterfaceForward.Domain.Shared.Dtos;
using InterfaceForward.Domain.Shared.Enum;
using InterfaceForward.Domain.Shared.Helpers;
using InterfaceForward.Repositories.Log.Entitys;
@ -15,16 +14,13 @@ namespace InterfaceForward.Repositories.Log.Services;
/// </summary>
public class LogRepository : ElasticSearchContextAbstract<LogIndex>, ILogRepository, IScopedDependency
{
private readonly IFeiShuNotificationService _feiShuNotificationService;
public LogRepository(ConnectionFactory connection, IFeiShuNotificationService feiShuNotificationService) :
public LogRepository(ConnectionFactory connection) :
base(connection)
{
var guid = Guid.NewGuid();
AppRequestLog = new RequestLogDto(guid);
AppRequestLog.IsSuccess = true; // 只有接口通发生未处理异常才算应用失败
ServiceProviderRequestLog = new RequestLogDto(guid);
_feiShuNotificationService = feiShuNotificationService;
}
public int AppId { get; set; }
@ -44,20 +40,20 @@ public class LogRepository : ElasticSearchContextAbstract<LogIndex>, ILogReposit
await AddLogAsync(AppRequestLog);
}
public async Task SendFeiShuAsync(Exception contextException)
public void SendFeiShu(Exception contextException)
{
await _feiShuNotificationService.SendAsync($"""
id{ServiceProviderRequestLog.RequestId}
--{ServiceProviderRequestLog.Name}--{ServiceProviderRequestLog.InterfaceName}
{Truncate(contextException.ToString())}
{ServiceProviderRequestLog.Address}
{Truncate(AppRequestLog.Content)}
{Truncate(ServiceProviderRequestLog.Content)}
{Truncate(ServiceProviderRequestLog.Response)}
""", LogLevel.Error, CancellationToken.None);
Serilog.Log.Error($"""
id{ServiceProviderRequestLog.RequestId}
--{ServiceProviderRequestLog.Name}--{ServiceProviderRequestLog.InterfaceName}
{Truncate(contextException.ToString())}
{ServiceProviderRequestLog.Address}
{Truncate(AppRequestLog.Content)}
{Truncate(ServiceProviderRequestLog.Content)}
{Truncate(ServiceProviderRequestLog.Response)}
""");
}
@ -90,11 +86,11 @@ public class LogRepository : ElasticSearchContextAbstract<LogIndex>, ILogReposit
IndexResponse response = await Context.IndexAsync(idx,
request => request.Index($"interface_forward-{DateTime.Now:yyyy.MM.dd}"));
if (!response.IsValid)
await _feiShuNotificationService.SendAsync("数据添加失败,原因:" + response.DebugInformation, LogLevel.Error, CancellationToken.None);
Serilog.Log.Error($"数据添加失败,原因:{response.DebugInformation}\n {idx.ToJson()}");
}
catch (Exception ex)
{
await _feiShuNotificationService.SendAsync($"日志插入ES失败{ex.Message}。{idx.ToJson()}", LogLevel.Error, CancellationToken.None);
Serilog.Log.Error($"日志插入ES失败{ex.Message}。{idx.ToJson()}");
}
}