TraceSourceLogger now takes exception into account(adds it to the log… (#42571)
authorrizi <rizi@users.noreply.github.com>
Mon, 28 Sep 2020 17:54:42 +0000 (19:54 +0200)
committerGitHub <noreply@github.com>
Mon, 28 Sep 2020 17:54:42 +0000 (10:54 -0700)
* 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>
src/libraries/Microsoft.Extensions.Logging.TraceSource/src/TraceSourceLogger.cs
src/libraries/Microsoft.Extensions.Logging/tests/Common/TraceSourceLoggerTest.cs

index 1c2f2a9..e3e81d4 100644 (file)
@@ -22,22 +22,24 @@ namespace Microsoft.Extensions.Logging.TraceSource
             {
                 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);
index fbabd02..52632e4 100644 (file)
@@ -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
@@ -45,7 +48,6 @@ namespace Microsoft.Extensions.Logging.Test
             secondSwitch.Level = second;
 
             // Act
-
             var factory = TestLoggerBuilder.Create(builder => builder
                 .AddTraceSource(firstSwitch)
                 .AddTraceSource(secondSwitch));
@@ -55,6 +57,33 @@ namespace Microsoft.Extensions.Logging.Test
             // 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);
+        }
     }
 }