Skip to content

Commit 3c4ea8b

Browse files
authored
Merge pull request #4454 from dotnet-maestro-bot/merge/release/8.0-to-main
[automated] Merge branch 'release/8.0' => 'main'
2 parents 687b637 + 89b3c52 commit 3c4ea8b

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

src/Libraries/Microsoft.Extensions.Telemetry/Logging/ExtendedLogger.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,13 @@ void HandleException(Exception exception, int indent)
165165

166166
_ = sb.Append(exception.GetType());
167167
_ = sb.Append(": ");
168-
_ = sb.AppendLine(exception.Message);
169-
_ = sb.Append(indentStr);
168+
169+
if (config.IncludeExceptionMessageInStackTraces)
170+
{
171+
_ = sb.AppendLine(exception.Message);
172+
_ = sb.Append(indentStr);
173+
}
174+
170175
_ = sb.Append(trace);
171176

172177
if (exception is AggregateException aggregateException)

src/Libraries/Microsoft.Extensions.Telemetry/Logging/ExtendedLoggerFactory.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,14 @@ private LoggerInformation[] CreateLoggers(string categoryName)
251251
private LoggerConfig ComputeConfig(LoggerEnrichmentOptions? enrichmentOptions)
252252
{
253253
return enrichmentOptions == null
254-
? new(Array.Empty<KeyValuePair<string, object?>>(), Array.Empty<Action<IEnrichmentTagCollector>>(), false, false, 0, _redactorProvider)
255-
: new(_staticTags, _enrichers, enrichmentOptions.CaptureStackTraces, enrichmentOptions.UseFileInfoForStackTraces, enrichmentOptions.MaxStackTraceLength, _redactorProvider);
254+
? new(Array.Empty<KeyValuePair<string, object?>>(), Array.Empty<Action<IEnrichmentTagCollector>>(), false, false, false, 0, _redactorProvider)
255+
: new(_staticTags,
256+
_enrichers,
257+
enrichmentOptions.CaptureStackTraces,
258+
enrichmentOptions.UseFileInfoForStackTraces,
259+
enrichmentOptions.IncludeExceptionMessageInStackTraces,
260+
enrichmentOptions.MaxStackTraceLength,
261+
_redactorProvider);
256262
}
257263

258264
private void UpdateStackTraceOptions(LoggerEnrichmentOptions enrichmentOptions) => Config = ComputeConfig(enrichmentOptions);

src/Libraries/Microsoft.Extensions.Telemetry/Logging/LoggerConfig.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public LoggerConfig(
1616
Action<IEnrichmentTagCollector>[] enrichers,
1717
bool captureStackTraces,
1818
bool useFileInfoForStackTraces,
19+
bool includeExceptionMessagesInStackTraces,
1920
int maxStackTraceLength,
2021
Func<DataClassification, Redactor> getRedactor)
2122
{
@@ -24,13 +25,15 @@ public LoggerConfig(
2425
CaptureStackTraces = captureStackTraces;
2526
UseFileInfoForStackTraces = useFileInfoForStackTraces;
2627
MaxStackTraceLength = maxStackTraceLength;
28+
IncludeExceptionMessageInStackTraces = includeExceptionMessagesInStackTraces;
2729
GetRedactor = getRedactor;
2830
}
2931

3032
public KeyValuePair<string, object?>[] StaticTags { get; }
3133
public Action<IEnrichmentTagCollector>[] Enrichers { get; }
3234
public bool CaptureStackTraces { get; }
3335
public bool UseFileInfoForStackTraces { get; }
36+
public bool IncludeExceptionMessageInStackTraces { get; }
3437
public int MaxStackTraceLength { get; }
3538
public Func<DataClassification, Redactor> GetRedactor { get; }
3639
}

src/Libraries/Microsoft.Extensions.Telemetry/Logging/LoggerEnrichmentOptions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,24 @@ public class LoggerEnrichmentOptions
4040
/// <remarks>
4141
/// Reading available debugging files produces richer stack traces, but can cost a substantial amount of time
4242
/// to generate. As a result, this option should only be turned on in development scenarios, not for production use.
43+
///
44+
/// This property has no effect if <see cref="CaptureStackTraces"/> is <see langword="false"/>.
45+
///
46+
/// This defaults to <see langword="false"/>.
4347
/// </remarks>
4448
public bool UseFileInfoForStackTraces { get; set; }
4549

