diff --git a/CHANGELOG.md b/CHANGELOG.md index 57b0d4b0..566f8075 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Updated Javascript Snippet with latest from [Github/ApplicationInsights-JS](https://github.com/Microsoft/ApplicationInsights-JS) - [Make all built-in TelemetryInitializers public to allow easy removal from DI Container.](https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/351) - [Enforced limits of values read from incoming http requests to prevent security vulnerability](https://github.com/Microsoft/ApplicationInsights-aspnetcore/pull/608) +- [ApplicationInsightsLogger adds EventId into telemetry properties. It is off by default for compatibility. It can be switched on by configuring ApplicationInsightsLoggerOptions.](https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/569) ## Version 2.2.1 - Updated Web/Base SDK version dependency to 2.5.1 which addresses a bug. diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLogger.cs b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLogger.cs index 2a7f2e3c..40334399 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLogger.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLogger.cs @@ -17,16 +17,18 @@ internal class ApplicationInsightsLogger : ILogger private readonly string categoryName; private readonly TelemetryClient telemetryClient; private readonly Func filter; + private readonly ApplicationInsightsLoggerOptions options; private readonly string sdkVersion; /// /// Creates a new instance of /// - public ApplicationInsightsLogger(string name, TelemetryClient telemetryClient, Func filter) + public ApplicationInsightsLogger(string name, TelemetryClient telemetryClient, Func filter, ApplicationInsightsLoggerOptions options) { this.categoryName = name; this.telemetryClient = telemetryClient; this.filter = filter; + this.options = options; this.sdkVersion = SdkVersionUtils.VersionPrefix + SdkVersionUtils.GetAssemblyVersion(); } @@ -51,7 +53,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except if (exception == null) { var traceTelemetry = new TraceTelemetry(formatter(state, exception), this.GetSeverityLevel(logLevel)); - PopulateTelemetry(traceTelemetry, stateDictionary); + PopulateTelemetry(traceTelemetry, stateDictionary, eventId); this.telemetryClient.TrackTrace(traceTelemetry); } else @@ -60,16 +62,30 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except exceptionTelemetry.Message = formatter(state, exception); exceptionTelemetry.SeverityLevel = this.GetSeverityLevel(logLevel); exceptionTelemetry.Context.Properties["Exception"] = exception.ToString(); - PopulateTelemetry(exceptionTelemetry, stateDictionary); + PopulateTelemetry(exceptionTelemetry, stateDictionary, eventId); this.telemetryClient.TrackException(exceptionTelemetry); } } } - private void PopulateTelemetry(ITelemetry telemetry, IReadOnlyList> stateDictionary) + private void PopulateTelemetry(ITelemetry telemetry, IReadOnlyList> stateDictionary, EventId eventId) { IDictionary dict = telemetry.Context.Properties; dict["CategoryName"] = this.categoryName; + + if (this.options?.IncludeEventId ?? false) + { + if (eventId.Id != 0) + { + dict["EventId"] = eventId.Id.ToString(System.Globalization.CultureInfo.InvariantCulture); + } + + if (!string.IsNullOrEmpty(eventId.Name)) + { + dict["EventName"] = eventId.Name; + } + } + if (stateDictionary != null) { foreach (KeyValuePair item in stateDictionary) diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerFactoryExtensions.cs b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerFactoryExtensions.cs index 10f8c225..2a10bf59 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerFactoryExtensions.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerFactoryExtensions.cs @@ -4,6 +4,7 @@ using ApplicationInsights; using ApplicationInsights.AspNetCore.Logging; using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.Options; /// /// Extension methods for that allow adding Application Insights logger. @@ -64,6 +65,12 @@ public static ILoggerFactory AddApplicationInsights( { var client = serviceProvider.GetService(); var debugLoggerControl = serviceProvider.GetService(); + var options = serviceProvider.GetService>(); + + if (options == null) + { + options = Options.Create(new ApplicationInsightsLoggerOptions()); + } if (debugLoggerControl != null) { @@ -75,7 +82,7 @@ public static ILoggerFactory AddApplicationInsights( } } - factory.AddProvider(new ApplicationInsightsLoggerProvider(client, filter)); + factory.AddProvider(new ApplicationInsightsLoggerProvider(client, filter, options)); return factory; } } diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerOptions.cs b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerOptions.cs new file mode 100644 index 00000000..68417612 --- /dev/null +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerOptions.cs @@ -0,0 +1,13 @@ +namespace Microsoft.ApplicationInsights.AspNetCore.Logging +{ + /// + /// Options for configuration of . + /// + public class ApplicationInsightsLoggerOptions + { + /// + /// Gets or sets value indicating, whether EventId and EventName properties should be included in telemetry. + /// + public bool IncludeEventId { get; set; } + } +} diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerProvider.cs b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerProvider.cs index f40e8a20..f38c3792 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerProvider.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerProvider.cs @@ -2,6 +2,7 @@ { using System; using Microsoft.Extensions.Logging; + using Microsoft.Extensions.Options; /// /// implementation that creates returns instances of @@ -11,20 +12,22 @@ internal class ApplicationInsightsLoggerProvider : ILoggerProvider { private readonly TelemetryClient telemetryClient; private readonly Func filter; + private readonly ApplicationInsightsLoggerOptions options; /// /// Initializes a new instance of the class. /// - public ApplicationInsightsLoggerProvider(TelemetryClient telemetryClient, Func filter) + public ApplicationInsightsLoggerProvider(TelemetryClient telemetryClient, Func filter, IOptions options) { this.telemetryClient = telemetryClient; this.filter = filter; + this.options = options.Value; } /// public ILogger CreateLogger(string categoryName) { - return new ApplicationInsightsLogger(categoryName, this.telemetryClient, filter); + return new ApplicationInsightsLogger(categoryName, this.telemetryClient, filter, options); } /// diff --git a/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Logging/ApplicationInsightsLoggerTests.cs b/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Logging/ApplicationInsightsLoggerTests.cs index 510d9f90..e489fbb9 100644 --- a/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Logging/ApplicationInsightsLoggerTests.cs +++ b/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Logging/ApplicationInsightsLoggerTests.cs @@ -13,6 +13,8 @@ namespace Microsoft.ApplicationInsights.AspNetCore.Tests.Logging /// public class ApplicationInsightsLoggerTests { + private static readonly ApplicationInsightsLoggerOptions defaultOptions = new ApplicationInsightsLoggerOptions(); + /// /// Tests that the SDK version is correctly set on the telemetry context when messages are logged to AI. /// @@ -25,11 +27,183 @@ public void TestLoggerSetsSdkVersionOnLoggedTelemetry() isCorrectVersion = t.Context.GetInternalContext().SdkVersion.StartsWith(SdkVersionUtils.VersionPrefix); }); - ILogger logger = new ApplicationInsightsLogger("test", client, (s, l) => { return true; }); + ILogger logger = new ApplicationInsightsLogger("test", client, (s, l) => { return true; }, null); logger.LogTrace("This is a test.", new object[] { }); Assert.True(isCorrectVersion); } + /// + /// Tests that logging an information results in tracking an instance. + /// + [Fact] + public void TestLoggerCreatesTraceTelemetryOnLoggedInfo() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var traceTelemetry = (TraceTelemetry)t; + Assert.Equal("This is an information", traceTelemetry.Message); + Assert.Equal(SeverityLevel.Information, traceTelemetry.SeverityLevel); + Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]); + }); + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, null); + logger.LogInformation(0, "This is an information"); + } + + /// + /// Tests that logging a warning with parameters results in tracking an instance with telemetry properties. + /// + [Fact] + public void TestLoggerCreatesTraceTelemetryOnLoggedWarningWithParameters() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var traceTelemetry = (TraceTelemetry)t; + Assert.Equal("This is 123 value: Hello, World!", traceTelemetry.Message); + Assert.Equal(SeverityLevel.Warning, traceTelemetry.SeverityLevel); + Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]); + + Assert.Equal("123", traceTelemetry.Properties["testParam"]); + Assert.Equal("Hello, World!", traceTelemetry.Properties["param2"]); + }); + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, null); + logger.LogWarning(0, "This is {testParam} value: {param2}", 123, "Hello, World!"); + } + + /// + /// Tests that logging an information with results in tracking an instance, when includeEventId is false. + /// + [Fact] + public void TestLoggerCreatesTraceTelemetryWithoutEventIdOnLoggedDebug() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var traceTelemetry = (TraceTelemetry)t; + Assert.Equal("This is an information", traceTelemetry.Message); + Assert.Equal(SeverityLevel.Verbose, traceTelemetry.SeverityLevel); + Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]); + + Assert.False(traceTelemetry.Properties.ContainsKey("EventId")); + Assert.False(traceTelemetry.Properties.ContainsKey("EventName")); + }); + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, null); + logger.LogDebug(new EventId(22, "TestEvent"), "This is an information"); + } + + /// + /// Tests that logging an error with results in tracking an instance with event properties, when includeEventId is true. + /// + [Fact] + public void TestLoggerIncludingEventIdCreatesTraceTelemetryWithEventIdOnLoggedError() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var traceTelemetry = (TraceTelemetry)t; + Assert.Equal("This is an error", traceTelemetry.Message); + Assert.Equal(SeverityLevel.Error, traceTelemetry.SeverityLevel); + Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]); + + Assert.Equal("22", traceTelemetry.Properties["EventId"]); + Assert.Equal("TestEvent", traceTelemetry.Properties["EventName"]); + }); + var options = new ApplicationInsightsLoggerOptions { IncludeEventId = true }; + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, options); + logger.LogError(new EventId(22, "TestEvent"), "This is an error"); + } + + /// + /// Tests that logging an information without results in tracking an instance. + /// + [Fact] + public void TestLoggerIncludingEventIdCreatesTraceTelemetryWithoutEventIdOnLoggedInfo() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var traceTelemetry = (TraceTelemetry)t; + Assert.Equal("This is an information", traceTelemetry.Message); + Assert.Equal(SeverityLevel.Information, traceTelemetry.SeverityLevel); + Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]); + + Assert.False(traceTelemetry.Properties.ContainsKey("EventId")); + Assert.False(traceTelemetry.Properties.ContainsKey("EventName")); + }); + var options = new ApplicationInsightsLoggerOptions { IncludeEventId = true }; + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, options); + logger.LogInformation(0, "This is an information"); + } + + /// + /// Tests that logging an information with emtpy event name results in tracking an instance. + /// + [Fact] + public void TestLoggerIncludingEventIdCreatesTraceTelemetryWithoutEventNameOnLoggedInfo() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var traceTelemetry = (TraceTelemetry)t; + Assert.Equal("This is an information", traceTelemetry.Message); + Assert.Equal(SeverityLevel.Information, traceTelemetry.SeverityLevel); + Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]); + + Assert.Equal("100", traceTelemetry.Properties["EventId"]); + Assert.False(traceTelemetry.Properties.ContainsKey("EventName")); + }); + var options = new ApplicationInsightsLoggerOptions { IncludeEventId = true }; + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, options); + logger.LogInformation(new EventId(100, string.Empty), "This is an information"); + } + + /// + /// Tests that logging an error with results in tracking an instance with event properties, + /// when includeEventId is true and parameter is named EventId. + /// + [Fact] + public void TestLoggerIncludingEventIdCreatesTraceTelemetryWithEventIdOnLoggedErrorWithParameters() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var traceTelemetry = (TraceTelemetry)t; + Assert.Equal("This is an ERROR error. 15", traceTelemetry.Message); + Assert.Equal(SeverityLevel.Error, traceTelemetry.SeverityLevel); + Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]); + + Assert.Equal("ERROR", traceTelemetry.Properties["EventId"]); + Assert.Equal("TestEvent", traceTelemetry.Properties["EventName"]); + Assert.Equal("15", traceTelemetry.Properties["Message"]); + }); + var options = new ApplicationInsightsLoggerOptions { IncludeEventId = true }; + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, options); + logger.LogError(new EventId(22, "TestEvent"), "This is an {EventId} error. {Message}", "ERROR", 15); + } + + /// + /// Tests that logging an information results in not tracking an instance, when filter returns false. + /// + [Fact] + public void TestLoggerWithNegativeFilterDoesNotCreateTraceTelemetryOnLoggedInfo() + { + bool trackedTelemetry = false; + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => trackedTelemetry = true); + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return false; }, defaultOptions); + logger.LogInformation(0, "This is an information"); + + Assert.False(trackedTelemetry); + } + /// /// Tests that logging an exception results in tracking an instance. /// @@ -40,14 +214,181 @@ public void TestLoggerCreatesExceptionTelemetryOnLoggedError() { Assert.IsType(t); var exceptionTelemetry = (ExceptionTelemetry)t; + Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel); + Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]); + Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]); + Assert.Equal("Error: This is an error", exceptionTelemetry.Message); + }); + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, defaultOptions); + var exception = new Exception("This is an error"); + logger.LogError(0, exception, "Error: " + exception.Message); + } + + /// + /// Tests that logging an exception with parameters results in tracking an instance with parameters. + /// + [Fact] + public void TestLoggerCreatesExceptionTelemetryOnLoggedErrorWithParameters() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var exceptionTelemetry = (ExceptionTelemetry)t; + Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel); + Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]); + Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]); + Assert.Equal("Error: This is an error, Value: 123", exceptionTelemetry.Message); + + Assert.Equal("This is an error", exceptionTelemetry.Properties["ex"]); + Assert.Equal("123", exceptionTelemetry.Properties["value"]); + }); + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, defaultOptions); + var exception = new Exception("This is an error"); + logger.LogError(0, exception, "Error: {ex}, Value: {value}", exception.Message, 123); + } + + /// + /// Tests that logging an exception with results in tracking an instance, when includeEventId is false. + /// + [Fact] + public void TestLoggerCreatesExceptionWithoutEventIdTelemetryOnLoggedError() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var exceptionTelemetry = (ExceptionTelemetry)t; + Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel); + Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]); + Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]); + Assert.Equal("Error: This is an error", exceptionTelemetry.Message); + + Assert.False(exceptionTelemetry.Properties.ContainsKey("EventId")); + Assert.False(exceptionTelemetry.Properties.ContainsKey("EventName")); + }); + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, defaultOptions); + var exception = new Exception("This is an error"); + logger.LogError(new EventId(22, "TestEvent"), exception, "Error: " + exception.Message); + } + + /// + /// Tests that logging an exception with results in tracking an instance, when includeEventId is true. + /// + [Fact] + public void TestLoggerIncludingEventIdCreatesExceptionWithEventIdTelemetryOnLoggedError() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var exceptionTelemetry = (ExceptionTelemetry)t; + Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel); + Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]); + Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]); + Assert.Equal("Error: This is an error", exceptionTelemetry.Message); + + Assert.Equal("22", exceptionTelemetry.Properties["EventId"]); + Assert.Equal("TestEvent", exceptionTelemetry.Properties["EventName"]); + }); + var options = new ApplicationInsightsLoggerOptions { IncludeEventId = true }; + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, options); + var exception = new Exception("This is an error"); + logger.LogError(new EventId(22, "TestEvent"), exception, "Error: " + exception.Message); + } + + /// + /// Tests that logging an exception without results in tracking an instance, when includeEventId is true. + /// + [Fact] + public void TestLoggerIncludingEventIdCreatesExceptionWithoutEventIdTelemetryOnLoggedError() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var exceptionTelemetry = (ExceptionTelemetry)t; + Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel); + Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]); + Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]); Assert.Equal("Error: This is an error", exceptionTelemetry.Message); + + Assert.False(exceptionTelemetry.Properties.ContainsKey("EventId")); + Assert.False(exceptionTelemetry.Properties.ContainsKey("EventName")); + }); + var options = new ApplicationInsightsLoggerOptions { IncludeEventId = true }; + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, options); + var exception = new Exception("This is an error"); + logger.LogError(0, exception, "Error: " + exception.Message); + } + + /// + /// Tests that logging an exception with empty event name results in tracking an instance, when includeEventId is true. + /// + [Fact] + public void TestLoggerIncludingEventIdCreatesExceptionWithoutEventNameTelemetryOnLoggedError() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var exceptionTelemetry = (ExceptionTelemetry)t; + Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel); + Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]); Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]); + Assert.Equal("Error: This is an error", exceptionTelemetry.Message); + + Assert.Equal("100", exceptionTelemetry.Properties["EventId"]); + Assert.False(exceptionTelemetry.Properties.ContainsKey("EventName")); + }); + var options = new ApplicationInsightsLoggerOptions { IncludeEventId = true }; + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, options); + var exception = new Exception("This is an error"); + logger.LogError(new EventId(100, string.Empty), exception, "Error: " + exception.Message); + } + + /// + /// Tests that logging an exception with results in tracking an instance, + /// when includeEventId is true and parameter is EventName. + /// + [Fact] + public void TestLoggerIncludingEventIdCreatesExceptionWithEventIdTelemetryOnLoggedErrorWithParameters() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var exceptionTelemetry = (ExceptionTelemetry)t; Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel); + Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]); + Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]); + Assert.Equal("Error: ERROR This is an error", exceptionTelemetry.Message); + + Assert.Equal("22", exceptionTelemetry.Properties["EventId"]); + Assert.Equal("ERROR", exceptionTelemetry.Properties["EventName"]); + Assert.Equal("This is an error", exceptionTelemetry.Properties["Message"]); }); + var options = new ApplicationInsightsLoggerOptions { IncludeEventId = true }; + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return true; }, options); + var exception = new Exception("This is an error"); + logger.LogError(new EventId(22, "TestEvent"), exception, "Error: {EventName} {Message}", "ERROR", exception.Message); + } - ILogger logger = new ApplicationInsightsLogger("test", client, (s, l) => { return true; }); + /// + /// Tests that logging an exception results in not tracking an instance, when filter returns false. + /// + [Fact] + public void TestLoggerWithNegativeFilterDoesNotCreateExceptionTelemetryOnLoggedError() + { + bool trackedTelemetry = false; + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => trackedTelemetry = true); + + ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return false; }, defaultOptions); var exception = new Exception("This is an error"); logger.LogError(0, exception, "Error: " + exception.Message); + + Assert.False(trackedTelemetry); } /// @@ -56,7 +397,7 @@ public void TestLoggerCreatesExceptionTelemetryOnLoggedError() [Fact] public void TestUninitializedLoggerDoesNotThrowExceptions() { - ILogger logger = new ApplicationInsightsLogger("test", null, null); + ILogger logger = new ApplicationInsightsLogger("test", null, null, null); logger.LogTrace("This won't do anything.", new object[] { }); } }