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> <Project>
<PropertyGroup> <PropertyGroup>
<!-- All Fake packages --> <!-- All Fake packages -->
<FakePackageVersion>8.0.5.3</FakePackageVersion> <FakePackageVersion>8.0.5.6</FakePackageVersion>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

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

View File

@ -6,7 +6,7 @@ using Fake.Autofac;
using Fake.FeiShu; using Fake.FeiShu;
using Fake.Modularity; using Fake.Modularity;
using Fake.Security.Claims; using Fake.Security.Claims;
using Fake.SyncEx; using Fake.Serilog.Sink.FeiShu;
using InterfaceForward.Application; using InterfaceForward.Application;
using InterfaceForward.Domain.Shared; using InterfaceForward.Domain.Shared;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
@ -19,6 +19,7 @@ namespace InterfaceForward.Api;
[DependsOn(typeof(FakeAuthorizationModule))] [DependsOn(typeof(FakeAuthorizationModule))]
[DependsOn(typeof(FakeAutofacModule))] [DependsOn(typeof(FakeAutofacModule))]
[DependsOn(typeof(InterfaceForwardApplicationModule))] [DependsOn(typeof(InterfaceForwardApplicationModule))]
[DependsOn(typeof(FakeSerilogSinkFeiShuModule))]
public class InterfaceForwardApiModule : FakeModule public class InterfaceForwardApiModule : FakeModule
{ {
private const string DefaultCorsPolicyName = "default"; private const string DefaultCorsPolicyName = "default";
@ -123,9 +124,4 @@ public class InterfaceForwardApiModule : FakeModule
app.Services.GetRequiredService<IFeiShuNotificationService>().Send("Application started"); 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; 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设计的意义是 * ServiceProviderRequestLog设计的意义是
@ -50,7 +50,7 @@ public class ForwardExceptionFilter(
} }
else //未处理异常 else //未处理异常
{ {
await logRepository.SendFeiShuAsync(context.Exception); logRepository.SendFeiShu(context.Exception);
res.Code = StatusCodes.Status500InternalServerError.ToString(); res.Code = StatusCodes.Status500InternalServerError.ToString();
res.Msg = "服务器发生未处理异常"; res.Msg = "服务器发生未处理异常";
context.Result = new JsonResult(res); context.Result = new JsonResult(res);
@ -65,6 +65,7 @@ public class ForwardExceptionFilter(
logRepository.AppRequestLog.Response = JsonConvert.SerializeObject(res); logRepository.AppRequestLog.Response = JsonConvert.SerializeObject(res);
_ = logRepository.WriteAppLog(context.Exception); _ = logRepository.WriteAppLog(context.Exception);
context.ExceptionHandled = true; context.ExceptionHandled = true;
return Task.CompletedTask;
} }
private bool NeedPush(ExceptionContext context, out string subscribeName) private bool NeedPush(ExceptionContext context, out string subscribeName)

View File

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

View File

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

View File

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