Skip to content

Commit f4a7e3d

Browse files
committed
TraceSourceLogger now takes exception into account(adds it to the log message) even if formatter is not null
1 parent 28c0d2c commit f4a7e3d

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

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

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

44
using System;
55
using System.Diagnostics;
6+
using System.Globalization;
67
using DiagnosticsTraceSource = System.Diagnostics.TraceSource;
78

89
namespace Microsoft.Extensions.Logging.TraceSource
@@ -35,13 +36,20 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
3536
}
3637
if (exception != null)
3738
{
38-
message += Environment.NewLine + exception;
39+
message += string.Format(CultureInfo.InvariantCulture, "{0}{1}", Environment.NewLine, exception);
3940
}
4041
}
41-
if (!string.IsNullOrEmpty(message))
42+
43+
if (string.IsNullOrEmpty(message) && exception == null)
44+
return;
45+
46+
if (exception != null)
4247
{
43-
_traceSource.TraceEvent(GetEventType(logLevel), eventId.Id, message);
48+
string exceptionDelimiter = !string.IsNullOrEmpty(message) ? Environment.NewLine : string.Empty;
49+
message += string.Format(CultureInfo.InvariantCulture, "{0}Error: {1}", exceptionDelimiter, exception);
4450
}
51+
52+
_traceSource.TraceEvent(GetEventType(logLevel), eventId.Id, message);
4553
}
4654

4755
public bool IsEnabled(LogLevel logLevel)

0 commit comments

Comments
 (0)