-
Notifications
You must be signed in to change notification settings - Fork 49
NLog + log4net added NetStandard support #167
Changes from 2 commits
6043faa
7ff620e
f545052
2e03b9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it help to use the propertyBag = ((ISupportProperties)trace).Properties;
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
|
@@ -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) | ||
|
|
@@ -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()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <RootNamespace>Microsoft.ApplicationInsights.Log4NetAppender.Tests</RootNamespace> | ||
| <AssemblyName>Microsoft.ApplicationInsights.Log4NetAppender.NetCoreApp10.Tests</AssemblyName> | ||
| <TargetFramework>netcoreapp1.0</TargetFramework> | ||
| <GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="..\Log4NetAppender.Net45.Tests\ApplicationInsightsAppenderTests.cs" Link="ApplicationInsightsAppenderTests.cs" /> | ||
| <Compile Include="..\Log4NetAppender.Net45.Tests\GlobalSuppressions.cs" Link="GlobalSuppressions.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> | ||
| <PackageReference Include="MSTest.TestAdapter" Version="1.2.0" /> | ||
| <PackageReference Include="MSTest.TestFramework" Version="1.2.0" /> | ||
| <PackageReference Include="Microsoft.ApplicationInsights" Version="2.6.0-beta2" /> | ||
| <PackageReference Include="Microbuild.Core" Version="0.2.0"> | ||
| <PrivateAssets>All</PrivateAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="log4net" Version="2.0.8" /> | ||
| <ProjectReference Include="..\..\src\Log4NetAppender\Log4NetAppender.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <!-- Identifies the project as test project --> | ||
| <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | ||
| </ItemGroup> | ||
|
|
||
| <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), 'Test.props'))\Test.props" /> | ||
|
|
||
| <Import Project="..\Shared\Adapters.Shared.Tests.projitems" Label="Shared" Condition="Exists('..\Shared\Adapters.Shared.Tests.projitems')" /> | ||
| <Import Project="..\CommonTestShared\CommonTestShared.projitems" Label="Shared" Condition="Exists('..\CommonTestShared\CommonTestShared.projitems')" /> | ||
| <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), 'Common.targets'))\Common.targets" /> | ||
|
|
||
| </Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this update required for the PR? Can the older version reference be kept for better compatibility?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see... older version doesn't have netstandard target.
Can you please enable optional reference here - older version for
net45and newer fronetstandard.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now only using ver. 2.0.8 for netstandard, but I have to force log4net from 2.0.5 to 2.0.6 on net45 to support flush.