Skip to content

Commit 7eaea1e

Browse files
committed
feat: fix duplicate log
1 parent fd118be commit 7eaea1e

4 files changed

Lines changed: 24 additions & 5 deletions

File tree

src/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,6 @@
156156
<!-- ============================================ -->
157157
<ItemGroup Label="Utilities and Helpers">
158158
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
159+
<PackageVersion Include="Json.Formater" Version="1.0.0" />
159160
</ItemGroup>
160161
</Project>

src/Shared/BuildingBlocks/BuildingBlocks.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" />
88
<PackageReference Include="MediatR" />
99
<PackageReference Include="Microsoft.FeatureManagement.AspNetCore" />
10+
<PackageReference Include="Json.Formater" />
1011
<PackageReference Include="Serilog" />
1112
<PackageReference Include="Serilog.AspNetCore" />
1213
<PackageReference Include="Serilog.Sinks.Console" />

src/Shared/BuildingBlocks/DistributedTracing/DistributedTracingExtension.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using OpenTelemetry.Trace;
1111
using Common.Configurations;
1212
using System.Diagnostics;
13+
using Json.Formater;
1314

1415
#endregion
1516

@@ -38,7 +39,7 @@ public static IServiceCollection AddDistributedTracing(
3839
.AddAttributes(new Dictionary<string, object>
3940
{
4041
["deployment.environment"] = cfg["ASPNETCORE_ENVIRONMENT"] ?? "Production",
41-
["host.name"] = Environment.MachineName
42+
["host.name"] = Environment.MachineName.Format()
4243
}))
4344
.WithTracing(tracingBuilder =>
4445
{
@@ -50,7 +51,7 @@ public static IServiceCollection AddDistributedTracing(
5051
opts.RecordException = true;
5152
opts.EnrichWithException = (activity, exception) =>
5253
{
53-
activity.SetTag("exception.type", exception.GetType().FullName);
54+
activity.SetTag("exception.type", exception.GetType().FullName.Format());
5455
activity.SetTag("exception.message", exception.Message);
5556
};
5657
})

src/Shared/BuildingBlocks/Logging/SerilogLoggingExtensions.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#region using
22

3+
using Common.Configurations;
4+
using Json.Formater;
35
using Microsoft.AspNetCore.Builder;
46
using Microsoft.AspNetCore.Http;
57
using Microsoft.Extensions.Configuration;
@@ -9,7 +11,7 @@
911
using Serilog.Events;
1012
using Serilog.Formatting.Compact;
1113
using Serilog.Sinks.OpenTelemetry;
12-
using Common.Configurations;
14+
using System.Diagnostics;
1315

1416
#endregion
1517

@@ -27,7 +29,7 @@ public static IServiceCollection AddSerilogLogging(
2729
var enable = cfg.GetValue($"{section}:{SerilogCfg.Enable}", false);
2830
if (!enable) return services;
2931

30-
var serviceName = cfg[$"{section}:{SerilogCfg.ServiceName}"] ?? "unknown-service";
32+
var serviceName = cfg[$"{section}:{SerilogCfg.ServiceName}"] ?? AppDomain.CurrentDomain.FriendlyName;
3133
var env = cfg["ASPNETCORE_ENVIRONMENT"] ?? "Production";
3234
var lvlDefault = ParseLevel(cfg[$"{section}:{SerilogCfg.MinimumLevel}:{SerilogCfg.Default}"] ?? "Information");
3335
var lvlMicrosoft = ParseLevel(cfg[$"{section}:{SerilogCfg.MinimumLevel}:{SerilogCfg.Override}:{SerilogCfg.Microsoft}"] ?? "Warning");
@@ -42,7 +44,16 @@ public static IServiceCollection AddSerilogLogging(
4244
.MinimumLevel.Override("System", lvlSystem)
4345
.Enrich.FromLogContext()
4446
.Enrich.WithProperty("service.name", serviceName)
47+
.Enrich.WithProperty("service.version", "1.0.0")
4548
.Enrich.WithProperty("deployment.environment", env)
49+
.Enrich.WithProperty("host.name", Environment.MachineName)
50+
.Enrich.WithProperty("process.id", Environment.ProcessId)
51+
.Enrich.WithProperty("process.name", Process.GetCurrentProcess().ProcessName)
52+
.Enrich.WithProperty("app.domain", AppDomain.CurrentDomain.FriendlyName)
53+
.Enrich.WithProperty("runtime.version", Environment.Version.ToString())
54+
.Enrich.WithProperty("user.name", Environment.UserName)
55+
.Enrich.WithProperty("os.platform", Environment.OSVersion.Platform.ToString())
56+
4657
.Enrich.With<ActivityTraceEnricher>();
4758

4859
if (consoleEnable)
@@ -63,6 +74,10 @@ public static IServiceCollection AddSerilogLogging(
6374
};
6475
});
6576

77+
#region Clear Empty
78+
loggerCfg.Enrich.WithProperty(string.Empty.Format(), string.Empty.BeautyFormat());
79+
#endregion
80+
6681
Log.Logger = loggerCfg.CreateLogger();
6782

6883
services.AddSerilog(logger: Log.Logger, dispose: true);
@@ -100,7 +115,8 @@ public static WebApplication UseSerilogReqLogging(this WebApplication app)
100115
diag.Set("ClientIP", ctx.Connection.RemoteIpAddress?.ToString());
101116
diag.Set("Route", ctx.GetEndpoint()?.DisplayName);
102117
};
103-
options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} -> {StatusCode} in {Elapsed:0.0000} ms";
118+
options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} -> {StatusCode} in {Elapsed:0.0000} ms"
119+
.BeautyFormat();
104120
options.IncludeQueryInRequestPath = false;
105121
});
106122

0 commit comments

Comments
 (0)