Skip to content

Commit a78df28

Browse files
authored
Attempt reproduction of #195 (#196)
* Attempt reproduction of #195 * Fix for #195, add TraceId and SpanId to log
1 parent 41ce4db commit a78df28

File tree

5 files changed

+75
-4
lines changed

5 files changed

+75
-4
lines changed

DockerfileSolutionRestore.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ COPY ["sample/Sample/Sample.csproj", "sample/Sample/"]
44
COPY ["src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj", "src/Serilog.Sinks.Splunk/"]
55
COPY ["src/Serilog.Sinks.TCP/Serilog.Sinks.Splunk.TCP.csproj", "src/Serilog.Sinks.TCP/"]
66
COPY ["src/Serilog.Sinks.UDP/Serilog.Sinks.Splunk.UDP.csproj", "src/Serilog.Sinks.UDP/"]
7+
COPY ["test/Serilog.Sinks.Splunk.TCP.Tests/Serilog.Sinks.Splunk.TCP.Tests.csproj", "test/Serilog.Sinks.Splunk.TCP.Tests/"]
78
COPY ["test/Serilog.Sinks.Splunk.Tests/Serilog.Sinks.Splunk.Tests.csproj", "test/Serilog.Sinks.Splunk.Tests/"]
89
COPY ["docker-compose.dcproj", "./"]
9-
COPY ["nuget.config", "./"]
1010
COPY ["serilog-sinks-splunk.sln", "./"]
1111
RUN dotnet restore "serilog-sinks-splunk.sln"
1212

sample/Sample/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
2-
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
2+
FROM mcr.microsoft.com/dotnet/aspnet:8.0.4-alpine3.19 AS base
33
WORKDIR /app
44
EXPOSE 8080
55

@@ -12,6 +12,7 @@ COPY ["sample/Sample/Sample.csproj", "sample/Sample/"]
1212
COPY ["src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj", "src/Serilog.Sinks.Splunk/"]
1313
COPY ["src/Serilog.Sinks.TCP/Serilog.Sinks.Splunk.TCP.csproj", "src/Serilog.Sinks.TCP/"]
1414
COPY ["src/Serilog.Sinks.UDP/Serilog.Sinks.Splunk.UDP.csproj", "src/Serilog.Sinks.UDP/"]
15+
COPY ["test/Serilog.Sinks.Splunk.TCP.Tests/Serilog.Sinks.Splunk.TCP.Tests.csproj", "test/Serilog.Sinks.Splunk.TCP.Tests/"]
1516
COPY ["test/Serilog.Sinks.Splunk.Tests/Serilog.Sinks.Splunk.Tests.csproj", "test/Serilog.Sinks.Splunk.Tests/"]
1617
COPY ["docker-compose.dcproj", "./"]
1718
COPY ["serilog-sinks-splunk.sln", "./"]

sample/Sample/Program.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
using Microsoft.Extensions.Configuration;
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
24
using Serilog;
35
using Serilog.Events;
6+
using Serilog.Exceptions;
7+
using Serilog.Extensions;
48
using Serilog.Sinks.Splunk;
59
using System;
610
using System.Collections.Generic;
11+
using System.Diagnostics;
712
using System.IO;
813
using System.Linq;
914
using System.Threading.Tasks;
@@ -55,6 +60,10 @@ public static async Task Main(string[] args)
5560
await Task.Delay(1000);
5661
}
5762

63+
64+
logger.Information("Creating logger {MethodName}.", nameof(ReproduceGitHubIssue195));
65+
ReproduceGitHubIssue195();
66+
5867
logger.Information("Creating logger {MethodName}.", nameof(ReproduceGitHubIssue183));
5968
ReproduceGitHubIssue183();
6069

@@ -394,6 +403,49 @@ public static void ReproduceGitHubIssue183()
394403

395404
Log.CloseAndFlush();
396405
}
406+
407+
public static void ReproduceGitHubIssue195()
408+
{
409+
var serviceCollection = new ServiceCollection()
410+
.AddLogging(builder => builder.AddSerilog(dispose: true));
411+
412+
serviceCollection.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
413+
414+
var services = serviceCollection.BuildServiceProvider();
415+
416+
// Override Source
417+
var loggerConfig = new LoggerConfiguration()
418+
.Enrich.FromLogContext()
419+
.Enrich.WithExceptionDetails()
420+
.Enrich.WithCorrelationId(addValueIfHeaderAbsence: true)
421+
.MinimumLevel.Verbose()
422+
.WriteTo.EventCollector(
423+
SPLUNK_ENDPOINT,
424+
Program.EventCollectorToken,
425+
restrictedToMinimumLevel: LogEventLevel.Debug);
426+
427+
using (var logger = loggerConfig.CreateLogger())
428+
{
429+
var http = services.GetRequiredService<IHttpContextAccessor>();
430+
431+
http.HttpContext = new DefaultHttpContext();
432+
433+
434+
using var activity = new Activity("TraceIDTest");
435+
activity.Start();
436+
Activity.Current = activity;
437+
http.HttpContext.TraceIdentifier = activity.Id;
438+
439+
Log.Logger = logger;
440+
441+
logger.Information("TraceID Information message {@param}", new { Property1 = 1, Property2 = 2 });
442+
logger.Warning("TraceID Warning message {@param}", "Hello this is a string");
443+
logger.Error(new Exception("Bang"), "TraceID Error message");
444+
activity.Stop();
445+
}
446+
447+
Log.CloseAndFlush();
448+
}
397449
}
398450

399451

sample/Sample/Sample.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
@@ -7,10 +7,15 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
1011
<PackageReference Include="Serilog" Version="3.1.1" />
12+
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
1113
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
1214
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
1315
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
16+
<PackageReference Include="Serilog.Enrichers.HttpContext" Version="8.0.4" />
17+
<PackageReference Include="Destructurama.Attributed" Version="4.0.0" />
18+
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
1419
</ItemGroup>
1520

1621
<ItemGroup>

src/Serilog.Sinks.Splunk/Sinks/Splunk/SplunkJsonFormatter.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public SplunkJsonFormatter(
135135
// "fields": {"club":"glee", "wins",["regionals","nationals"]}
136136
suffixWriter.Write(",\"fields\": {");
137137
var lastFieldIndex = customFields.CustomFieldList.Count;
138+
138139
foreach (var customField in customFields.CustomFieldList)
139140
{
140141
if (customField.ValueList.Count == 1)
@@ -194,6 +195,18 @@ public void Format(LogEvent logEvent, TextWriter output)
194195
JsonValueFormatter.WriteQuotedJsonString(logEvent.Exception.ToString(), output);
195196
}
196197

198+
if (logEvent.TraceId != null)
199+
{
200+
output.Write(",\"TraceId\":");
201+
JsonValueFormatter.WriteQuotedJsonString(logEvent.TraceId.ToString()!, output);
202+
}
203+
204+
if (logEvent.SpanId != null)
205+
{
206+
output.Write(",\"SpanId\":");
207+
JsonValueFormatter.WriteQuotedJsonString(logEvent.SpanId.ToString()!, output);
208+
}
209+
197210
if (logEvent.Properties.Count != 0)
198211
WriteProperties(logEvent.Properties, output);
199212

0 commit comments

Comments
 (0)