Skip to content

Commit 5240ad4

Browse files
committed
Added event id and trace to diagnostic summary log
1 parent 2189d8d commit 5240ad4

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

identity-server/src/IdentityServer/Events/Infrastructure/EventIds.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,11 @@ public static class EventIds
6969

7070
public const int BackchannelAuthenticationSuccess = BackchannelAuthenticationEventsStart + 0;
7171
public const int BackchannelAuthenticationFailure = BackchannelAuthenticationEventsStart + 1;
72+
73+
//////////////////////////////////////////////////////
74+
/// Diagnostics related events
75+
//////////////////////////////////////////////////////
76+
private const int DiagnosticEventsStart = 7000;
77+
78+
public const int DiagnosticSummaryLogged = DiagnosticEventsStart + 0;
7279
}

identity-server/src/IdentityServer/Licensing/V2/Diagnostics/DiagnosticSummary.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public async Task PrintSummary()
2929

3030
var span = bufferWriter.WrittenSpan;
3131

32+
using var diagnosticActivity = Tracing.DiagnosticsActivitySource.StartActivity("DiagnosticSummary");
3233
var chunkSize = options.Diagnostics.ChunkSize;
3334
if (span.Length > chunkSize)
3435
{
@@ -38,12 +39,12 @@ public async Task PrintSummary()
3839
var offset = i * chunkSize;
3940
var length = Math.Min(chunkSize, span.Length - offset);
4041
var chunk = span.Slice(offset, length);
41-
logger.LogInformation("Diagnostic data ({current} of {totalChunks}): {diagnosticData}", i + 1, totalChunks, Encoding.UTF8.GetString(chunk));
42+
logger.DiagnosticSummaryLogged(i + 1, totalChunks, Encoding.UTF8.GetString(chunk));
4243
}
4344
}
4445
else
4546
{
46-
logger.LogInformation("Diagnostic data: {diagnosticData}", Encoding.UTF8.GetString(bufferWriter.WrittenSpan));
47+
logger.DiagnosticSummaryLogged(1, 1, Encoding.UTF8.GetString(bufferWriter.WrittenSpan));
4748
}
4849
}
4950
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// See LICENSE in the project root for license information.
3+
4+
using Duende.IdentityServer.Events;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Duende.IdentityServer.Licensing.V2.Diagnostics;
8+
9+
internal static class DiagnosticLogParameters
10+
{
11+
public const string Current = "current";
12+
public const string TotalChunks = "totalChunks";
13+
public const string DiagnosticData = "diagnosticData";
14+
}
15+
16+
internal static partial class Log
17+
{
18+
[LoggerMessage(
19+
LogLevel.Information,
20+
EventId = EventIds.DiagnosticSummaryLogged,
21+
EventName = "DiagnosticSummaryLogged",
22+
Message =
23+
$"Diagnostic data ({{{DiagnosticLogParameters.Current}}} of {{{DiagnosticLogParameters.TotalChunks}}}): {{{DiagnosticLogParameters.DiagnosticData}}}"
24+
)]
25+
public static partial void DiagnosticSummaryLogged(this ILogger logger, int current, int totalChunks, string diagnosticData);
26+
}

identity-server/src/Telemetry/Tracing.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public static class Tracing
4848
TraceNames.Validation,
4949
ServiceVersion);
5050

51+
/// <summary>
52+
/// Diagnostics ActivitySource
53+
/// </summary>
54+
public static ActivitySource DiagnosticsActivitySource { get; } = new(
55+
TraceNames.Diagnostics,
56+
ServiceVersion);
57+
5158
/// <summary>
5259
/// Service version
5360
/// </summary>
@@ -79,6 +86,11 @@ public static class TraceNames
7986
/// Service name for detailed validation traces
8087
/// </summary>
8188
public static string Validation => Basic + ".Validation";
89+
90+
/// <summary>
91+
/// Service name for diagnostics traces
92+
/// </summary>
93+
public static string Diagnostics => Basic + ".Diagnostics";
8294
}
8395

8496
public static class Properties

identity-server/test/IdentityServer.UnitTests/Licensing/v2/DiagnosticSummaryTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Text.Json;
55
using Duende.IdentityServer.Configuration;
6+
using Duende.IdentityServer.Events;
67
using Duende.IdentityServer.Licensing.V2.Diagnostics;
78
using Microsoft.Extensions.Logging.Abstractions;
89
using Microsoft.Extensions.Logging.Testing;
@@ -84,6 +85,20 @@ public async Task PrintSummary_ShouldCreateChunksWithMaxSizeEightKB()
8485
}
8586
}
8687

88+
[Fact]
89+
public async Task PrintSummary_ShouldIncludeLogEventId()
90+
{
91+
var options = new IdentityServerOptions();
92+
var logger = new FakeLogger<DiagnosticSummary>();
93+
var diagnosticEntry = new LongDiagnosticEntry { OutputLength = 100000 };
94+
var summary = new DiagnosticSummary([diagnosticEntry], options, logger);
95+
96+
await summary.PrintSummary();
97+
98+
var logSnapshot = logger.Collector.GetSnapshot();
99+
logSnapshot.Count.ShouldBeGreaterThan(0);
100+
logSnapshot[0].Id.Id.ShouldBe(EventIds.DiagnosticSummaryLogged);
101+
}
87102

88103
private class TestDiagnosticEntry : IDiagnosticEntry
89104
{

0 commit comments

Comments
 (0)