-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ namespace Microsoft.ApplicationInsights.NLogTarget | |
public sealed class ApplicationInsightsTarget : TargetWithLayout | ||
{ | ||
private TelemetryClient telemetryClient; | ||
private DateTime lastLogEventTime; | ||
private TelemetryConfiguration telemetryConfiguration; | ||
private NLog.Layouts.Layout instrumentationKeyLayout = string.Empty; | ||
private NLog.Layouts.Layout connectionStringLayout = string.Empty; | ||
|
||
|
@@ -68,12 +68,9 @@ public string ConnectionString | |
public IList<TargetPropertyWithContext> ContextProperties { get; } = new List<TargetPropertyWithContext>(); | ||
|
||
/// <summary> | ||
/// Gets the logging controller we will be using. | ||
/// Gets or sets the factory for creating TelemetryConfiguration, so unit-tests can override in-memory-channel. | ||
/// </summary> | ||
internal TelemetryClient TelemetryClient | ||
{ | ||
get { return this.telemetryClient; } | ||
} | ||
internal Func<TelemetryConfiguration> TelemetryConfigurationFactory { get; set; } | ||
|
||
internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace) | ||
{ | ||
|
@@ -119,7 +116,7 @@ internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace) | |
} | ||
|
||
/// <summary> | ||
/// Initializes the Target and perform instrumentationKey validation. | ||
/// Initializes the Target and configures TelemetryClient | ||
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net7.0)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net7.0)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net7.0)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net7.0)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net481)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net481)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net481)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net481)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net8.0)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net8.0)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net8.0)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net8.0)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net452)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net452)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net452)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net452)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net462)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net462)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net462)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net462)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net6.0)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net6.0)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net6.0)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net6.0)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net472)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net472)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net472)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net472)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net480)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net480)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net480)
Check failure on line 119 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net480)
|
||
/// </summary> | ||
/// <exception cref="NLogConfigurationException">Will throw when <see cref="InstrumentationKey"/> is not set.</exception> | ||
protected override void InitializeTarget() | ||
|
@@ -132,9 +129,9 @@ protected override void InitializeTarget() | |
// configure new telemetryclient with the connectionstring otherwise using legacy instrumentationkey. | ||
if (!string.IsNullOrWhiteSpace(connectionString)) | ||
{ | ||
var telemetryConfiguration = TelemetryConfiguration.CreateDefault(); | ||
telemetryConfiguration.ConnectionString = connectionString; | ||
this.telemetryClient = new TelemetryClient(telemetryConfiguration); | ||
this.telemetryConfiguration = this.TelemetryConfigurationFactory?.Invoke() ?? TelemetryConfiguration.CreateDefault(); | ||
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net7.0)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net7.0)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net7.0)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net7.0)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net481)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net481)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net481)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net481)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net8.0)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net8.0)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net8.0)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net8.0)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net452)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net452)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net452)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net452)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net462)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net462)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net462)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net462)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net6.0)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net6.0)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net6.0)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net6.0)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net472)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net472)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net472)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net472)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net480)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net480)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net480)
Check warning on line 132 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net480)
|
||
this.telemetryConfiguration.ConnectionString = connectionString; | ||
this.telemetryClient = new TelemetryClient(this.telemetryConfiguration); | ||
} | ||
else | ||
{ | ||
|
@@ -151,6 +148,18 @@ protected override void InitializeTarget() | |
this.telemetryClient.Context.GetInternalContext().SdkVersion = SdkVersionUtils.GetSdkVersion("nlog:"); | ||
} | ||
|
||
/// <summary> | ||
/// Closes the target and releases resources used by the current instance of the <see cref="ApplicationInsightsTarget"/> class. | ||
/// </summary> | ||
protected override void CloseTarget() | ||
{ | ||
var telemetryConfiguration = this.telemetryConfiguration; | ||
this.telemetryConfiguration = null; | ||
telemetryConfiguration?.Dispose(); | ||
|
||
base.CloseTarget(); | ||
} | ||
|
||
/// <summary> | ||
/// Send the log message to Application Insights. | ||
/// </summary> | ||
|
@@ -162,8 +171,6 @@ protected override void Write(LogEventInfo logEvent) | |
throw new ArgumentNullException(nameof(logEvent)); | ||
} | ||
|
||
this.lastLogEventTime = DateTime.UtcNow; | ||
|
||
if (logEvent.Exception != null) | ||
{ | ||
this.SendException(logEvent); | ||
|
@@ -187,14 +194,26 @@ protected override void FlushAsync(AsyncContinuation asyncContinuation) | |
|
||
try | ||
{ | ||
this.TelemetryClient.FlushAsync(System.Threading.CancellationToken.None).ContinueWith(t => asyncContinuation(t.Exception)); | ||
this.telemetryClient.FlushAsync(System.Threading.CancellationToken.None).ContinueWith(t => asyncContinuation(t.Exception)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
asyncContinuation(ex); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Releases resources used by the current instance of the <see cref="ApplicationInsightsTarget"/> class. | ||
/// </summary> | ||
/// <param name="disposing"></param> | ||
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net7.0)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net7.0)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net7.0)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net7.0)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net481)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net481)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net481)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net481)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net8.0)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net8.0)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net8.0)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net8.0)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net452)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net452)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net452)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net452)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net462)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net462)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net462)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net462)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net6.0)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net6.0)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net6.0)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net6.0)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net472)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net472)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net472)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net472)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net480)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net480)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net480)
Check failure on line 208 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs GitHub Actions / build-test-LOGGING (windows-latest, net480)
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
base.Dispose(disposing); | ||
var telemetryConfiguration = this.telemetryConfiguration; | ||
this.telemetryConfiguration = null; | ||
telemetryConfiguration?.Dispose(); | ||
} | ||
|
||
private static void LoadLogEventProperties(LogEventInfo logEvent, IDictionary<string, string> propertyBag) | ||
{ | ||
if (logEvent.Properties?.Count > 0) | ||
|