Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.
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 @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -45,22 +46,38 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
{
if (this.IsEnabled(logLevel))
{
TraceTelemetry traceTelemetry = new TraceTelemetry(formatter(state, exception), this.GetSeverityLevel(logLevel));
IDictionary<string, string> dict = traceTelemetry.Context.Properties;
dict["CategoryName"] = this.categoryName;
dict["Exception"] = exception?.ToString();
IReadOnlyList<KeyValuePair<string, object>> stateDictionary = state as IReadOnlyList<KeyValuePair<string, object>>;
if (stateDictionary != null)
var stateDictionary = state as IReadOnlyList<KeyValuePair<string, object>>;
if (exception == null)
{
foreach (KeyValuePair<string, object> item in stateDictionary)
{
dict[item.Key] = Convert.ToString(item.Value);
}
var traceTelemetry = new TraceTelemetry(formatter(state, exception), this.GetSeverityLevel(logLevel));
PopulateTelemetry(traceTelemetry, stateDictionary);
this.telemetryClient.TrackTrace(traceTelemetry);
}
else
{
var exceptionTelemetry = new ExceptionTelemetry(exception);
exceptionTelemetry.Message = formatter(state, exception);
exceptionTelemetry.SeverityLevel = this.GetSeverityLevel(logLevel);
exceptionTelemetry.Context.Properties["Exception"] = exception.ToString();
PopulateTelemetry(exceptionTelemetry, stateDictionary);
this.telemetryClient.TrackException(exceptionTelemetry);
}
}
}

traceTelemetry.Context.GetInternalContext().SdkVersion = this.sdkVersion;
this.telemetryClient.TrackTrace(traceTelemetry);
private void PopulateTelemetry(ITelemetry telemetry, IReadOnlyList<KeyValuePair<string, object>> stateDictionary)
{
IDictionary<string, string> dict = telemetry.Context.Properties;
dict["CategoryName"] = this.categoryName;
if (stateDictionary != null)
{
foreach (KeyValuePair<string, object> item in stateDictionary)
{
dict[item.Key] = Convert.ToString(item.Value);
}
}

telemetry.Context.GetInternalContext().SdkVersion = this.sdkVersion;
}

private SeverityLevel GetSeverityLevel(LogLevel logLevel)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.ApplicationInsights.AspNetCore.Logging;
using System;
using Microsoft.ApplicationInsights.AspNetCore.Logging;
using Microsoft.ApplicationInsights.AspNetCore.Tests.Helpers;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.Extensions.Logging;
using Xunit;
Expand Down Expand Up @@ -28,6 +30,26 @@ public void TestLoggerSetsSdkVersionOnLoggedTelemetry()
Assert.True(isCorrectVersion);
}

/// <summary>
/// Tests that logging an exception results in tracking an <see cref="ExceptionTelemetry"/> instance.
/// </summary>
[Fact]
public void TestLoggerCreatesExceptionTelemetryOnLoggedError()
{
TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
{
Assert.IsType<ExceptionTelemetry>(t);
var exceptionTelemetry = (ExceptionTelemetry)t;
Assert.Equal("Error: This is an error", exceptionTelemetry.Message);
Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]);
Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel);
});

ILogger logger = new ApplicationInsightsLogger("test", client, (s, l) => { return true; });
var exception = new Exception("This is an error");
logger.LogError(0, exception, "Error: " + exception.Message);
}

/// <summary>
/// Tests that an incorrectly constructed or uninitialized Application Insights ILogger does not throw exceptions.
/// </summary>
Expand Down