Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog

### Version 2.6.0-beta1
### Version 2.6.0-beta3
- [NetStandard Support for TraceListener](https://github.com/Microsoft/ApplicationInsights-dotnet-logging/pull/166)
- [NetStandard Support for NLog and log4net](https://github.com/Microsoft/ApplicationInsights-dotnet-logging/pull/167)
- [NLog and log4net can Flush](https://github.com/Microsoft/ApplicationInsights-dotnet-logging/pull/167)
- Update log4net reference to [2.0.6](https://www.nuget.org/packages/log4net/2.0.6)

### Version 2.6.0-beta2
- [Include NLog GlobalDiagnosticsContext properties](https://github.com/Microsoft/ApplicationInsights-dotnet-logging/pull/152)
- [Remove automatic collection of User Id](https://github.com/Microsoft/ApplicationInsights-dotnet-logging/issues/153)

Expand Down
22 changes: 22 additions & 0 deletions Logging.sln
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorldTest", "HelloWorl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TraceListener.netcoreapp10.Tests", "test\TraceListener.netcoreapp10.Tests\TraceListener.netcoreapp10.Tests.csproj", "{11E4A92F-154E-450B-8508-437C28744BC2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NLogTarget.NetCoreApp10.Tests", "test\NLogTarget.NetCoreApp10.Tests\NLogTarget.NetCoreApp10.Tests.csproj", "{B24CB3C6-A3A0-4190-97E4-E847C7B1A691}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Log4NetAppender.NetCoreApp10.Tests", "test\Log4NetAppender.NetCoreApp10.Tests\Log4NetAppender.NetCoreApp10.Tests.csproj", "{F74826DB-53B1-4588-BD02-4DD25DCBF292}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
test\CommonTestShared\CommonTestShared.projitems*{3b9ab7fa-562d-4e4e-86e3-3348426bc0d9}*SharedItemsImports = 13
Expand Down Expand Up @@ -205,6 +209,22 @@ Global
{11E4A92F-154E-450B-8508-437C28744BC2}.Release|Any CPU.Build.0 = Release|Any CPU
{11E4A92F-154E-450B-8508-437C28744BC2}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{11E4A92F-154E-450B-8508-437C28744BC2}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{B24CB3C6-A3A0-4190-97E4-E847C7B1A691}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B24CB3C6-A3A0-4190-97E4-E847C7B1A691}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B24CB3C6-A3A0-4190-97E4-E847C7B1A691}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{B24CB3C6-A3A0-4190-97E4-E847C7B1A691}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{B24CB3C6-A3A0-4190-97E4-E847C7B1A691}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B24CB3C6-A3A0-4190-97E4-E847C7B1A691}.Release|Any CPU.Build.0 = Release|Any CPU
{B24CB3C6-A3A0-4190-97E4-E847C7B1A691}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{B24CB3C6-A3A0-4190-97E4-E847C7B1A691}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{F74826DB-53B1-4588-BD02-4DD25DCBF292}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F74826DB-53B1-4588-BD02-4DD25DCBF292}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F74826DB-53B1-4588-BD02-4DD25DCBF292}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{F74826DB-53B1-4588-BD02-4DD25DCBF292}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{F74826DB-53B1-4588-BD02-4DD25DCBF292}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F74826DB-53B1-4588-BD02-4DD25DCBF292}.Release|Any CPU.Build.0 = Release|Any CPU
{F74826DB-53B1-4588-BD02-4DD25DCBF292}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{F74826DB-53B1-4588-BD02-4DD25DCBF292}.Release|Mixed Platforms.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -229,6 +249,8 @@ Global
{625DABF6-C4AD-41A9-B2C0-04C9FEC2ADCF} = {2FCC45B3-D820-405D-87FA-467C96465BB1}
{7AAF876A-43B5-4F47-B402-DC5C94948F3F} = {2FCC45B3-D820-405D-87FA-467C96465BB1}
{11E4A92F-154E-450B-8508-437C28744BC2} = {2FCC45B3-D820-405D-87FA-467C96465BB1}
{B24CB3C6-A3A0-4190-97E4-E847C7B1A691} = {2FCC45B3-D820-405D-87FA-467C96465BB1}
{F74826DB-53B1-4588-BD02-4DD25DCBF292} = {2FCC45B3-D820-405D-87FA-467C96465BB1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AC8888B1-0E98-49D7-BE15-EB97A6261C0D}
Expand Down
11 changes: 11 additions & 0 deletions src/Log4NetAppender/ApplicationInsightsAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ public override void ActivateOptions()
this.telemetryClient.Context.GetInternalContext().SdkVersion = SdkVersionUtils.GetSdkVersion("log4net:");
}

