Skip to content

Commit 0d7d2fd

Browse files
authored
NLog ApplicationInsightsTarget InitializeTarget requires ConnectionString (#3014)
1 parent e38ed32 commit 0d7d2fd

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public sealed class ApplicationInsightsTarget : TargetWithLayout
3131
private TelemetryConfiguration telemetryConfiguration;
3232
private DateTime lastLogEventTime;
3333
private NLog.Layouts.Layout connectionStringLayout = string.Empty;
34-
private bool initializationFailed;
3534

3635
/// <summary>
3736
/// Initializers a new instance of ApplicationInsightsTarget type.
@@ -114,16 +113,14 @@ internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace)
114113
protected override void InitializeTarget()
115114
{
116115
base.InitializeTarget();
117-
this.telemetryConfiguration = new TelemetryConfiguration();
118-
this.initializationFailed = false;
119116

120117
string connectionString = this.connectionStringLayout.Render(LogEventInfo.CreateNullEvent());
121118
if (string.IsNullOrWhiteSpace(connectionString))
122119
{
123-
this.initializationFailed = true;
124-
return;
120+
throw new NLogConfigurationException(ConnectionStringRequiredMessage);
125121
}
126122

123+
this.telemetryConfiguration = new TelemetryConfiguration();
127124
this.telemetryConfiguration.ConnectionString = connectionString;
128125
this.telemetryClient = new TelemetryClient(this.telemetryConfiguration);
129126

@@ -140,7 +137,6 @@ protected override void CloseTarget()
140137
this.telemetryConfiguration?.Dispose();
141138
this.telemetryConfiguration = null;
142139
this.telemetryClient = null;
143-
this.initializationFailed = false;
144140
}
145141

146142
/// <summary>
@@ -170,9 +166,9 @@ protected override void Write(LogEventInfo logEvent)
170166
throw new ArgumentNullException(nameof(logEvent));
171167
}
172168

173-
if (this.telemetryClient == null || this.initializationFailed)
169+
if (this.telemetryClient == null)
174170
{
175-
throw new NLogConfigurationException(ConnectionStringRequiredMessage);
171+
throw new NLogRuntimeException(ConnectionStringRequiredMessage);
176172
}
177173

178174
this.lastLogEventTime = DateTime.UtcNow;
@@ -200,25 +196,24 @@ protected override void FlushAsync(AsyncContinuation asyncContinuation)
200196

201197
try
202198
{
203-
if (this.telemetryClient == null || this.initializationFailed)
204-
{
205-
InternalLogger.Debug("ApplicationInsightsTarget.FlushAsync skipped - telemetry client not initialized.");
206-
asyncContinuation(null);
207-
return;
208-
}
209-
210-
InternalLogger.Debug("ApplicationInsightsTarget.FlushAsync flushing telemetry client.");
211-
this.TelemetryClient.Flush();
212-
if (DateTime.UtcNow.AddSeconds(-30) > this.lastLogEventTime)
199+
if (this.telemetryClient == null)
213200
{
214-
// Nothing has been written, so nothing to wait for
215201
asyncContinuation(null);
216202
}
217203
else
218204
{
219-
// Documentation says it is important to wait after flush, else nothing will happen
220-
// https://docs.microsoft.com/azure/application-insights/app-insights-api-custom-events-metrics#flushing-data
221-
System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(500)).ContinueWith((task) => asyncContinuation(null));
205+
this.TelemetryClient.Flush();
206+
if (DateTime.UtcNow.AddSeconds(-30) > this.lastLogEventTime)
207+
{
208+
// Nothing has been written, so nothing to wait for
209+
asyncContinuation(null);
210+
}
211+
else
212+
{
213+
// Documentation says it is important to wait after flush, else nothing will happen
214+
// https://docs.microsoft.com/azure/application-insights/app-insights-api-custom-events-metrics#flushing-data
215+
System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(500)).ContinueWith((task) => asyncContinuation(null));
216+
}
222217
}
223218
}
224219
catch (Exception ex)

LOGGING/test/NLogTarget.Tests/NLogTargetTests.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ public void InitializeTargetThrowsWhenConnectionStringIsNull()
6161
config.AddTarget("AITarget", target);
6262
config.LoggingRules.Add(rule);
6363

64-
LogManager.Configuration = config;
65-
var logger = LogManager.GetLogger("AITarget");
66-
67-
Assert.ThrowsException<NLogConfigurationException>(() => logger.Info("trigger"));
64+
Assert.ThrowsException<NLogConfigurationException>(() =>
65+
{
66+
LogManager.Configuration = config;
67+
var logger = LogManager.GetLogger("AITarget");
68+
logger.Info("trigger");
69+
});
6870
}
6971

7072
[TestMethod]
@@ -81,10 +83,12 @@ public void InitializeTargetThrowsWhenConnectionStringIsEmpty()
8183
config.AddTarget("AITarget", target);
8284
config.LoggingRules.Add(rule);
8385

84-
LogManager.Configuration = config;
85-
var logger = LogManager.GetLogger("AITarget");
86-
87-
Assert.ThrowsException<NLogConfigurationException>(() => logger.Info("trigger"));
86+
Assert.ThrowsException<NLogConfigurationException>(() =>
87+
{
88+
LogManager.Configuration = config;
89+
var logger = LogManager.GetLogger("AITarget");
90+
logger.Info("trigger");
91+
});
8892
}
8993

9094
[TestMethod]

examples/NLogConsoleApp/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
Console.WriteLine("NLog Console App - Application Insights Example");
44
Console.WriteLine("================================================\n");
55

6+
NLog.Common.InternalLogger.LogToConsole = true;
7+
NLog.Common.InternalLogger.LogLevel = NLog.LogLevel.Warn;
8+
69
// Get NLog logger - the ApplicationInsightsTarget will handle telemetry
710
var logger = LogManager.GetCurrentClassLogger();
811

0 commit comments

Comments
 (0)