Skip to content

Commit

Permalink
Collect trace and span ids in request completion events
Browse files Browse the repository at this point in the history
  • Loading branch information
nblumhardt committed Oct 11, 2023
1 parent 5ca0d90 commit f1c1bd7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class RequestLoggingMiddleware
readonly Func<HttpContext, string, double, int, IEnumerable<LogEventProperty>> _getMessageTemplateProperties;
readonly ILogger? _logger;
readonly bool _includeQueryInRequestPath;
static readonly LogEventProperty[] NoProperties = new LogEventProperty[0];
static readonly LogEventProperty[] NoProperties = Array.Empty<LogEventProperty>();

public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnosticContext, RequestLoggingOptions options)
{
Expand Down Expand Up @@ -82,7 +82,6 @@ bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector

if (!logger.IsEnabled(level)) return false;

// Enrich diagnostic context
_enrichDiagnosticContext?.Invoke(_diagnosticContext, httpContext);

if (!collector.TryComplete(out var collectedProperties, out var collectedException))
Expand All @@ -91,7 +90,19 @@ bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector
// Last-in (correctly) wins...
var properties = collectedProperties.Concat(_getMessageTemplateProperties(httpContext, GetPath(httpContext, _includeQueryInRequestPath), elapsedMs, statusCode));

var evt = new LogEvent(DateTimeOffset.Now, level, ex ?? collectedException, _messageTemplate, properties);
var (traceId, spanId) = Activity.Current is { } activity ?
(activity.TraceId, activity.SpanId) :
(default(ActivityTraceId), default(ActivitySpanId));

var evt = new LogEvent(
DateTimeOffset.Now,
level,
ex ?? collectedException,
_messageTemplate,
properties,
traceId,
spanId);

logger.Write(evt);

return false;
Expand Down
4 changes: 2 additions & 2 deletions src/Serilog.AspNetCore/Serilog.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>Serilog support for ASP.NET Core logging</Description>
<!-- This must match the major and minor components of the referenced *.Extensions.* packages (and highest supported .NET TFM). -->
<VersionPrefix>7.0.0</VersionPrefix>
<VersionPrefix>7.0.1</VersionPrefix>
<Authors>Microsoft;Serilog Contributors</Authors>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand All @@ -23,7 +23,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog" Version="3.1.0-*" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Debug" Version="2.0.0" />
Expand Down

0 comments on commit f1c1bd7

Please sign in to comment.