/// <summary>
/// Flushes any buffered log data
/// </summary>
/// <param name="millisecondsTimeout">The maximum time to wait for logging events to be flushed</param>
/// <returns>True if all logging events were flushed successfully, else false</returns>
public override bool Flush(int millisecondsTimeout)
{
this.telemetryClient.Flush();
return true;
}

/// <summary>
/// Append LoggingEvent Application Insights logging framework.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Log4NetAppender/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
[assembly: Guid("599daa99-7d0b-4cae-81b8-3a73509f2efa")]

[assembly: InternalsVisibleTo("Microsoft.ApplicationInsights.Log4NetAppender.Net45.Tests, PublicKey=" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("Microsoft.ApplicationInsights.Log4NetAppender.NetCoreApp10.Tests, PublicKey=" + AssemblyInfo.PublicKey)]

internal static class AssemblyInfo
{
Expand Down
11 changes: 9 additions & 2 deletions src/Log4NetAppender/Log4NetAppender.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>

<TargetFrameworks>net45</TargetFrameworks>
<TargetFrameworks>net45;netstandard1.3</TargetFrameworks>

<RootNamespace>Microsoft.ApplicationInsights.Log4NetAppender</RootNamespace>
<AssemblyName>Microsoft.ApplicationInsights.Log4NetAppender</AssemblyName>
Expand Down Expand Up @@ -41,9 +41,16 @@
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.6.0-beta2" />
<PackageReference Include="log4net" Version="2.0.5" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="log4net" Version="2.0.8" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' ">
<PackageReference Include="log4net" Version="2.0.6" />
</ItemGroup>

<ItemGroup>
<Content Include="app.config.install.xdt" />
<Content Include="app.config.uninstall.xdt" />
Expand Down
84 changes: 51 additions & 33 deletions src/NLogTarget/ApplicationInsightsTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ namespace Microsoft.ApplicationInsights.NLogTarget
using System;
using System.Collections.Generic;
using System.Globalization;

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.ApplicationInsights.Implementation;

using NLog;
using NLog.Common;
using NLog.Targets;

/// <summary>
/// NLog Target that routes all logging output to the Application Insights logging framework.
/// The messages will be uploaded to the Application Insights cloud service.
Expand Down Expand Up @@ -49,6 +50,37 @@ internal TelemetryClient TelemetryClient
get { return this.telemetryClient; }
}

internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace)
{
trace.Timestamp = logEvent.TimeStamp;
trace.Sequence = logEvent.SequenceID.ToString(CultureInfo.InvariantCulture);

IDictionary<string, string> propertyBag;

if (trace is ExceptionTelemetry)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it help to use the ISupportProperties interface here? i.e.

propertyBag = ((ISupportProperties)trace).Properties;

Copy link
Copy Markdown
Contributor Author

@snakefoot snakefoot Apr 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just moving code around (stylecop) to fix the unit tests (No PrivateObject in NetCore)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. Fine to leave it as it is, then.

{
propertyBag = ((ExceptionTelemetry)trace).Properties;
}
else
{
propertyBag = ((TraceTelemetry)trace).Properties;
}

if (!string.IsNullOrEmpty(logEvent.LoggerName))
{
propertyBag.Add("LoggerName", logEvent.LoggerName);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider nameof(logEvent.LoggerName)

Copy link
Copy Markdown
Contributor Author

@snakefoot snakefoot Apr 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just moving code around (stylecop) to fix the unit tests (No PrivateObject in NetCore)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine to leave it as it is, then.

}

if (logEvent.UserStackFrame != null)
{
propertyBag.Add("UserStackFrame", logEvent.UserStackFrame.ToString());
propertyBag.Add("UserStackFrameNumber", logEvent.UserStackFrameNumber.ToString(CultureInfo.InvariantCulture));
}

this.LoadGlobalDiagnosticsContextProperties(propertyBag);
this.LoadLogEventProperties(logEvent, propertyBag);
}

