11#region using
22
3+ using Common . Configurations ;
4+ using Json . Formater ;
35using Microsoft . AspNetCore . Builder ;
46using Microsoft . AspNetCore . Http ;
57using Microsoft . Extensions . Configuration ;
911using Serilog . Events ;
1012using Serilog . Formatting . Compact ;
1113using 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