Skip to content

Commit

Permalink
Adjusted unit-test to handle isolated TelemetryClient
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Aug 13, 2024
1 parent ea79e58 commit 5a8548d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 22 deletions.
14 changes: 4 additions & 10 deletions LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ namespace Microsoft.ApplicationInsights.NLogTarget
public sealed class ApplicationInsightsTarget : TargetWithLayout
{
private TelemetryClient telemetryClient;
private DateTime lastLogEventTime;
private NLog.Layouts.Layout instrumentationKeyLayout = string.Empty;
private NLog.Layouts.Layout connectionStringLayout = string.Empty;

Expand Down Expand Up @@ -68,12 +67,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 TelemetryClient, so unit-tests can assign in-memory-channel.
/// </summary>
internal TelemetryClient TelemetryClient
{
get { return this.telemetryClient; }
}
internal Func<TelemetryConfiguration, TelemetryClient> TelemetryClientFactory { get; set; } = new Func<TelemetryConfiguration, TelemetryClient>((cfg) => new TelemetryClient(cfg));

internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace)
{
Expand Down Expand Up @@ -134,7 +130,7 @@ protected override void InitializeTarget()
{
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net7.0)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net7.0)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net7.0)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net7.0)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net462)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net462)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net462)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net462)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net472)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net472)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net472)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net472)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net6.0)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net6.0)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net6.0)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net6.0)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net481)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net481)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net481)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net481)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net480)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net480)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net480)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net480)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net8.0)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net8.0)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net8.0)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net8.0)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net452)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net452)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net452)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, net452)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 131 in LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs

View workflow job for this annotation

GitHub Actions / build-test-LOGGING (windows-latest, netcoreapp3.1)

Call System.IDisposable.Dispose on object created by 'TelemetryConfiguration.CreateDefault()' before all references to it are out of scope (https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)
telemetryConfiguration.ConnectionString = connectionString;
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
this.telemetryClient = this.TelemetryClientFactory(telemetryConfiguration);
}
else
{
Expand Down Expand Up @@ -162,8 +158,6 @@ protected override void Write(LogEventInfo logEvent)
throw new ArgumentNullException(nameof(logEvent));
}

this.lastLogEventTime = DateTime.UtcNow;

if (logEvent.Exception != null)
{
this.SendException(logEvent);
Expand All @@ -187,7 +181,7 @@ 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)
{
Expand Down
7 changes: 2 additions & 5 deletions LOGGING/test/NLogTarget.Tests/NLogTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,16 +565,13 @@ private Logger CreateTargetWithGivenConnectionString(
string connectionString = "Your_ApplicationInsights_ConnectionString",
Action<Logger> loggerAction = null)
{
// Mock channel to validate that our appender is trying to send logs
#pragma warning disable CS0618 // Type or member is obsolete
TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;
#pragma warning restore CS0618 // Type or member is obsolete

ApplicationInsightsTarget target = new ApplicationInsightsTarget
{
ConnectionString = connectionString
};

target.TelemetryClientFactory = (cfg) => { cfg.TelemetryChannel = this.adapterHelper.Channel; return new TelemetryClient(cfg); };

LoggingRule rule = new LoggingRule("*", LogLevel.Trace, target);
LoggingConfiguration config = new LoggingConfiguration();
config.AddTarget("AITarget", target);
Expand Down
9 changes: 2 additions & 7 deletions LOGGING/test/Shared/AdapterHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public class AdapterHelper : IDisposable
{
public string InstrumentationKey { get; }

public string ConnectionString { get; }

#if NET452 || NET46
private static readonly string ApplicationInsightsConfigFilePath =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ApplicationInsights.config");
Expand All @@ -29,19 +27,16 @@ public class AdapterHelper : IDisposable
Path.Combine(Path.GetDirectoryName(typeof(AdapterHelper).GetTypeInfo().Assembly.Location), "ApplicationInsights.config");
#endif

public AdapterHelper(string instrumentationKey = "F8474271-D231-45B6-8DD4-D344C309AE69"
, string connectionString = "Your_ApplicationInsights_ConnectionString")
public AdapterHelper(string instrumentationKey = "F8474271-D231-45B6-8DD4-D344C309AE69")
{
this.InstrumentationKey = instrumentationKey;
this.ConnectionString = connectionString;

string configuration = string.Format(InvariantCulture,
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<ApplicationInsights xmlns=""http://schemas.microsoft.com/ApplicationInsights/2013/Settings"">
<InstrumentationKey>{0}</InstrumentationKey>
</ApplicationInsights>",
instrumentationKey,
connectionString);
instrumentationKey);

File.WriteAllText(ApplicationInsightsConfigFilePath, configuration);
this.Channel = new CustomTelemetryChannel();
Expand Down

0 comments on commit 5a8548d

Please sign in to comment.