/// <summary>
/// Initializes the Target and perform instrumentationKey validation.
/// </summary>
Expand Down Expand Up @@ -86,6 +118,23 @@ protected override void Write(LogEventInfo logEvent)
}
}

/// <summary>
/// Flush any pending log messages
/// </summary>
/// <param name="asyncContinuation">The asynchronous continuation</param>
protected override void FlushAsync(AsyncContinuation asyncContinuation)
{
try
{
this.TelemetryClient.Flush();
asyncContinuation(null);
}
catch (Exception ex)
{
asyncContinuation(ex);
}
}

private void SendException(LogEventInfo logEvent)
{
var exceptionTelemetry = new ExceptionTelemetry(logEvent.Exception)
Expand Down Expand Up @@ -113,37 +162,6 @@ private void SendTrace(LogEventInfo logEvent)
this.telemetryClient.Track(trace);
}

private void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace)
{
trace.Timestamp = logEvent.TimeStamp;
trace.Sequence = logEvent.SequenceID.ToString(CultureInfo.InvariantCulture);

IDictionary<string, string> propertyBag;

if (trace is ExceptionTelemetry)
{
propertyBag = ((ExceptionTelemetry)trace).Properties;
}
else
{
propertyBag = ((TraceTelemetry)trace).Properties;
}

if (!string.IsNullOrEmpty(logEvent.LoggerName))
{
propertyBag.Add("LoggerName", logEvent.LoggerName);
}

if (logEvent.UserStackFrame != null)
{
propertyBag.Add("UserStackFrame", logEvent.UserStackFrame.ToString());
propertyBag.Add("UserStackFrameNumber", logEvent.UserStackFrameNumber.ToString(CultureInfo.InvariantCulture));
}

this.LoadGlobalDiagnosticsContextProperties(propertyBag);
this.LoadLogEventProperties(logEvent, propertyBag);
}

