Skip to content

Commit

Permalink
Attempt reproduction of #195 (#196)
Browse files Browse the repository at this point in the history
* Attempt reproduction of #195

* Fix for #195, add TraceId and SpanId to log
  • Loading branch information
EEParker authored Apr 19, 2024
1 parent 41ce4db commit a78df28
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DockerfileSolutionRestore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ COPY ["sample/Sample/Sample.csproj", "sample/Sample/"]
COPY ["src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj", "src/Serilog.Sinks.Splunk/"]
COPY ["src/Serilog.Sinks.TCP/Serilog.Sinks.Splunk.TCP.csproj", "src/Serilog.Sinks.TCP/"]
COPY ["src/Serilog.Sinks.UDP/Serilog.Sinks.Splunk.UDP.csproj", "src/Serilog.Sinks.UDP/"]
COPY ["test/Serilog.Sinks.Splunk.TCP.Tests/Serilog.Sinks.Splunk.TCP.Tests.csproj", "test/Serilog.Sinks.Splunk.TCP.Tests/"]
COPY ["test/Serilog.Sinks.Splunk.Tests/Serilog.Sinks.Splunk.Tests.csproj", "test/Serilog.Sinks.Splunk.Tests/"]
COPY ["docker-compose.dcproj", "./"]
COPY ["nuget.config", "./"]
COPY ["serilog-sinks-splunk.sln", "./"]
RUN dotnet restore "serilog-sinks-splunk.sln"

Expand Down
3 changes: 2 additions & 1 deletion sample/Sample/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#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.
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
FROM mcr.microsoft.com/dotnet/aspnet:8.0.4-alpine3.19 AS base
WORKDIR /app
EXPOSE 8080

Expand All @@ -12,6 +12,7 @@ COPY ["sample/Sample/Sample.csproj", "sample/Sample/"]
COPY ["src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj", "src/Serilog.Sinks.Splunk/"]
COPY ["src/Serilog.Sinks.TCP/Serilog.Sinks.Splunk.TCP.csproj", "src/Serilog.Sinks.TCP/"]
COPY ["src/Serilog.Sinks.UDP/Serilog.Sinks.Splunk.UDP.csproj", "src/Serilog.Sinks.UDP/"]
COPY ["test/Serilog.Sinks.Splunk.TCP.Tests/Serilog.Sinks.Splunk.TCP.Tests.csproj", "test/Serilog.Sinks.Splunk.TCP.Tests/"]
COPY ["test/Serilog.Sinks.Splunk.Tests/Serilog.Sinks.Splunk.Tests.csproj", "test/Serilog.Sinks.Splunk.Tests/"]
COPY ["docker-compose.dcproj", "./"]
COPY ["serilog-sinks-splunk.sln", "./"]
Expand Down
54 changes: 53 additions & 1 deletion sample/Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Events;
using Serilog.Exceptions;
using Serilog.Extensions;
using Serilog.Sinks.Splunk;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -55,6 +60,10 @@ public static async Task Main(string[] args)
await Task.Delay(1000);
}


logger.Information("Creating logger {MethodName}.", nameof(ReproduceGitHubIssue195));
ReproduceGitHubIssue195();

logger.Information("Creating logger {MethodName}.", nameof(ReproduceGitHubIssue183));
ReproduceGitHubIssue183();

Expand Down Expand Up @@ -394,6 +403,49 @@ public static void ReproduceGitHubIssue183()

Log.CloseAndFlush();
}

public static void ReproduceGitHubIssue195()
{
var serviceCollection = new ServiceCollection()
.AddLogging(builder => builder.AddSerilog(dispose: true));

serviceCollection.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

var services = serviceCollection.BuildServiceProvider();

// Override Source
var loggerConfig = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.Enrich.WithCorrelationId(addValueIfHeaderAbsence: true)
.MinimumLevel.Verbose()
.WriteTo.EventCollector(
SPLUNK_ENDPOINT,
Program.EventCollectorToken,
restrictedToMinimumLevel: LogEventLevel.Debug);

using (var logger = loggerConfig.CreateLogger())
{
var http = services.GetRequiredService<IHttpContextAccessor>();

http.HttpContext = new DefaultHttpContext();


using var activity = new Activity("TraceIDTest");
activity.Start();
Activity.Current = activity;
http.HttpContext.TraceIdentifier = activity.Id;

Log.Logger = logger;

logger.Information("TraceID Information message {@param}", new { Property1 = 1, Property2 = 2 });
logger.Warning("TraceID Warning message {@param}", "Hello this is a string");
logger.Error(new Exception("Bang"), "TraceID Error message");
activity.Stop();
}

Log.CloseAndFlush();
}
}


Expand Down
7 changes: 6 additions & 1 deletion sample/Sample/Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -7,10 +7,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Serilog.Enrichers.HttpContext" Version="8.0.4" />
<PackageReference Include="Destructurama.Attributed" Version="4.0.0" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 13 additions & 0 deletions src/Serilog.Sinks.Splunk/Sinks/Splunk/SplunkJsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public SplunkJsonFormatter(
// "fields": {"club":"glee", "wins",["regionals","nationals"]}
suffixWriter.Write(",\"fields\": {");
var lastFieldIndex = customFields.CustomFieldList.Count;

foreach (var customField in customFields.CustomFieldList)
{
if (customField.ValueList.Count == 1)
Expand Down Expand Up @@ -194,6 +195,18 @@ public void Format(LogEvent logEvent, TextWriter output)
JsonValueFormatter.WriteQuotedJsonString(logEvent.Exception.ToString(), output);
}

if (logEvent.TraceId != null)
{
output.Write(",\"TraceId\":");
JsonValueFormatter.WriteQuotedJsonString(logEvent.TraceId.ToString()!, output);
}

if (logEvent.SpanId != null)
{
output.Write(",\"SpanId\":");
JsonValueFormatter.WriteQuotedJsonString(logEvent.SpanId.ToString()!, output);
}

if (logEvent.Properties.Count != 0)
WriteProperties(logEvent.Properties, output);

Expand Down

0 comments on commit a78df28

Please sign in to comment.