Skip to content

Commit 5a8548d

Browse files
committed
Adjusted unit-test to handle isolated TelemetryClient
1 parent ea79e58 commit 5a8548d

File tree

3 files changed

+8
-22
lines changed

3 files changed

+8
-22
lines changed

LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ namespace Microsoft.ApplicationInsights.NLogTarget
3030
public sealed class ApplicationInsightsTarget : TargetWithLayout
3131
{
3232
private TelemetryClient telemetryClient;
33-
private DateTime lastLogEventTime;
3433
private NLog.Layouts.Layout instrumentationKeyLayout = string.Empty;
3534
private NLog.Layouts.Layout connectionStringLayout = string.Empty;
3635

@@ -68,12 +67,9 @@ public string ConnectionString
6867
public IList<TargetPropertyWithContext> ContextProperties { get; } = new List<TargetPropertyWithContext>();
6968

7069
/// <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.
7271
/// </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));
7773

7874
internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace)
7975
{
@@ -134,7 +130,7 @@ protected override void InitializeTarget()
134130
{
135131
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
136132
telemetryConfiguration.ConnectionString = connectionString;
137-
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
133+
this.telemetryClient = this.TelemetryClientFactory(telemetryConfiguration);
138134
}
139135
else
140136
{
@@ -162,8 +158,6 @@ protected override void Write(LogEventInfo logEvent)
162158
throw new ArgumentNullException(nameof(logEvent));
163159
}
164160

165-
this.lastLogEventTime = DateTime.UtcNow;
166-
167161
if (logEvent.Exception != null)
168162
{
169163
this.SendException(logEvent);
@@ -187,7 +181,7 @@ protected override void FlushAsync(AsyncContinuation asyncContinuation)
187181

188182
try
189183
{
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));
191185
}
192186
catch (Exception ex)
193187
{

LOGGING/test/NLogTarget.Tests/NLogTargetTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,16 +565,13 @@ private Logger CreateTargetWithGivenConnectionString(
565565
string connectionString = "Your_ApplicationInsights_ConnectionString",
566566
Action<Logger> loggerAction = null)
567567
{
568-
// Mock channel to validate that our appender is trying to send logs
569-
#pragma warning disable CS0618 // Type or member is obsolete
570-
TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;
571-
#pragma warning restore CS0618 // Type or member is obsolete
572-
573568
ApplicationInsightsTarget target = new ApplicationInsightsTarget
574569
{
575570
ConnectionString = connectionString
576571
};
577572

573+
target.TelemetryClientFactory = (cfg) => { cfg.TelemetryChannel = this.adapterHelper.Channel; return new TelemetryClient(cfg); };
574+
578575
LoggingRule rule = new LoggingRule("*", LogLevel.Trace, target);
579576
LoggingConfiguration config = new LoggingConfiguration();
580577
config.AddTarget("AITarget", target);

LOGGING/test/Shared/AdapterHelper.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public class AdapterHelper : IDisposable
1919
{
2020
public string InstrumentationKey { get; }
2121

22-
public string ConnectionString { get; }
23-
2422
#if NET452 || NET46
2523
private static readonly string ApplicationInsightsConfigFilePath =
2624
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ApplicationInsights.config");
@@ -29,19 +27,16 @@ public class AdapterHelper : IDisposable
2927
Path.Combine(Path.GetDirectoryName(typeof(AdapterHelper).GetTypeInfo().Assembly.Location), "ApplicationInsights.config");
3028
#endif
3129

32-
public AdapterHelper(string instrumentationKey = "F8474271-D231-45B6-8DD4-D344C309AE69"
33-
, string connectionString = "Your_ApplicationInsights_ConnectionString")
30+
public AdapterHelper(string instrumentationKey = "F8474271-D231-45B6-8DD4-D344C309AE69")
3431
{
3532
this.InstrumentationKey = instrumentationKey;
36-
this.ConnectionString = connectionString;
3733

3834
string configuration = string.Format(InvariantCulture,
3935
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
4036
<ApplicationInsights xmlns=""http://schemas.microsoft.com/ApplicationInsights/2013/Settings"">
4137
<InstrumentationKey>{0}</InstrumentationKey>
4238
</ApplicationInsights>",
43-
instrumentationKey,
44-
connectionString);
39+
instrumentationKey);
4540

4641
File.WriteAllText(ApplicationInsightsConfigFilePath, configuration);
4742
this.Channel = new CustomTelemetryChannel();

0 commit comments

Comments
 (0)