1+ using System . Reflection ;
12using LogPort . Core . Models ;
23using LogPort . SDK ;
34using Microsoft . Extensions . Logging ;
@@ -6,6 +7,12 @@ namespace LogPort.AspNetCore;
67
78internal class LogPortLogger : ILogger
89{
10+ private static readonly string ? _serviceVersion =
11+ ( Assembly . GetEntryAssembly ( ) ?
12+ . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ?
13+ . InformationalVersion ) ?
14+ . Split ( '+' ) [ 0 ] ;
15+
916 private readonly string _category ;
1017 private readonly LogPortClient _client ;
1118 private readonly string ? _serviceName ;
@@ -31,7 +38,8 @@ public void Log<TState>(
3138 return ;
3239
3340 var message = formatter ( state , exception ) ;
34-
41+ var environment = Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ?? "Production" ;
42+ var hostname = Environment . MachineName ;
3543 var entry = new LogEntry
3644 {
3745 Timestamp = DateTime . UtcNow ,
@@ -41,10 +49,23 @@ public void Log<TState>(
4149 {
4250 { "Category" , _category } ,
4351 { "EventId" , eventId . Id } ,
44- { "EventName" , eventId . Name ?? string . Empty }
52+ { "EventName" , eventId . Name ?? string . Empty } ,
4553 } ,
46- ServiceName = _serviceName
54+ ServiceName = _serviceName ,
55+ Environment = environment ,
56+ Hostname = hostname
4757 } ;
58+ if ( exception is not null )
59+ {
60+ entry . Metadata [ "Exception" ] = exception . ToString ( ) ;
61+ }
62+
63+ if ( ! string . IsNullOrEmpty ( _serviceVersion ) )
64+ {
65+ entry . Metadata [ "Version" ] = _serviceVersion ;
66+ }
67+
68+
4869
4970 _client . Log ( entry ) ;
5071 }
0 commit comments