private void LoadGlobalDiagnosticsContextProperties(IDictionary<string, string> propertyBag)
{
foreach (string key in GlobalDiagnosticsContext.GetNames())
Expand Down
1 change: 1 addition & 0 deletions src/NLogTarget/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
[assembly: Guid("44abc834-bd19-449f-ad64-f8c83ca078ed")]

[assembly: InternalsVisibleTo("Microsoft.ApplicationInsights.NLogTarget.Net45.Tests, PublicKey=" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("Microsoft.ApplicationInsights.NLogTarget.NetCoreApp10.Tests, PublicKey=" + AssemblyInfo.PublicKey)]

internal static class AssemblyInfo
{
Expand Down
9 changes: 8 additions & 1 deletion src/NLogTarget/NLogTarget.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>

<TargetFrameworks>net45</TargetFrameworks>
<TargetFrameworks>net45;netstandard1.3</TargetFrameworks>

<RootNamespace>Microsoft.ApplicationInsights.NLogTarget</RootNamespace>
<AssemblyName>Microsoft.ApplicationInsights.NLogTarget</AssemblyName>
Expand Down Expand Up @@ -41,6 +41,13 @@
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.6.0-beta2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="NLog" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' ">
<PackageReference Include="NLog" Version="4.4.12" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft.ApplicationInsights.Log4NetAppender.Tests
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

using log4net;
Expand All @@ -31,6 +32,7 @@ namespace Microsoft.ApplicationInsights.Log4NetAppender.Tests
[TestClass]
public class ApplicationInsightsAppenderTests
{
private static readonly Assembly callingAssembly = typeof(ApplicationInsightsAppenderTests).GetTypeInfo().Assembly;
private AdapterHelper adapterHelper;
private AppendableLogger appendableLogger;

Expand Down Expand Up @@ -92,12 +94,12 @@ public void ValidateLoggingIsWorking()
ApplicationInsightsAppenderTests.InitializeLog4NetAIAdapter(string.Format(@"<InstrumentationKey value=""{0}"" />", instrumentationKey));

// Set up error handler to intercept exception
ApplicationInsightsAppender aiAppender = (ApplicationInsightsAppender)log4net.LogManager.GetRepository().GetAppenders()[0];
ApplicationInsightsAppender aiAppender = (ApplicationInsightsAppender)log4net.LogManager.GetRepository(callingAssembly).GetAppenders()[0];
log4net.Util.OnlyOnceErrorHandler errorHandler = new log4net.Util.OnlyOnceErrorHandler();
aiAppender.ErrorHandler = errorHandler;

// Log something
ILog logger = log4net.LogManager.GetLogger("TestAIAppender");
ILog logger = log4net.LogManager.GetLogger(callingAssembly, "TestAIAppender");
for (int i = 0; i < 1500; i++)
{
logger.Debug("Trace Debug" + i + DateTime.Now);
Expand Down Expand Up @@ -339,15 +341,15 @@ internal static void InitializeLog4NetAIAdapter(string adapterComponentIdSnippet

using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xmlRawText)))
{
XmlConfigurator.Configure(stream);
XmlConfigurator.Configure(log4net.LogManager.GetRepository(callingAssembly), stream);
}
}

private void VerifyInitializationSuccess(Action testAction, string expectedInstrumentationKey)
{
this.VerifyInitializationError(testAction, 0, null);

ApplicationInsightsAppender aiAppender = (ApplicationInsightsAppender)log4net.LogManager.GetRepository().GetAppenders()[0];
ApplicationInsightsAppender aiAppender = (ApplicationInsightsAppender)log4net.LogManager.GetRepository(callingAssembly).GetAppenders()[0];
Assert.AreEqual(expectedInstrumentationKey, aiAppender.InstrumentationKey);
}

Expand Down Expand Up @@ -401,12 +403,12 @@ private IEnumerable<Exception> GetAllInnerExceptions(Exception ex)
private void SendMessagesToMockChannel(string instrumentationKey)
{
// Set up error handler to intercept exception
ApplicationInsightsAppender aiAppender = (ApplicationInsightsAppender)log4net.LogManager.GetRepository().GetAppenders()[0];
ApplicationInsightsAppender aiAppender = (ApplicationInsightsAppender)log4net.LogManager.GetRepository(callingAssembly).GetAppenders()[0];
log4net.Util.OnlyOnceErrorHandler errorHandler = new log4net.Util.OnlyOnceErrorHandler();
aiAppender.ErrorHandler = errorHandler;

// Log something
ILog logger = log4net.LogManager.GetLogger("TestAIAppender");
ILog logger = log4net.LogManager.GetLogger(callingAssembly, "TestAIAppender");
logger.Debug("Trace Debug");
logger.Error("Trace Error");
logger.Fatal("Trace Fatal");
Expand All @@ -422,12 +424,12 @@ private void VerifyPropertiesInTelemetry()
TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;

// Set up error handler to intercept exception
ApplicationInsightsAppender aiAppender = (ApplicationInsightsAppender)LogManager.GetRepository().GetAppenders()[0];
ApplicationInsightsAppender aiAppender = (ApplicationInsightsAppender)LogManager.GetRepository(callingAssembly).GetAppenders()[0];
OnlyOnceErrorHandler errorHandler = new OnlyOnceErrorHandler();
aiAppender.ErrorHandler = errorHandler;

// Log something
ILog logger = LogManager.GetLogger("TestAIAppender");
ILog logger = LogManager.GetLogger(callingAssembly, "TestAIAppender");
logger.Debug("Trace Debug");

ITelemetry[] sentItems = this.adapterHelper.Channel.SentItems;
Expand Down Expand Up @@ -463,11 +465,11 @@ public AppendableLogger()
ApplicationInsightsAppenderTests.InitializeLog4NetAIAdapter(string.Empty);

// Set up error handler to intercept exception
var aiAppender = (ApplicationInsightsAppender)LogManager.GetRepository().GetAppenders()[0];
var aiAppender = (ApplicationInsightsAppender)LogManager.GetRepository(callingAssembly).GetAppenders()[0];
var errorHandler = new OnlyOnceErrorHandler();
aiAppender.ErrorHandler = errorHandler;

this.Logger = LogManager.GetLogger("TestAIAppender");
this.Logger = LogManager.GetLogger(callingAssembly, "TestAIAppender");
}

public ILog Logger { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="Microbuild.Core" Version="0.2.0">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="log4net" Version="2.0.5" />
<PackageReference Include="log4net" Version="2.0.6" />
<ProjectReference Include="..\..\src\Log4NetAppender\Log4NetAppender.csproj" />
<Reference Include="System.Net.Http" />

Expand Down
Loading