@@ -30,7 +30,6 @@ namespace Microsoft.ApplicationInsights.NLogTarget
30
30
public sealed class ApplicationInsightsTarget : TargetWithLayout
31
31
{
32
32
private TelemetryClient telemetryClient ;
33
- private DateTime lastLogEventTime ;
34
33
private NLog . Layouts . Layout instrumentationKeyLayout = string . Empty ;
35
34
private NLog . Layouts . Layout connectionStringLayout = string . Empty ;
36
35
@@ -68,12 +67,9 @@ public string ConnectionString
68
67
public IList < TargetPropertyWithContext > ContextProperties { get ; } = new List < TargetPropertyWithContext > ( ) ;
69
68
70
69
/// <summary>
71
- /// Gets the logging controller we will be using .
70
+ /// Gets or sets the factory for creating TelemetryClient, so unit-tests can assign in-memory-channel .
72
71
/// </summary>
73
- internal TelemetryClient TelemetryClient
74
- {
75
- get { return this . telemetryClient ; }
76
- }
72
+ internal Func < TelemetryConfiguration , TelemetryClient > TelemetryClientFactory { get ; set ; } = new Func < TelemetryConfiguration , TelemetryClient > ( ( cfg ) => new TelemetryClient ( cfg ) ) ;
77
73
78
74
internal void BuildPropertyBag ( LogEventInfo logEvent , ITelemetry trace )
79
75
{
@@ -134,7 +130,7 @@ protected override void InitializeTarget()
134
130
{
135
131
var telemetryConfiguration = TelemetryConfiguration . CreateDefault ( ) ;
136
132
telemetryConfiguration . ConnectionString = connectionString ;
137
- this . telemetryClient = new TelemetryClient ( telemetryConfiguration ) ;
133
+ this . telemetryClient = this . TelemetryClientFactory ( telemetryConfiguration ) ;
138
134
}
139
135
else
140
136
{
@@ -162,8 +158,6 @@ protected override void Write(LogEventInfo logEvent)
162
158
throw new ArgumentNullException ( nameof ( logEvent ) ) ;
163
159
}
164
160
165
- this . lastLogEventTime = DateTime . UtcNow ;
166
-
167
161
if ( logEvent . Exception != null )
168
162
{
169
163
this . SendException ( logEvent ) ;
@@ -187,7 +181,7 @@ protected override void FlushAsync(AsyncContinuation asyncContinuation)
187
181
188
182
try
189
183
{
190
- this . TelemetryClient . FlushAsync ( System . Threading . CancellationToken . None ) . ContinueWith ( t => asyncContinuation ( t . Exception ) ) ;
184
+ this . telemetryClient . FlushAsync ( System . Threading . CancellationToken . None ) . ContinueWith ( t => asyncContinuation ( t . Exception ) ) ;
191
185
}
192
186
catch ( Exception ex )
193
187
{
0 commit comments