Skip to content

Commit 267c25a

Browse files
author
Daniel Marbach
authored
Support types in diagnostic entries (#6829)
1 parent f4b65d0 commit 267c25a

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"TypeIndicator":{"SomeType":"NServiceBus.Core.Tests.OpenTelemetry.DiagnosticsWriterTests"}}

src/NServiceBus.Core.Tests/OpenTelemetry/DiagnosticsWriterTests.cs

+19
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,24 @@ public async Task ShouldWriteWhenDuplicateEntriesPresent()
2929

3030
Approver.Verify(output);
3131
}
32+
33+
[Test]
34+
public async Task ShouldWriteEntriesWithTypesUsingTheFullName()
35+
{
36+
var output = string.Empty;
37+
var testWriter = new Func<string, CancellationToken, Task>((diagnosticOutput, _) =>
38+
{
39+
output = diagnosticOutput;
40+
return Task.CompletedTask;
41+
});
42+
var diagnostics = new StartupDiagnosticEntries();
43+
diagnostics.Add("TypeIndicator", new { SomeType = typeof(DiagnosticsWriterTests) });
44+
45+
var writer = new HostStartupDiagnosticsWriter(testWriter, true);
46+
47+
await writer.Write(diagnostics.entries);
48+
49+
Approver.Verify(output);
50+
}
3251
}
3352
}

src/NServiceBus.Core/Hosting/StartupDiagnostics/HostStartupDiagnosticsWriter.cs

+22-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Linq;
66
using System.Text.Json;
7+
using System.Text.Json.Serialization;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using Logging;
@@ -27,7 +28,7 @@ public async Task Write(List<StartupDiagnosticEntries.StartupDiagnosticEntry> en
2728

2829
try
2930
{
30-
data = JsonSerializer.Serialize(dictionary);
31+
data = JsonSerializer.Serialize(dictionary, diagnosticsOptions);
3132
}
3233
catch (Exception exception)
3334
{
@@ -80,6 +81,26 @@ await diagnosticsWriter(data, cancellationToken)
8081
readonly Func<string, CancellationToken, Task> diagnosticsWriter;
8182
readonly bool isCustomWriter;
8283

84+
static readonly JsonSerializerOptions diagnosticsOptions = new()
85+
{
86+
Converters = { new TypeConverter() }
87+
};
88+
89+
/// <summary>
90+
/// By default System.Text.Json would throw with "Serialization and deserialization of 'System.Type' instances are not supported" which normally
91+
/// would make sense because it can be considered unsafe to serialize and deserialize types. We add a custom converter here to make
92+
/// sure when diagnostics entries accidentally use types it will just print the full name as a string. We never intent to read these things
93+
/// back so this is a safe approach.
94+
/// </summary>
95+
sealed class TypeConverter : JsonConverter<Type>
96+
{
97+
// we never need to deserialize
98+
public override Type Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => throw new NotImplementedException();
99+
100+
public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options) => writer.WriteStringValue(value.FullName);
101+
}
102+
103+
83104
static readonly ILog logger = LogManager.GetLogger<HostStartupDiagnosticsWriter>();
84105
}
85106
}

0 commit comments

Comments
 (0)