diff --git a/CHANGELOG.md b/CHANGELOG.md index 566f8075..d24ac90b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - [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) +- [ApplicationInsightsLogger logs exceptions as ExceptionTelemetry by default. This can now be configured with ApplicationInsightsLoggerOptions.TrackExceptionsAsExceptionTelemetry] (https://github.com/Microsoft/ApplicationInsights-aspnetcore/pull/574) ## 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 40334399..87d4dfff 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLogger.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLogger.cs @@ -50,7 +50,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except if (this.IsEnabled(logLevel)) { var stateDictionary = state as IReadOnlyList>; - if (exception == null) + if (exception == null || this.options?.TrackExceptionsAsExceptionTelemetry == false) { var traceTelemetry = new TraceTelemetry(formatter(state, exception), this.GetSeverityLevel(logLevel)); PopulateTelemetry(traceTelemetry, stateDictionary, eventId); diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerOptions.cs b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerOptions.cs index 68417612..f9236910 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerOptions.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerOptions.cs @@ -1,13 +1,31 @@ -namespace Microsoft.ApplicationInsights.AspNetCore.Logging +using Microsoft.ApplicationInsights.DataContracts; +using Microsoft.Extensions.Logging; + +namespace Microsoft.ApplicationInsights.AspNetCore.Logging { /// - /// Options for configuration of . + /// defines the custom behavior of the tracing information sent to Application Insights. /// public class ApplicationInsightsLoggerOptions { + /// + /// Initializes a new instance of the class. + /// Application Insights logger options can configure how behaves when sending telemetry. + /// + public ApplicationInsightsLoggerOptions() + { + TrackExceptionsAsExceptionTelemetry = true; + } + + /// + /// Gets or sets a value whether to track exceptions as . + /// + public bool TrackExceptionsAsExceptionTelemetry + { get; set; } + /// /// Gets or sets value indicating, whether EventId and EventName properties should be included in telemetry. /// public bool IncludeEventId { get; set; } } -} +} \ No newline at end of file diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerProvider.cs b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerProvider.cs index f38c3792..fe247d86 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerProvider.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerProvider.cs @@ -22,6 +22,7 @@ public ApplicationInsightsLoggerProvider(TelemetryClient telemetryClient, Func diff --git a/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Logging/ApplicationInsightsLoggerTests.cs b/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Logging/ApplicationInsightsLoggerTests.cs index e489fbb9..c83957dc 100644 --- a/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Logging/ApplicationInsightsLoggerTests.cs +++ b/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Logging/ApplicationInsightsLoggerTests.cs @@ -386,11 +386,48 @@ public void TestLoggerWithNegativeFilterDoesNotCreateExceptionTelemetryOnLoggedE 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); } + [Fact] + public void TestLoggerCreatesTraceTelemetryOnLoggedErrorWhenTrackExceptionsAsExceptionTelemetryIsSetToFalse() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var traceTelemetry = (TraceTelemetry)t; + + Assert.Equal("Error: This is an error", traceTelemetry.Message); + Assert.Equal(SeverityLevel.Error, traceTelemetry.SeverityLevel); + }); + + ILogger logger = new ApplicationInsightsLogger("test", client, (s, l) => { return true; }, new ApplicationInsightsLoggerOptions { TrackExceptionsAsExceptionTelemetry = false }); + var exception = new Exception("This is an error"); + + logger.LogError(0, exception, "Error: " + exception.Message); + } + + [Fact] + public void TestLoggerCreatesExceptionTelemetryOnLoggedErrorWhenTrackExceptionsAsExceptionTelemetryIsSetToTrue() + { + TelemetryClient client = CommonMocks.MockTelemetryClient((t) => + { + Assert.IsType(t); + var exceptionTelemetry = (ExceptionTelemetry)t; + + Assert.Equal("Error: This is an error", exceptionTelemetry.Message); + Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel); + }); + + ILogger logger = new ApplicationInsightsLogger("test", client, (s, l) => { return true; }, new ApplicationInsightsLoggerOptions { TrackExceptionsAsExceptionTelemetry = true }); + var exception = new Exception("This is an error"); + + logger.LogError(0, exception, "Error: " + exception.Message); + } + /// /// Tests that an incorrectly constructed or uninitialized Application Insights ILogger does not throw exceptions. ///