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 f2ebc85
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 30 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 TelemetryConfiguration, so unit-tests can override in-memory-channel.
/// </summary>
internal TelemetryClient TelemetryClient
{
get { return this.telemetryClient; }
}
internal Func<TelemetryConfiguration> TelemetryConfigurationFactory { get; set; }

internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace)
{
Expand Down Expand Up @@ -132,7 +128,7 @@ protected override void InitializeTarget()
// configure new telemetryclient with the connectionstring otherwise using legacy instrumentationkey.
if (!string.IsNullOrWhiteSpace(connectionString))
{
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
var telemetryConfiguration = this.TelemetryConfigurationFactory?.Invoke() ?? 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, 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, 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, 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, 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, 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)

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, 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, 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)
telemetryConfiguration.ConnectionString = connectionString;
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
}
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
21 changes: 8 additions & 13 deletions LOGGING/test/NLogTarget.Tests/NLogTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,13 @@ public void NLogTargetFlushesTelemetryClient()
NLog.Common.AsyncContinuation asyncContinuation = (ex) => { flushException = ex; flushEvent.Set(); };
aiLogger.Factory.Flush(asyncContinuation, 5000);
Assert.IsTrue(flushEvent.WaitOne(5000));
Assert.IsNotNull(flushException);
Assert.AreEqual("Flush called", flushException.Message);
}

[TestMethod]
[TestCategory("NLogTarget")]
public void NLogInfoIsSentAsInformationTraceItemWithAIConnectionString()
{
var aiLogger = this.CreateTargetWithGivenConnectionString("Your_ApplicationInsights_ConnectionString");
var aiLogger = this.CreateTargetWithGivenConnectionString();
aiLogger.Info("Info message");

var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.First();
Expand All @@ -474,7 +472,7 @@ public void NLogInfoIsSentAsInformationTraceItemWithAIConnectionString()
[TestCategory("NLogTarget")]
public void NLogTraceIsSentAsVerboseTraceItemWithAIConnectionString()
{
var aiLogger = this.CreateTargetWithGivenConnectionString("Your_ApplicationInsights_ConnectionString");
var aiLogger = this.CreateTargetWithGivenConnectionString();
aiLogger.Trace("Trace message");

var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault();
Expand All @@ -485,7 +483,7 @@ public void NLogTraceIsSentAsVerboseTraceItemWithAIConnectionString()
[TestCategory("NLogTarget")]
public void NLogDebugIsSentAsVerboseTraceItemWithAIConnectionString()
{
var aiLogger = this.CreateTargetWithGivenConnectionString("Your_ApplicationInsights_ConnectionString");
var aiLogger = this.CreateTargetWithGivenConnectionString();
aiLogger.Debug("Debug Message");

var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault();
Expand All @@ -496,7 +494,7 @@ public void NLogDebugIsSentAsVerboseTraceItemWithAIConnectionString()
[TestCategory("NLogTarget")]
public void NLogWarnIsSentAsWarningTraceItemWithAIConnectionString()
{
var aiLogger = this.CreateTargetWithGivenConnectionString("Your_ApplicationInsights_ConnectionString");
var aiLogger = this.CreateTargetWithGivenConnectionString();

aiLogger.Warn("Warn message");

Expand All @@ -508,7 +506,7 @@ public void NLogWarnIsSentAsWarningTraceItemWithAIConnectionString()
[TestCategory("NLogTarget")]
public void NLogErrorIsSentAsVerboseTraceItemWithAIConnectionString()
{
var aiLogger = this.CreateTargetWithGivenConnectionString("InstrumentationKey=b91a8f48-c77c-4f12-80e2-f96bc1abb126;IngestionEndpoint=https://centralus-2.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/");
var aiLogger = this.CreateTargetWithGivenConnectionString();
aiLogger.Error("Error Message");

var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault();
Expand Down Expand Up @@ -562,19 +560,16 @@ private Logger CreateTargetWithGivenInstrumentationKey(
}

private Logger CreateTargetWithGivenConnectionString(
string connectionString = "Your_ApplicationInsights_ConnectionString",
string connectionString = "InstrumentationKey=b91a8f48-c77c-4f12-80e2-f96bc1abb126;IngestionEndpoint=https://localhost/;LiveEndpoint=https://localhost/",
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.TelemetryConfigurationFactory = () => new TelemetryConfiguration() { TelemetryChannel = this.adapterHelper.Channel };

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 f2ebc85

Please sign in to comment.