Skip to content

Commit c79cb2b

Browse files
committed
feat: Include additional context info from ILogger inside logs
1 parent a3ad8ea commit c79cb2b

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

src/LogPort.Agent/LogPort.Agent.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
8+
<Version>1.0.0-nightly</Version>
89
</PropertyGroup>
910

1011
<ItemGroup>

src/LogPort.AspNetCore/LogPortLogger.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Reflection;
12
using LogPort.Core.Models;
23
using LogPort.SDK;
34
using Microsoft.Extensions.Logging;
@@ -6,6 +7,12 @@ namespace LogPort.AspNetCore;
67

78
internal 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
}

src/WebApi/WebApi.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<Version>1.0.0-nightly</Version>
78
</PropertyGroup>
89

910
<ItemGroup>
@@ -13,5 +14,6 @@
1314
<ItemGroup>
1415
<ProjectReference Include="..\LogPort.AspNetCore\LogPort.AspNetCore.csproj" />
1516
</ItemGroup>
17+
1618

1719
</Project>

0 commit comments

Comments
 (0)