diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLogger.cs b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLogger.cs index 8c79926c..a4512c12 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLogger.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLogger.cs @@ -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; /// @@ -13,6 +15,7 @@ internal class ApplicationInsightsLogger : ILogger private readonly string categoryName; private readonly TelemetryClient telemetryClient; private readonly Func filter; + private readonly string sdkVersion; /// /// Creates a new instance of @@ -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(); } /// @@ -41,19 +45,21 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except { if (this.IsEnabled(logLevel)) { - var dict = new Dictionary(); + TraceTelemetry traceTelemetry = new TraceTelemetry(formatter(state, exception), this.GetSeverityLevel(logLevel)); + IDictionary dict = traceTelemetry.Context.Properties; dict["CategoryName"] = this.categoryName; dict["Exception"] = exception?.ToString(); - var stateDictionary = state as IReadOnlyList>; + IReadOnlyList> stateDictionary = state as IReadOnlyList>; if (stateDictionary != null) { - foreach (var item in stateDictionary) + foreach (KeyValuePair 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); } } diff --git a/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Logging/ApplicationInsightsLoggerTests.cs b/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Logging/ApplicationInsightsLoggerTests.cs new file mode 100644 index 00000000..08fc629e --- /dev/null +++ b/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Logging/ApplicationInsightsLoggerTests.cs @@ -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 +{ + /// + /// Tests for the Application Insights ILogger implementation. + /// + public class ApplicationInsightsLoggerTests + { + /// + /// Tests that the SDK version is correctly set on the telemetry context when messages are logged to AI. + /// + [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); + } + } +}