Skip to content

Commit

Permalink
Added explicit setting of the SDK version to ILogger trace calls. (mi…
Browse files Browse the repository at this point in the history
…crosoft#322)

* Added explicit setting of the SDK version to ILogger trace calls.
* Switched to explicit types for readability.
* Added unit test for AI ILogger setting SDK version on telemetry context.
  • Loading branch information
dnduffy authored Jan 19, 2017
1 parent 65cc088 commit ba0fe23
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
{
using System;
using System.Collections.Generic;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.Extensions.Logging;

/// <summary>
Expand All @@ -13,6 +15,7 @@ internal class ApplicationInsightsLogger : ILogger
private readonly string categoryName;
private readonly TelemetryClient telemetryClient;
private readonly Func<string, LogLevel, bool> filter;
private readonly string sdkVersion;

/// <summary>
/// Creates a new instance of <see cref="ApplicationInsightsLogger"/>
Expand All @@ -22,6 +25,7 @@ public ApplicationInsightsLogger(string name, TelemetryClient telemetryClient, F
this.categoryName = name;
this.telemetryClient = telemetryClient;
this.filter = filter;
this.sdkVersion = SdkVersionUtils.VersionPrefix + SdkVersionUtils.GetAssemblyVersion();
}

/// <inheritdoc />
Expand All @@ -41,19 +45,21 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
{
if (this.IsEnabled(logLevel))
{
var dict = new Dictionary<string, string>();
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();
var stateDictionary = state as IReadOnlyList<KeyValuePair<string, object>>;
IReadOnlyList<KeyValuePair<string, object>> stateDictionary = state as IReadOnlyList<KeyValuePair<string, object>>;
if (stateDictionary != null)
{
foreach (var item in stateDictionary)
foreach (KeyValuePair<string, object> item in stateDictionary)
{
dict[item.Key] = Convert.ToString(item.Value);
}
}

this.telemetryClient.TrackTrace(formatter(state, exception), this.GetSeverityLevel(logLevel), dict);
traceTelemetry.Context.GetInternalContext().SdkVersion = this.sdkVersion;
this.telemetryClient.TrackTrace(traceTelemetry);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.ApplicationInsights.AspNetCore.Logging;
using Microsoft.ApplicationInsights.AspNetCore.Tests.Helpers;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.Extensions.Logging;
using Xunit;

namespace Microsoft.ApplicationInsights.AspNetCore.Tests.Logging
{
/// <summary>
/// Tests for the Application Insights ILogger implementation.
/// </summary>
public class ApplicationInsightsLoggerTests
{
/// <summary>
/// Tests that the SDK version is correctly set on the telemetry context when messages are logged to AI.
/// </summary>
[Fact]
public void TestLoggerSetsSdkVersionOnLoggedTelemetry()
{
bool isCorrectVersion = false;
TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
{
isCorrectVersion = t.Context.GetInternalContext().SdkVersion.StartsWith(SdkVersionUtils.VersionPrefix);
});

ILogger logger = new ApplicationInsightsLogger("test", client, (s, l) => { return true; });
logger.LogTrace("This is a test.", new object[] { });
Assert.True(isCorrectVersion);
}
}
}

0 comments on commit ba0fe23

Please sign in to comment.