Skip to content

Commit

Permalink
ApplicationInsights logging in NLog and log4net should include Except…
Browse files Browse the repository at this point in the history
…ionTelemetry.Message (#1315)

* ApplicationInsights logging in NLog and log4net should include ExceptionTelemetry.Message
  • Loading branch information
snakefoot authored and TimothyMothra committed Nov 21, 2019
1 parent eac9c6b commit 33ca19d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
14 changes: 6 additions & 8 deletions LOGGING/src/Log4NetAppender/ApplicationInsightsAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public sealed class ApplicationInsightsTarget : TargetWithLayout
public ApplicationInsightsTarget()
{
this.Layout = @"${message}";
this.OptimizeBufferReuse = true;
}

/// <summary>
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -252,19 +253,22 @@ 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);
}

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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
2 changes: 2 additions & 0 deletions LOGGING/test/NLogTarget.Net45.Tests/NLogTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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));
}

Expand Down

0 comments on commit 33ca19d

Please sign in to comment.