@@ -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 )
0 commit comments