50+
/// <summary>
51+
/// Gets or sets a value indicating whether to include exception messages in stack traces.
52+
/// </summary>
53+
/// <value>
54+
/// This defaults to <see langword="false"/>.
55+
/// </value>
56+
/// <remarks>
57+
/// This property has no effect if <see cref="CaptureStackTraces"/> is <see langword="false"/>.
58+
/// </remarks>
59+
public bool IncludeExceptionMessageInStackTraces { get; set; }
60+
4661
/// <summary>
4762
/// Gets or sets the maximum stack trace length to emit for a given log record.
4863
/// </summary>
@@ -51,6 +66,8 @@ public class LoggerEnrichmentOptions
5166
/// </value>
5267
/// <remarks>
5368
/// When set to a value less than 2 KB or greater than 32 KB, an exception will be thrown.
69+
///
70+
/// This property has no effect if <see cref="CaptureStackTraces"/> is <see langword="false"/>.
5471
/// </remarks>
5572
[Range(MinDefinedStackTraceLength, MaxDefinedStackTraceLength)]
5673
public int MaxStackTraceLength { get; set; } = DefaultStackTraceLength;

test/Libraries/Microsoft.Extensions.Telemetry.Tests/Logging/ExtendedLoggerTests.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,10 @@ public static void StringStateObject()
371371
Assert.Equal("PAYLOAD", snap[0].StructuredState!.GetValue("{OriginalFormat}"));
372372
}
373373

374-
[Fact]
375-
public static void Exceptions()
374+
[Theory]
375+
[InlineData(true)]
376+
[InlineData(false)]
377+
public static void Exceptions(bool includeExceptionMessage)
376378
{
377379
const string Category = "C1";
378380

@@ -382,6 +384,7 @@ public static void Exceptions()
382384
CaptureStackTraces = true,
383385
UseFileInfoForStackTraces = true,
384386
MaxStackTraceLength = 4096,
387+
IncludeExceptionMessageInStackTraces = includeExceptionMessage,
385388
};
386389

387390
using var lf = new ExtendedLoggerFactory(
@@ -401,7 +404,7 @@ public static void Exceptions()
401404
List<Exception> exceptions = [];
402405
try
403406
{
404-
throw new ArgumentNullException("ARG1");
407+
throw new ArgumentNullException("EM1", (Exception?)null);
405408
}
406409
catch (ArgumentNullException e)
407410
{
@@ -410,7 +413,7 @@ public static void Exceptions()
410413

411414
try
412415
{
413-
throw new ArgumentOutOfRangeException("ARG2");
416+
throw new ArgumentOutOfRangeException("EM2", (Exception?)null);
414417
}
415418
catch (ArgumentOutOfRangeException e)
416419
{
@@ -419,14 +422,14 @@ public static void Exceptions()
419422

420423
try
421424
{
422-
throw new InvalidOperationException("Doing crazy things");
425+
throw new InvalidOperationException("EM3");
423426
}
424427
catch (InvalidOperationException e)
425428
{
426429
exceptions.Add(e);
427430
}
428431

429-
throw new AggregateException("Outer", exceptions);
432+
throw new AggregateException("EM4", exceptions);
430433
}
431434
catch (AggregateException e)
432435
{
@@ -487,6 +490,21 @@ public static void Exceptions()
487490
Assert.Contains("ArgumentNullException", stackTrace);
488491
Assert.Contains("ArgumentOutOfRangeException", stackTrace);
489492
Assert.Contains("InvalidOperationException", stackTrace);
493+
494+
if (includeExceptionMessage)
495+
{
496+
Assert.Contains("EM1", stackTrace);
497+
Assert.Contains("EM2", stackTrace);
498+
Assert.Contains("EM3", stackTrace);
499+
Assert.Contains("EM4", stackTrace);
500+
}
501+
else
502+
{
503+
Assert.DoesNotContain("EM1", stackTrace);
504+
Assert.DoesNotContain("EM2", stackTrace);
505+
Assert.DoesNotContain("EM3", stackTrace);
506+
Assert.DoesNotContain("EM4", stackTrace);
507+
}
490508
}
491509

492510
#if false

0 commit comments

Comments
 (0)