Skip to content

Commit 5cb5ae9

Browse files
rizimaryamariyan
andauthored
TraceSourceLogger now takes exception into account(adds it to the log… (#42571)
* TraceSourceLogger now takes exception into account(adds it to the log message) even if formatter is not null * Fixed a logical error, that would have added the exception twice to the log message when the formatter would have been null. * Added tests for TraceSourceLogger. * Removed unnecessary white-spaces. * Make the TraceLogger output more "natural" (one line message) and similar to SystemdConsoleFormatter. * Update TraceSourceLoggerTest.cs * Update TraceSourceLoggerTest.cs Co-authored-by: Maryam Ariyan <maryam.ariyan@microsoft.com>
1 parent 1ea8eb1 commit 5cb5ae9

2 files changed

Lines changed: 41 additions & 10 deletions

File tree

src/libraries/Microsoft.Extensions.Logging.TraceSource/src/TraceSourceLogger.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,24 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
2222
{
2323
return;
2424
}
25+
2526
string message = string.Empty;
27+
2628
if (formatter != null)
2729
{
2830
message = formatter(state, exception);
2931
}
30-
else
32+
else if (state != null)
33+
{
34+
message += state;
35+
}
36+
37+
if (exception != null)
3138
{
32-
if (state != null)
33-
{
34-
message += state;
35-
}
36-
if (exception != null)
37-
{
38-
message += Environment.NewLine + exception;
39-
}
39+
string exceptionDelimiter = string.IsNullOrEmpty(message) ? string.Empty : " " ;
40+
message += exceptionDelimiter + exception;
4041
}
42+
4143
if (!string.IsNullOrEmpty(message))
4244
{
4345
_traceSource.TraceEvent(GetEventType(logLevel), eventId.Id, message);

src/libraries/Microsoft.Extensions.Logging/tests/Common/TraceSourceLoggerTest.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Diagnostics;
6+
using Moq;
7+
58
using Xunit;
69

710
namespace Microsoft.Extensions.Logging.Test
@@ -45,7 +48,6 @@ public static void MultipleLoggers_IsEnabledReturnsCorrectValue(SourceLevels fir
4548
secondSwitch.Level = second;
4649

4750
// Act
48-
4951
var factory = TestLoggerBuilder.Create(builder => builder
5052
.AddTraceSource(firstSwitch)
5153
.AddTraceSource(secondSwitch));
@@ -55,6 +57,33 @@ public static void MultipleLoggers_IsEnabledReturnsCorrectValue(SourceLevels fir
5557
// Assert
5658
Assert.Equal(expected, logger.IsEnabled(LogLevel.Information));
5759
}
60+
61+
[Theory]
62+
[InlineData(true)]
63+
[InlineData(false)]
64+
public static void Log_Shoud_Add_Exception_To_Message_Whether_Formatter_Is_Null_Or_Not(bool shouldFormatterBeNull)
65+
{
66+
// Arrange
67+
Mock<TraceListener> traceListener = new Mock<TraceListener>();
68+
SourceSwitch sourceSwitch = new SourceSwitch("TestSwitch") {Level = SourceLevels.All};
69+
70+
ILoggerFactory factory = TestLoggerBuilder.Create(builder => builder.AddTraceSource(sourceSwitch, traceListener.Object));
71+
ILogger logger = factory.CreateLogger("Test");
72+
73+
const LogLevel logLevel = LogLevel.Information;
74+
EventId eventId = new EventId(1);
75+
const string message = "some log message";
76+
Exception exception = new Exception("Some error occurred");
77+
Func<string, Exception, string> formatter = shouldFormatterBeNull ? (Func<string, Exception, string>)null : (value, passedException) => value;
78+
79+
string expectedMessage = $"{message} {exception}";
80+
81+
// Act
82+
logger.Log(logLevel, eventId, message, exception, formatter);
83+
84+
// Assert
85+
traceListener.Verify(listener => listener.TraceEvent(It.IsAny<TraceEventCache>(), It.IsAny<string>(), It.IsAny<TraceEventType>(), It.IsAny<int>(), expectedMessage), Times.Once);
86+
}
5887
}
5988
}
6089

0 commit comments

Comments
 (0)