Skip to content

TraceSourceLogger now takes exception into account(adds it to the log… #42571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
{
return;
}

string message = string.Empty;

if (formatter != null)
{
message = formatter(state, exception);
}
else
else if (state != null)
{
message += state;
}

if (exception != null)
{
if (state != null)
{
message += state;
}
if (exception != null)
{
message += Environment.NewLine + exception;
}
string exceptionDelimiter = string.IsNullOrEmpty(message) ? string.Empty : " " ;
message += exceptionDelimiter + exception;
}

if (!string.IsNullOrEmpty(message))
{
_traceSource.TraceEvent(GetEventType(logLevel), eventId.Id, message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using Moq;

using Xunit;

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

// Act

var factory = TestLoggerBuilder.Create(builder => builder
.AddTraceSource(firstSwitch)
.AddTraceSource(secondSwitch));
Expand All @@ -55,6 +57,33 @@ public static void MultipleLoggers_IsEnabledReturnsCorrectValue(SourceLevels fir
// Assert
Assert.Equal(expected, logger.IsEnabled(LogLevel.Information));
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public static void Log_Shoud_Add_Exception_To_Message_Whether_Formatter_Is_Null_Or_Not(bool shouldFormatterBeNull)
{
// Arrange
Mock<TraceListener> traceListener = new Mock<TraceListener>();
SourceSwitch sourceSwitch = new SourceSwitch("TestSwitch") {Level = SourceLevels.All};

ILoggerFactory factory = TestLoggerBuilder.Create(builder => builder.AddTraceSource(sourceSwitch, traceListener.Object));
ILogger logger = factory.CreateLogger("Test");

const LogLevel logLevel = LogLevel.Information;
EventId eventId = new EventId(1);
const string message = "some log message";
Exception exception = new Exception("Some error occurred");
Func<string, Exception, string> formatter = shouldFormatterBeNull ? (Func<string, Exception, string>)null : (value, passedException) => value;

string expectedMessage = $"{message} {exception}";

// Act
logger.Log(logLevel, eventId, message, exception, formatter);

// Assert
traceListener.Verify(listener => listener.TraceEvent(It.IsAny<TraceEventCache>(), It.IsAny<string>(), It.IsAny<TraceEventType>(), It.IsAny<int>(), expectedMessage), Times.Once);
}
}
}