Skip to content

Commit 95a7383

Browse files
committed
fix: 兼容2种日志输出
1 parent 7a52f69 commit 95a7383

File tree

6 files changed

+59
-6
lines changed

6 files changed

+59
-6
lines changed

framework/src/Bing.Logging.Sinks.Exceptionless/Bing.Logging.Sinks.Exceptionless.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
<Import Project="project.dependency.props" />
55

66
<Import Project="..\..\..\framework.props" />
7+
8+
<ItemGroup>
9+
<PackageReference Include="Serilog.Sinks.Exceptionless" Version="3.1.5" />
10+
</ItemGroup>
711
</Project>

modules/admin/src/Bing.Admin/Apis/TestController.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ public async Task<IActionResult> TestDisposed()
8787
[HttpPost("testMessage")]
8888
public async Task<IActionResult> TestMessageAsync([FromBody] TestMessage request)
8989
{
90-
await MessageEventBus.PublishAsync(new TestMessageEvent1(request));
91-
await UnitOfWork.CommitAsync();
90+
Log.Info("测试日志消息Begin");
91+
await MessageEventBus.PublishAsync(new TestMessageEvent1(request, request.Send));
92+
if (request.NeedCommit)
93+
await UnitOfWork.CommitAsync();
94+
Log.Info("测试日志消息End");
9295
return Success();
9396
}
9497

modules/admin/src/Bing.Admin/Bing.Admin.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25+
<ProjectReference Include="..\..\..\..\framework\src\Bing.Logging.Serilog\Bing.Logging.Serilog.csproj" />
26+
<ProjectReference Include="..\..\..\..\framework\src\Bing.Logging.Sinks.Exceptionless\Bing.Logging.Sinks.Exceptionless.csproj" />
2527
<ProjectReference Include="..\Bing.Admin.Data.EFCore\Bing.Admin.Data.EFCore.csproj" />
2628
<ProjectReference Include="..\Bing.Admin.EventHandlers\Bing.Admin.EventHandlers.csproj" />
2729
<ProjectReference Include="..\Bing.Admin.Service\Bing.Admin.Service.csproj" />
2830
</ItemGroup>
2931

32+
<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>
33+
3034
</Project>
3135

modules/admin/src/Bing.Admin/Modules/AppModule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public override IServiceCollection AddServices(IServiceCollection services)
4545
o.Filters.Add<BingExceptionFilter>();
4646
o.Conventions.Add(new AuthorizeControllerModelConvention());
4747
})
48+
.AddControllersAsServices()// 解决属性注入无法在控制器中注入的问题
4849
.AddNewtonsoftJson(options =>
4950
{
5051
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";

modules/admin/src/Bing.Admin/Modules/LogModule.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using System.ComponentModel;
22
using Bing.AspNetCore;
33
using Bing.Core.Modularity;
4-
using Bing.Logs.Exceptionless;
4+
using Bing.Logging.Serilog;
55
using Bing.Logs.NLog;
6+
using Exceptionless;
67
using Microsoft.Extensions.DependencyInjection;
8+
using Serilog;
9+
using serilog = Serilog;
710

811
namespace Bing.Admin.Modules
912
{
@@ -24,19 +27,36 @@ public class LogModule : AspNetCoreBingModule
2427
/// 级别默认为0,表示无依赖,需要在同级别有依赖顺序的时候,再重写为>0的顺序值
2528
/// </summary>
2629
public override int Order => 0;
27-
30+
2831
/// <summary>
2932
/// 添加服务。将模块服务添加到依赖注入服务容器中
3033
/// </summary>
3134
/// <param name="services">服务集合</param>
3235
public override IServiceCollection AddServices(IServiceCollection services)
3336
{
3437
//services.AddNLog();
35-
services.AddExceptionless(o =>
38+
// 同时输出2种方式的日志,可能存在重复 需要陆续兼容
39+
Logs.Exceptionless.Extensions.AddExceptionless(services, o =>
3640
{
3741
o.ApiKey = "ez9jumyxVxjTxqSm0oUQhCML3OGCkDfMGyW1hfmn";
3842
o.ServerUrl = "http://10.186.132.40:5100";
3943
});
44+
//ExceptionlessClient.Default.Configuration.ApiKey= "ez9jumyxVxjTxqSm0oUQhCML3OGCkDfMGyW1hfmn";
45+
//ExceptionlessClient.Default.Configuration.ServerUrl = "http://10.186.132.40:5100";
46+
//ExceptionlessClient.Default.Startup();
47+
services.AddLogging(loggingBuilder =>
48+
{
49+
var configuration = services.GetConfiguration();
50+
serilog.Log.Logger = new serilog.LoggerConfiguration()
51+
.Enrich.FromLogContext()
52+
.Enrich.WithLogContext()
53+
.Enrich.WithLogLevel()
54+
.WriteTo.Exceptionless()
55+
.ReadFrom.Configuration(configuration)
56+
.ConfigLogLevel(configuration)
57+
.CreateLogger();
58+
loggingBuilder.AddSerilog();
59+
});
4060
return services;
4161
}
4262
}

modules/admin/src/Bing.Admin/appsettings.json

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,29 @@
1616
"Logging": {
1717
"LogLevel": {
1818
"Default": "Debug",
19-
"Microsoft": "Warning"
19+
"Microsoft": "Warning"
2020
}
2121
},
22+
"Serilog": {
23+
"WriteTo": [
24+
{
25+
"Name": "File",
26+
"Args": {
27+
"path": "d:\\bing.admin\\log.txt",
28+
"rollingInterval": "Day",
29+
"rollOnFileSizeLimit": true,
30+
"fileSizeLimitBytes": 102400,
31+
"retainedFileCountLimit": 10,
32+
"outputTemplate": "{LogLevel}: {TraceId} >> [{SourceContext}] [{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Duration}]{NewLine}{Message}{NewLine}{Exception}"
33+
}
34+
},
35+
{
36+
"Name": "Seq",
37+
"Args": {
38+
"serverUrl": "http://seq.a.com:5341"
39+
}
40+
}
41+
]
42+
},
2243
"AllowedHosts": "*"
2344
}

0 commit comments

Comments
 (0)