From 33ca19d042a41707e6430109ec3e710417b79f76 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Thu, 21 Nov 2019 19:42:43 +0100 Subject: [PATCH] ApplicationInsights logging in NLog and log4net should include ExceptionTelemetry.Message (#1315) * ApplicationInsights logging in NLog and log4net should include ExceptionTelemetry.Message --- CHANGELOG.md | 2 ++ .../ApplicationInsightsAppender.cs | 14 ++++++-------- .../src/NLogTarget/ApplicationInsightsTarget.cs | 16 ++++++++++------ .../ApplicationInsightsAppenderTests.cs | 1 + .../NLogTarget.Net45.Tests/NLogTargetTests.cs | 2 ++ 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 909b49e418..869182cd9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## VNext +- [Log4Net includes Message for ExceptionTelemetry](https://github.com/microsoft/ApplicationInsights-dotnet/pull/1315) +- [NLog includes Message for ExceptionTelemetry](https://github.com/microsoft/ApplicationInsights-dotnet/pull/1315) ## Version 2.12.0-beta3 - [Standard Metric extractor for Dependency) add Dependency.ResultCode dimension.](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1233) diff --git a/LOGGING/src/Log4NetAppender/ApplicationInsightsAppender.cs b/LOGGING/src/Log4NetAppender/ApplicationInsightsAppender.cs index f25b2af71a..650ce009d7 100644 --- a/LOGGING/src/Log4NetAppender/ApplicationInsightsAppender.cs +++ b/LOGGING/src/Log4NetAppender/ApplicationInsightsAppender.cs @@ -186,17 +186,16 @@ private void SendException(LoggingEvent loggingEvent) var exceptionTelemetry = new ExceptionTelemetry(loggingEvent.ExceptionObject) { SeverityLevel = GetSeverityLevel(loggingEvent.Level), + Message = $"{loggingEvent.ExceptionObject.GetType().ToString()}: {loggingEvent.ExceptionObject.Message}", }; - string message = null; if (loggingEvent.RenderedMessage != null) { - message = this.RenderLoggingEvent(loggingEvent); - } - - if (!string.IsNullOrEmpty(message)) - { - exceptionTelemetry.Properties.Add("Message", message); + var message = this.RenderLoggingEvent(loggingEvent); + if (!string.IsNullOrEmpty(message)) + { + exceptionTelemetry.Properties.Add("Message", message); + } } BuildCustomProperties(loggingEvent, exceptionTelemetry); @@ -212,7 +211,6 @@ private void SendTrace(LoggingEvent loggingEvent) { try { - loggingEvent.GetProperties(); string message = loggingEvent.RenderedMessage != null ? this.RenderLoggingEvent(loggingEvent) : "Log4Net Trace"; var trace = new TraceTelemetry(message) diff --git a/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs b/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs index 029caee1c7..454fa4ab44 100644 --- a/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs +++ b/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs @@ -38,6 +38,7 @@ public sealed class ApplicationInsightsTarget : TargetWithLayout public ApplicationInsightsTarget() { this.Layout = @"${message}"; + this.OptimizeBufferReuse = true; } /// @@ -93,9 +94,9 @@ internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace) for (int i = 0; i < this.ContextProperties.Count; ++i) { var contextProperty = this.ContextProperties[i]; - if (!string.IsNullOrEmpty(contextProperty.Name)) + if (!string.IsNullOrEmpty(contextProperty.Name) && contextProperty.Layout != null) { - string propertyValue = contextProperty.Layout?.Render(logEvent); + string propertyValue = RenderLogEvent(contextProperty.Layout, logEvent); PopulatePropertyBag(propertyBag, contextProperty.Name, propertyValue); } } @@ -252,10 +253,14 @@ private void SendException(LogEventInfo logEvent) var exceptionTelemetry = new ExceptionTelemetry(logEvent.Exception) { SeverityLevel = GetSeverityLevel(logEvent.Level), + Message = $"{logEvent.Exception.GetType().ToString()}: {logEvent.Exception.Message}", }; - string logMessage = this.Layout.Render(logEvent); - exceptionTelemetry.Properties.Add("Message", logMessage); + string logMessage = RenderLogEvent(this.Layout, logEvent); + if (!string.IsNullOrEmpty(logMessage)) + { + exceptionTelemetry.Properties.Add("Message", logMessage); + } this.BuildPropertyBag(logEvent, exceptionTelemetry); this.telemetryClient.Track(exceptionTelemetry); @@ -263,8 +268,7 @@ private void SendException(LogEventInfo logEvent) private void SendTrace(LogEventInfo logEvent) { - string logMessage = this.Layout.Render(logEvent); - + string logMessage = RenderLogEvent(this.Layout, logEvent); var trace = new TraceTelemetry(logMessage) { SeverityLevel = GetSeverityLevel(logEvent.Level), diff --git a/LOGGING/test/Log4NetAppender.Net45.Tests/ApplicationInsightsAppenderTests.cs b/LOGGING/test/Log4NetAppender.Net45.Tests/ApplicationInsightsAppenderTests.cs index 6ee110fcde..d2dc9fcb4c 100644 --- a/LOGGING/test/Log4NetAppender.Net45.Tests/ApplicationInsightsAppenderTests.cs +++ b/LOGGING/test/Log4NetAppender.Net45.Tests/ApplicationInsightsAppenderTests.cs @@ -325,6 +325,7 @@ public void CustomMessageIsAddedToExceptionTelemetryCustomProperties() } ExceptionTelemetry telemetry = (ExceptionTelemetry)this.appendableLogger.SentItems.First(); + Assert.AreEqual("System.Exception: Test logging exception", telemetry.Message); Assert.IsTrue(telemetry.Properties["Message"].StartsWith("custom message", StringComparison.Ordinal)); } diff --git a/LOGGING/test/NLogTarget.Net45.Tests/NLogTargetTests.cs b/LOGGING/test/NLogTarget.Net45.Tests/NLogTargetTests.cs index 7d2863a0fc..92a6604fd7 100644 --- a/LOGGING/test/NLogTarget.Net45.Tests/NLogTargetTests.cs +++ b/LOGGING/test/NLogTarget.Net45.Tests/NLogTargetTests.cs @@ -305,6 +305,7 @@ public void TraceAreEnqueuedInChannelAndContainExceptionMessage() } var telemetry = (ExceptionTelemetry)this.adapterHelper.Channel.SentItems.First(); + Assert.AreEqual("System.Exception: Test logging exception", telemetry.Message); Assert.AreEqual(expectedException.Message, telemetry.Exception.Message); } @@ -324,6 +325,7 @@ public void CustomMessageIsAddedToExceptionTelemetryCustomProperties() } ExceptionTelemetry telemetry = (ExceptionTelemetry)this.adapterHelper.Channel.SentItems.First(); + Assert.AreEqual("System.Exception: Test logging exception", telemetry.Message); Assert.IsTrue(telemetry.Properties["Message"].StartsWith("custom message", StringComparison.Ordinal)); }