From a78df28743396d8fed8c84554f66f46492d11226 Mon Sep 17 00:00:00 2001 From: "Jeff Parker, PE" Date: Fri, 19 Apr 2024 10:52:03 -0500 Subject: [PATCH] Attempt reproduction of #195 (#196) * Attempt reproduction of #195 * Fix for #195, add TraceId and SpanId to log --- DockerfileSolutionRestore.txt | 2 +- sample/Sample/Dockerfile | 3 +- sample/Sample/Program.cs | 54 ++++++++++++++++++- sample/Sample/Sample.csproj | 7 ++- .../Sinks/Splunk/SplunkJsonFormatter.cs | 13 +++++ 5 files changed, 75 insertions(+), 4 deletions(-) diff --git a/DockerfileSolutionRestore.txt b/DockerfileSolutionRestore.txt index a783374..91593ab 100644 --- a/DockerfileSolutionRestore.txt +++ b/DockerfileSolutionRestore.txt @@ -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" diff --git a/sample/Sample/Dockerfile b/sample/Sample/Dockerfile index a23ba1c..be545f7 100644 --- a/sample/Sample/Dockerfile +++ b/sample/Sample/Dockerfile @@ -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 @@ -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", "./"] diff --git a/sample/Sample/Program.cs b/sample/Sample/Program.cs index 81cf284..a07a45d 100644 --- a/sample/Sample/Program.cs +++ b/sample/Sample/Program.cs @@ -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; @@ -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(); @@ -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(); + + 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(); + + 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(); + } } diff --git a/sample/Sample/Sample.csproj b/sample/Sample/Sample.csproj index abc37a6..57b6a1f 100644 --- a/sample/Sample/Sample.csproj +++ b/sample/Sample/Sample.csproj @@ -1,4 +1,4 @@ - + Exe @@ -7,10 +7,15 @@ + + + + + diff --git a/src/Serilog.Sinks.Splunk/Sinks/Splunk/SplunkJsonFormatter.cs b/src/Serilog.Sinks.Splunk/Sinks/Splunk/SplunkJsonFormatter.cs index 0ef53c5..ef03c47 100644 --- a/src/Serilog.Sinks.Splunk/Sinks/Splunk/SplunkJsonFormatter.cs +++ b/src/Serilog.Sinks.Splunk/Sinks/Splunk/SplunkJsonFormatter.cs @@ -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) @@ -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);