Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

added specific ApplicationInsightsLoggerOptions to configure logging #574

Merged
merged 7 commits into from
Mar 19, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
if (this.IsEnabled(logLevel))
{
var stateDictionary = state as IReadOnlyList<KeyValuePair<string, object>>;
if (exception == null)
if (exception == null || this.options?.TrackExceptionsAsExceptionTelemetry == false)
{
var traceTelemetry = new TraceTelemetry(formatter(state, exception), this.GetSeverityLevel(logLevel));
PopulateTelemetry(traceTelemetry, stateDictionary, eventId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
namespace Microsoft.ApplicationInsights.AspNetCore.Logging
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Extensions.Logging;

namespace Microsoft.ApplicationInsights.AspNetCore.Logging
{
/// <summary>
/// Options for configuration of <see cref="ApplicationInsightsLogger"/>.
/// <see cref="ApplicationInsightsLoggerOptions"/> defines the custom behavior of the tracing information sent to Application Insights.
/// </summary>
public class ApplicationInsightsLoggerOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationInsightsLoggerOptions" /> class.
/// Application Insights logger options can configure how <see cref="ILogger"/> behaves when sending telemetry.
/// </summary>
public ApplicationInsightsLoggerOptions()
{
TrackExceptionsAsExceptionTelemetry = true;
}

/// <summary>
/// Gets or sets a value whether to track exceptions as <see cref="ExceptionTelemetry"/>.
/// </summary>
public bool TrackExceptionsAsExceptionTelemetry
{ get; set; }

/// <summary>
/// Gets or sets value indicating, whether EventId and EventName properties should be included in telemetry.
/// </summary>
public bool IncludeEventId { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public ApplicationInsightsLoggerProvider(TelemetryClient telemetryClient, Func<s
this.telemetryClient = telemetryClient;
this.filter = filter;
this.options = options.Value;

}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TraceTelemetry>(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<ExceptionTelemetry>(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);
}

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