Code Monkey home page Code Monkey logo

applicationinsights-dotnet-logging's Introduction

This repo is in the process of being migrated

Please pardon our progress... We are currently in the process of consolidating our github repos. See microsoft/ApplicationInsights-dotnet#1214

Build codecov.io

Nuget packages

Application Insights logging adapters.

If you use NLog, log4Net or System.Diagnostics.Trace for diagnostic tracing in your application, you can have your logs sent to Application Insights, where you can explore and search them. Your logs will be merged with the other telemetry coming from your application, so that you can identify the traces associated with servicing each user request, and correlate them with other events and exception reports.

Read more:

ILogger

See this.

NLog

Application Insights NLog Target nuget package adds ApplicationInsights target in your web.config.

If your application does not have web.config then it can also be configured manually.

  • Configure ApplicationInsightsTarget using NLog.config :
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <extensions>
		<add assembly="Microsoft.ApplicationInsights.NLogTarget" />
    </extensions>
	<targets>
		<target xsi:type="ApplicationInsightsTarget" name="aiTarget">
			<instrumentationKey>Your_Resource_Key</instrumentationKey>	<!-- Only required if not using ApplicationInsights.config -->
			<contextproperty name="threadid" layout="${threadid}" />	<!-- Can be repeated with more context -->
		</target>
	</targets>
	<rules>
		<logger name="*" minlevel="Trace" writeTo="aiTarget" />
	</rules>
</nlog>
// You need this only if you did not define InstrumentationKey in ApplicationInsights.config (Or in the NLog.config)
TelemetryConfiguration.Active.InstrumentationKey = "Your_Resource_Key";

Logger logger = LogManager.GetLogger("Example");

logger.Trace("trace log message");
  • Configure ApplicationInsightsTarget using NLog Config API : If you configure NLog programmatically with the NLog Config API, then create Application Insights target in code and add it to your other targets:
var config = new LoggingConfiguration();

ApplicationInsightsTarget target = new ApplicationInsightsTarget();
// You need this only if you did not define InstrumentationKey in ApplicationInsights.config or want to use different instrumentation key
target.InstrumentationKey = "Your_Resource_Key";

LoggingRule rule = new LoggingRule("*", LogLevel.Trace, target);
config.LoggingRules.Add(rule);

LogManager.Configuration = config;

Logger logger = LogManager.GetLogger("Example");

logger.Trace("trace log message");

Log4Net

Application Insights Log4Net adapter nuget modifies web.config and adds Application Insights Appender.

For more information, see Log4Net Configuration

// You do not need this if you have instrumentation key in the ApplicationInsights.config
TelemetryConfiguration.Active.InstrumentationKey = "Your_Resource_Key";

log4net.Config.XmlConfigurator.Configure();
var logger = LogManager.GetLogger(this.GetType());

logger.Info("Message");
logger.Warn("A warning message");
logger.Error("An error message");

System.Diagnostics

Microsoft.ApplicationInsights.TraceListener nuget package modifies web.config and adds application insights listener.

For more information, see "Microsoft Docs: "Tracing and Instrumenting Applications"

<configuration>
  <system.diagnostics>
    <trace>
      <listeners>
        <add name="myAppInsightsListener" type="Microsoft.ApplicationInsights.TraceListener.ApplicationInsightsTraceListener, Microsoft.ApplicationInsights.TraceListener" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

If your application type does not have web.config, add listener programmatically or in the configuration file appropriate to your application type

// You do not need this if you have instrumentation key in the ApplicationInsights.config
TelemetryConfiguration.Active.InstrumentationKey = "Your_Resource_Key";
System.Diagnostics.Trace.TraceWarning("Slow response - database01");

EventSource

EventSourceTelemetryModule allows you to configure EventSource events to be sent to Application Insights as traces.

For more information, see Microsoft Docs: "Using EventSource Events".

ETW

EtwCollectorTelemetryModule allows you to configure events from ETW providers to be sent to Application Insights as traces.

For more information, see Microsoft Docs: "Using ETW Events".

DiagnosticSource

You can configure System.Diagnostics.DiagnosticSource events to be sent to Application Insights as traces.

For more information, see CoreFX: "Diagnostic Source Users Guide".

To enable, edit the TelemetryModules section of the ApplicationInsights.config file:

<Add Type="Microsoft.ApplicationInsights.DiagnosticSourceListener.DiagnosticSourceTelemetryModule, Microsoft.ApplicationInsights.DiagnosticSourceListener">
      <Sources>
        <Add Name="MyDiagnosticSourceName" />
      </Sources>
 </Add>

applicationinsights-dotnet-logging's People

Contributors

abaranch avatar alexbulankou avatar cijothomas avatar clguiman avatar dmitry-matveev avatar dnduffy avatar ibebbs avatar jafletch avatar karolz-ms avatar kartang avatar msftgits avatar nikitasavinov avatar pharring avatar ramjotsingh avatar regexrowboat avatar rggammon avatar sergeykanzhelev avatar snakefoot avatar timothymothra avatar xiaomi7732 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

applicationinsights-dotnet-logging's Issues

XML Configuration

Hi Guys, thanks for maintaining this. It doesn't look like you can utilize xml configuration for this external target. Is that correct? I'm just looking to confirm that.

Thanks

Not compatible with log4net version 2.0.6.0 +

Starting with version 2.0.6.0 of the log4net nuget package, the log4net.dll assembly now has an assembly version that matches the nuget package version (both 2.0.x.0).

Previously, log4net.dll had an assembly version of 1.2.15.0.
Log4NetAppender references log4.net.dll, Version=1.2.15.0...

(See: https://github.com/Microsoft/ApplicationInsights-dotnet-logging/blob/7634c868bde12c1bddc881079cef86358a876ce7/src/Adapters/Log4NetAppender.Net45/Log4NetAppender.Net45.csproj#L24-L28)

Therefore, the Log4NetAppender will not work with log4net package 2.0.6 or greater.
Use log4net 2.0.5 as a temporary workaround.

Add 'CloudRoleName' property in 'ApplicationInsightsTarget.cs' to enable log filtering for multiple components

Need to add cloud_RoleName property to ApplicationInsightsTarget class. This property should enable filtering log by multiple components.
It might be really small but very important issue in term of using same resource of Application insight for multiple components.

An overview of this solution is given:

 /// <summary>
 /// Gets or sets cloud_roleName
 /// </summary>
 public string Cloud_RoleName { get; set; }

 /// <summary>
 /// Gets or sets cloud_roleName
 /// </summary>
 public string Cloud_RoleInstance { get; set; } 
...........................

 protected override void InitializeTarget()
        {
            base.InitializeTarget();
            this.telemetryClient = new TelemetryClient();
          
            if (!string.IsNullOrEmpty(this.InstrumentationKey))
            {
                this.telemetryClient.Context.InstrumentationKey = this.InstrumentationKey;
            }

            if (!string.IsNullOrEmpty(this.Cloud_RoleName))
            {
                this.telemetryClient.Context.Cloud.RoleName = this.Cloud_RoleName;
            }

            if (!string.IsNullOrEmpty(this.Cloud_RoleInstance))
            {
                this.telemetryClient.Context.Cloud.RoleInstance = this.Cloud_RoleInstance;
            }

            this.telemetryClient.Context.GetInternalContext().SdkVersion = SdkVersionUtils.GetSdkVersion("nlog:");
        }
 
         ...........

Question: Application Insights - Write to "exceptions" and not "traces"

What do I need to do to write the unhandled exceptions to "exceptions" tab in Application Insights instead of the usual "Traces" tab?

This is what my setup is:

            var config = new LoggingConfiguration();
            var target = new ApplicationInsightsTarget
            {
                InstrumentationKey = _instrumentationKey
            };

            var rule = new LoggingRule("*", LogLevel.Trace, target);
            config.LoggingRules.Add(rule);

            LogManager.Configuration = config;

And this is how I'm logging:

private readonly ILogger<ClassName> _logger;
public ClassName(ILoggerFactory loggerFactory) 
{
_logger = loggerFactory.CreateLogger<CustomErrorHandlingMiddleware>();
}
...
...
_logger.LogError(0, exception, exception.Message);
...

Everything is ending up under "traces" tab, nothing under "exceptions" tab.

image

FileNotFoundException: Could not load file or assembly 'System.Diagnostics.Process, Version=4.0.0.0'

Hi,

I'm using Microsoft.ApplicationInsights.EventSourceListener v2.4.0 to write ETW events to ApplicationInsights from a '.NET Framework 4.6' console application.

I have just started receiving the following error when an event is written (via EventSource.WriteEvent):

System.Diagnostics.Tracing.EventSourceException occurred
  HResult=0x80131500
  Message=An error occurred when writing to a listener.
  Source=mscorlib
  StackTrace:
   at System.Diagnostics.Tracing.EventSource.DispatchToAllListeners(Int32 eventId, Guid* childActivityID, EventWrittenEventArgs eventCallbackArgs)
   at System.Diagnostics.Tracing.EventSource.WriteToAllListeners(Int32 eventId, Guid* childActivityID, Object[] args)
   at System.Diagnostics.Tracing.EventSource.WriteToAllListeners(Int32 eventId, Guid* childActivityID, Int32 eventDataCount, EventData* data)
   at System.Diagnostics.Tracing.EventSource.WriteEventWithRelatedActivityIdCore(Int32 eventId, Guid* relatedActivityId, Int32 eventDataCount, EventData* data)
   at System.Diagnostics.Tracing.EventSource.WriteEvent(Int32 eventId, String arg1)

Inner Exception 1:
FileNotFoundException: Could not load file or assembly 'System.Diagnostics.Process, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

I isolated this to Microsoft.ApplicationInsights.EventSourceListener via Fusion assembly logging where I found this:

*** Assembly Binder Log Entry  (25/08/2017 @ 12:34:11) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  [REMOVED]
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Diagnostics.Process, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = [REMOVED]
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = [REMOVED]
Calling assembly : Microsoft.ApplicationInsights.EventSourceListener, Version=2.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: [REMOVED]
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Diagnostics.Process, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL [REMOVED]
LOG: Attempting download of new URL [REMOVED]
LOG: Attempting download of new URL [REMOVED]
LOG: Attempting download of new URL [REMOVED]
LOG: All probing URLs attempted and failed.

*** Assembly Binder Log Entry  (25/08/2017 @ 12:34:11) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  [REMOVED]
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Diagnostics.Process, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = [REMOVED]
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = [REMOVED]
Calling assembly : Microsoft.ApplicationInsights.EventSourceListener, Version=2.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: [REMOVED]
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Diagnostics.Process, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL [REMOVED]
LOG: Attempting download of new URL [REMOVED]
LOG: Attempting download of new URL [REMOVED]
LOG: Attempting download of new URL [REMOVED]
LOG: All probing URLs attempted and failed.

*** Assembly Binder Log Entry  (25/08/2017 @ 12:34:11) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  [REMOVED]
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Diagnostics.Process, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = [REMOVED]
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = [REMOVED]
Calling assembly : Microsoft.ApplicationInsights.EventSourceListener, Version=2.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: [REMOVED]
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Diagnostics.Process, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: The same bind was seen before, and was failed with hr = 0x80070002.
ERR: Unrecoverable error occurred during pre-download check (hr = 0x80070002).

I am unable to locate the System.Diagnostics.Process assembly anywhere nor am I able to find a nuget package that may be applicable.

This may be related to a recent update to 'Visual Studio 2017 version 15.3' which I upgraded to yesterday (not having seen this issue previously). However, I am unable to confirm this.

Any help or insight would be greatly appreciated.

Regards,
Ian

DiagnosticSourceListener fails to load

The version of Microsoft.ApplicationInsights.DiagnosticSourceListener on nuget.org (2.4.0-beta2) references Microsoft.ApplicationInsights 2.4
However, the .nuspec depends on 2.3
So, we have a mismatch which causes a System.IO.FileLoadException, resulting in the telemetry module being rejected:

AI: ApplicationInsights configuration file loading failed. Type 'Microsoft.ApplicationInsights.DiagnosticSourceListener.DiagnosticSourceTelemetryModule, Microsoft.ApplicationInsights.DiagnosticSourceListener' was not found. Type loading was skipped. Monitoring will continue.

Logary

Hello,

AppInsights target for Logary would be nice.

Occasional "item with the same key has been added" exceptions in Microsoft.ApplicationInsights.NLogTarget.ApplicationInsightsTarget.BuildPropertyBag

I have noted the following exception to occur from within some web jobs (and therefore causing them to die) when a particular logging path takes place. I'm still trying to figure out exactly what is causing this path, but I believe that the BuildPropertyBag shouldn't cause an unhandled exception that crashes my web job.
Note that this occurs sometimes in the midst of otherwise normal logging (including Exceptions, Traces, etc).

Thank you!

[05/02/2016 21:14:30 > 2d9624: ERR ] Unhandled Exception: System.ArgumentException: An item with the same key has already been added. [05/02/2016 21:14:30 > 2d9624: ERR ] at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) [05/02/2016 21:14:30 > 2d9624: ERR ] at System.Collections.Generic.Dictionary``2.Insert(TKey key, TValue value, Boolean add) [05/02/2016 21:14:30 > 2d9624: ERR ] at System.Collections.Generic.Dictionary``2.Add(TKey key, TValue value) [05/02/2016 21:14:30 > 2d9624: ERR ] at Microsoft.ApplicationInsights.NLogTarget.ApplicationInsightsTarget.BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace) [05/02/2016 21:14:30 > 2d9624: ERR ] at Microsoft.ApplicationInsights.NLogTarget.ApplicationInsightsTarget.SendException(LogEventInfo logEvent) [05/02/2016 21:14:30 > 2d9624: ERR ] at Microsoft.ApplicationInsights.NLogTarget.ApplicationInsightsTarget.Write(LogEventInfo logEvent) [05/02/2016 21:14:30 > 2d9624: ERR ] at NLog.Targets.Target.Write(AsyncLogEventInfo logEvent) [05/02/2016 21:14:30 > 2d9624: ERR ] at NLog.Targets.Target.Write(AsyncLogEventInfo[] logEvents) [05/02/2016 21:14:30 > 2d9624: ERR ] at NLog.Targets.Target.WriteAsyncLogEvents(AsyncLogEventInfo[] logEvents) [05/02/2016 21:14:30 > 2d9624: ERR ] at NLog.Targets.Wrappers.AsyncTargetWrapper.ProcessPendingEvents(Object state) [05/02/2016 21:14:30 > 2d9624: ERR ] at System.Threading.TimerQueueTimer.CallCallbackInContext(Object state) [05/02/2016 21:14:30 > 2d9624: ERR ] at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) [05/02/2016 21:14:30 > 2d9624: ERR ] at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) [05/02/2016 21:14:30 > 2d9624: ERR ] at System.Threading.TimerQueueTimer.CallCallback() [05/02/2016 21:14:30 > 2d9624: ERR ] at System.Threading.TimerQueueTimer.Fire() [05/02/2016 21:14:30 > 2d9624: ERR ] at System.Threading.TimerQueue.FireQueuedTimerCompletion(Object state) [05/02/2016 21:14:30 > 2d9624: ERR ] at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() [05/02/2016 21:14:30 > 2d9624: ERR ] at System.Threading.ThreadPoolWorkQueue.Dispatch() [05/02/2016 21:14:30 > 2d9624: ERR ] at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

telemetryclient.TrackTrace memory leak

Version: "Microsoft.ApplicationInsights" 2.1.0.
Repro of the issue:

TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();
config.InstrumentationKey = "{KEY}";

TelemetryClient client = new TelemetryClient(config);
var dictionary = new Dictionary<string, string>()
            {
                {"testprop", "testval" },
                {"testprop2", "testval2" },
                {"testprop3", "testval3" },
            };

Parallel.For(0, 10000000, (i) =>
            {
                client.TrackTrace("test " + i, SeverityLevel.Information, dictionary);
            });

This code will throw an out of memory exception when running in a 32-bit process, and eventually consume up to 21GB+ of RAM when running in a 64-bit process.

Here's a snapshot of the memory usage when profiling:
image

Add a DiagnosticSourceListener module

For Snapshot Collector, I created a general purpose TelemetryModule that listens to DiagnosticSource and tracks them via Application Insights. It's the DiagnosticSource equivalent of Microsoft.ApplicationInsights.EventSourceListener

Are you interested in including it here? I'll do the work and submit a PR, I just want to know if you're interested before I embark on porting it.

Remove duplicate and obsolete properties

Each adaptor adds several custom properties. We need to revisit what is collected. For example Timestamp is collected as a regular property and should not be added as custom. Or logging adapter name is not very useful knowing that customer will use only one adapter in his application.

Is there any plan to support .Net Core 2.0?

Currently, we are using NLog for .Net Core and Azure Application Insights for .Net core. And we would like to use NLog write log to Azure Application Insights. But Microsoft.ApplicationInsights.NLogTarget doesn’t support .Net Core 2.0 yet, do you have any plan for support .Net Core 2.0

Nlog: set user/session id possible?

I have nlog logging to appinsights. it is configured in the app.config without any keys.

in the program startup I call:

    const string ik = "123123123...";
    TelemetryConfiguration.Active.InstrumentationKey = ik;
    var tc = new TelemetryClient
    {
        InstrumentationKey = ik
    };

    tc.Context.User.Id = settings.InstallationId.ToString();
    tc.Context.Session.Id = Guid.NewGuid().ToString();

I use the TelemetryClient to log pageviews. I see pageviews and traces from nlog in my appinsights. What I want now is to provide the user_id and the session_id in each trace but I do not know how to set this up? the pageviews have a user/session id already.

thanks

Add custom property to Log4Net adapter

Hi, I'm trying to add a custom property to my log4net adapter so I can use it as a filter parameter in Application Insights.

I've tried -
In the config:

<parameter>
   <parameterName value="@CustomColumn"/>
   <dbType value="String" />
   <size value="255" />
   <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{CustomColumn}" />
  </layout>
</parameter>

and then :
log4net.LogicalThreadContext.Properties["CustomColumn"] = "Custom value";

But I don't see the custom property in Application Insights dashboard.

Thanks.

NLog ApplicationInsightsTarget Flush should wait for completion

DefaultFlushDelay is 30 secs, and when flushed manually, then it happens in async-style, so one has to manually wait as specified in the documentation:

https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics#flushing-data

Since it is not currently possible to know when a Flush-operation actually completes, then one just have to make a quick guess of how long to wait, but also not too long.

See also https://stackoverflow.com/questions/49514679/no-log-entries-in-azure-application-insights-using-microsoft-applicationinsights

EventSourceListener NuGet package overrides user settings in AI.config file

Repro steps (not completely verified, just something I noticed when doing app building)

  1. Create a .NET console app, add Microsoft.ApplicationInsights.EventSourceListener package, version < latest
  2. Modify the list of captured EventSources in ApplicationInsights.config
  3. Upgrade EventSourceListener to latest version (currently 2.4.0-beta1)

Expected: the list of captured EventSources is not affected
Actual: the list of captured EventSources is reset to the default (1 artificial EventSource)

This might also be an issue for the EtwCollector package (haven't verified that)

DeadLock over Microsoft.ApplicationInsights.EventSourceListener.EventSourceTelemetryModule.OnEventSourceCreated() call

We run into a deadlock issue over Microsoft.ApplicationInsights.EventSourceListener.EventSourceTelemetryModule.OnEventSourceCreated() function.

0:013> !critlist
CritSec at 000000acea379ae0. Owned by thread 13.
Waiting Threads: 10 12 15 16 27 28
so, the multiple threads hang on thread# 13.

0:013> !locks
CritSec +ea379ae0 at 000000acea379ae0
WaiterWoken No
LockCount 6
RecursionCount 1
OwningThread 868
EntryCount 0
ContentionCount 6

0:013> ~~[0x868]
. 13 Id: 1700.868 Suspend: 0 Teb: 00007ff5ff014000 Unfrozen Start: clr!Thread::intermediateThreadProc (00007ff86b57598c)
Priority: 0 Priority class: 32 Affinity: 3

Thread # 13 and Thread# 17 are deadlock on each other

Thread 17 takes a lock on the TelmetryModule
Thread 17 then takes a lock on EventListenersLock

Thread 13 takes a lock on EventListenersLock
Thread 13 then takes a lock on the TelmetryModule

Thread 13
000000ac829ace78 00007ff8749e0c6a [HelperMethodFrame_1OBJ: 000000ac829ace78] System.Threading.Monitor.Enter(System.Object) << Locks on Microsoft.ApplicationInsights.EventSourceListener.EventSourceTelemetryModule
000000ac829acf70 00007ff80c9788fd Microsoft.ApplicationInsights.EventSourceListener.EventSourceTelemetryModule.OnEventSourceCreated(System.Diagnostics.Tracing.EventSource)
000000ac829acfc0 00007ff86a12bc27 System.Diagnostics.Tracing.EventSource.AddListener(System.Diagnostics.Tracing.EventListener)
000000ac829ad030 00007ff86a1d40ca System.Diagnostics.Tracing.EventListener.AddEventSource(System.Diagnostics.Tracing.EventSource)
000000ac829ad0a0 00007ff86a1d341d System.Diagnostics.Tracing.EventSource.Initialize(System.Guid, System.String, System.String[]) << Locks on EventListener.EventListenersLock
000000ac829ad130 00007ff86a13260f System.Diagnostics.Tracing.EventSource..ctor(System.Diagnostics.Tracing.EventSourceSettings, System.String[])

Thread 17
000000ac8a20e068 00007ff8749e0c6a [HelperMethodFrame_1OBJ: 000000ac8a20e068] System.Threading.Monitor.Enter(System.Object) << Locks on EventListener.EventListenersLock
000000ac8a20e160 00007ff86a9f440c System.Diagnostics.Tracing.EventSource.SendCommand(System.Diagnostics.Tracing.EventListener, Int32, Int32, System.Diagnostics.Tracing.EventCommand, Boolean, System.Diagnostics.Tracing.EventLevel, System.Diagnostics.Tracing.EventKeywords, System.Collections.Generic.IDictionary2) 000000ac8a20e1e0 00007ff86a1a72d7 System.Diagnostics.Tracing.EventListener.EnableEvents(System.Diagnostics.Tracing.EventSource, System.Diagnostics.Tracing.EventLevel, System.Diagnostics.Tracing.EventKeywords, System.Collections.Generic.IDictionary2)
000000ac8a20e240 00007ff80c98ac07 Microsoft.ApplicationInsights.EventSourceListener.EventSourceTelemetryModule.EnableAsNecessary(System.Diagnostics.Tracing.EventSource)
000000ac8a20e2c0 00007ff80c98a853 Microsoft.ApplicationInsights.EventSourceListener.EventSourceTelemetryModule.Initialize(Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration) << Locks on Microsoft.ApplicationInsights.EventSourceListener.EventSourceTelemetryModule

Reference:
https://github.com/Microsoft/ApplicationInsights-dotnet-logging/blob/63a6886743dc025fe523324921c3667525d6ee95/src/Adapters/EventSource.Netstandard13/EventSourceTelemetryModule.cs#L122-L148

Use Authenticated User Id in Log4NetAppender only in Debug mode or not at all

We should remove automated Authenticated User ID collection from Log4NetAppender code here.

Having Authenticated User ID collected by default may present a privacy concern. However, it is acceptable to collect this for debug sessions to easily differentiate debug sessions between several users reporting to the same IKey. Acceptable does not mean necessary, so simple removal of User ID collection will do as well.

TraceListener adds EventId property that is always 0

When 'public override void Write(string message)' override is used EventId property is added with value 0. We should not add this property for this override. For other overrides 0 is a legitimate value. (Consider making EventId nullable and ckeck for null)

Custom telemetry initializer is not used

I have a custom telemetry initializer which adds the current environment to the AI logs.

public class EnvironmentTelementryInitializer : WcfTelemetryInitializer
    {
        public const string PropertyName = "Env";

        private readonly string _environment;

        public EnvironmentTelementryInitializer()
        {
            _environment = ConfigurationManager.AppSettings["EnvironmentName"];
        }

        protected override void OnInitialize(ITelemetry telemetry, IOperationContext operation)
        {
            telemetry.Context.Properties[PropertyName] = _environment;
        }
    }

Even though the initializer is added to the collection of initializers in ApplicationInsights.Config the AI logs does not contain the "Env" property.

I'm also using the RequestTrackingTelemetryModule and there the Env property is set so it seems that the NLogTarget does not use the custom intializers specified in ApplicationInsight.config

Inconsistent logging of role instance name between WAD2AI and .NET SDK

Performance counters reported by WAD2AI integration use the logical name of the Web Role (e.g. WebRole1_IN_3) while performance counters reported by the regular .NET SDK use the VM name (RD0003FF93FDC0).

Repro:
I have the latest AI SDK and latest Azure Cloud Tools. I have created a new Web Role and enabled WAD2AI through the UX in VS. Run some requests...
See attached.

inconsistentroleinstancename

Log time conversions - Display raw value/UTC time

Note: Not sure if this needs to be posted here, or sent to the parent portal group.
Currently the times of logs being sent to App Insights are logged via UTC at least in our particular use case. we have two different logging locations currently, one in SQL and one here in Application Insights.

When running a search in application insights the logging values are populated in whatever time zone is currently set to the computer. While this is helpful for regional debugging in quick succession, in trying to tie events together that are listed in UTC, there is often significant time spent on trying to correlate time stamps.

It would be nice if the logging item displayed the saved time in UTC or some other standard item, so when multiple people, in geographically dispersed locations can focus quickly on identifying items.

An enhancement that could be added to the logging portion to help create a shortcut, is an added field that showed the default UTC time of a logging event. This would help get around the portal time conversions, and the same value could be referenced twice, while still providing a true accounting of the time the action was preformed.

Is there a way to inject my own TelemetryClient?

I'm using a TelemetryClient in my WPF app to send metrics to App Insights, and I'm using the NLog target for sending logs to App Insights. I initialize both with the same API key, which works great.

I would like to pass my existing Telemetryclient in to the ApplicationInsightsTarget constructor so that they can share the same client.

I want to have both clients share the same Context, SessionID and User.

var telemetryClient = new TelemetryClient { InstrumentationKey = instrumentationKey };
telemetryClient.Context.Session.Id = Guid.NewGuid().ToString();
ApplicationInsightsTarget target = new ApplicationInsightsTarget { InstrumentationKey = instrumentationKey };

Is there a way to inject my own TelemtryClient? Of not, is there a way to access the Context or otherwise set context of the TelemetryClient used by the ApplicationInsightsTarget?

Thanks!

Marc

Track Events and Track Metrics

Hi Guys,

Thank you very much for this project. Is it possible in the current version to TrackEvents and TrackMetrics?
An example would be highly appreciated.

Web.config transformation is not applied correctly when using configSource

When you install the TraceListener package, it adds the trace listener to the system.diagnostics section in the web.config file. However, we're using the configSource attribute to configure trace listeners and sources in a separate file. When I install the package, it results in the following:

<system.diagnostics configSource="trace.config">
  <trace autoflush="true" indentsize="0">
    <listeners>
      <add name="myAppInsightsListener" type="Microsoft.ApplicationInsights.TraceListener.ApplicationInsightsTraceListener, Microsoft.ApplicationInsights.TraceListener" />
    </listeners>
  </trace>
</system.diagnostics>

I am not very familiar with the internals of the configuration, but I believe the configSource overrides web.config in this case. If there is a configSource present, I think it should modify the configSource file, or at the very least not modify the web.config incorrectly.

Microsoft.ApplicationInsights.NLogTarget not supported in .net core

Cross posted here: microsoft/ApplicationInsights-aspnetcore#356

Not sure if this is the right repo to inquire about this - please point me in the right direction if not.

We are using NLog in our aspnet.core project. We tried to add Microsoft.ApplicationInsights.NLogTarget package and noticed that it's not (yet) supported. Is there a repo/project for this package where I could track anyprogress on this?

adding this
<PackageReference Include="Microsoft.ApplicationInsights.NLogTarget" Version="2.0.0" />

causes this:

Severity Code Description Project File Line Suppression State
Error Package Microsoft.ApplicationInsights.NLogTarget 2.0.0 is not compatible with netcoreapp1.1 (.NETCoreApp,Version=v1.1). Package Microsoft.ApplicationInsights.NLogTarget 2.0.0 supports: net40 (.NETFramework,Version=v4.0) , net45 (.NETFramework,Version=v4.5) 0

Unit test is failing for EventSource Listener

The unit tests of 'ReportsAllProperties' recently starts to fail. I have a handle on the commit range causing the trouble but needs some follow up.
I file this issue to track the fix for the unit test.
PR will be provided when issues fixed.

Compatibility of Microsoft.ApplicationInsights.TraceListener Version 2.4.0 with Microsoft.ApplicationInsights Version 2.6.4

According to the nuget page Microsoft.ApplicationInsights.TraceListener Version 2.4.0 is compatible with Microsoft.ApplicationInsights (>= 2.4.0).

Therefore when I updated Microsoft.ApplicationInsights 2.4.0 to the latest version in Visual Studio 2017, my Microsoft.ApplicationInsights.TraceListener version 2.4.0 remained without an update.

When I ran the application I found out that Microsoft.ApplicationInsights.TraceListener Version 2.4.0 does not work with Microsoft.ApplicationInsights 2.6.4 anymore.

I then updated Microsoft.ApplicationInsights.TraceListener to version 2.6.4.

While I see that I should have matched the latest version for Microsoft.ApplicationInsights.TraceListener to version 2.6.4 in the first place, is this also an issues with the website that advertises compatibility when in fact in does not work?

Thank you!

Setting cloud_RoleName

AI now has better support for differentiating different roles (application components, or microservices), e.g. in Application Map.

In web application, I can set it with TelemetryInitializer or directly on the TelemetryClient.Context.Cloud.RoleName. But for the logging adapters, I don't find a way to set it.

Adding support for disabledEventSourceNamePrefix configuration

Due to a .NET Framework bug: https://github.com/dotnet/coreclr/issues/14434, there's a discussion providing support for configurations to allow totally disabling some event sources by their name prefixes.
Details of the discussion could be found here: Azure/diagnostics-eventflow#140.

@SergeyKanzhelev, @dnduffy, I am planning to put the implementation into EventSourceTelemetryModule.

Here's a simple description:
Configuration:
image

Details:

  • Disabling EventSources is not recommended under normal circumstances, however, due to some edge cases, like the bug here: https://github.com/dotnet/coreclr/issues/14434, there needs a way to work-around it.

  • For event-source that matches in the 'Sources' exactly and at the same time, starts with disabledEventSourceNamePrefix, the enabling of the source will take the priority because:

    • Exact match should take over partial match;
    • Generally, for telemetry module settings, we are using the opt-in than opt-out;
    • It is flexible that user can express: I want abc event source but not anything else that starts with ab, because either I don't care or they are having issues.

Please use this issue for feedback on the ideas and I'll submit PR for review when it is implemented.

Allow DiagnosticSourceTelemetryModule to take OnEventWrittenHandler like EventSourceTelemetryModule?

EventSourceTelemetryModule constructor accepts a OnEventWrittenHandler parameter, defaulting to EventDataExtensions.Track (which calls CreateTraceTelementry & client.Track())

Would it make sense for DiagnosticSourceTelemetryModule to also provide a constructor that supported a caller-provided OnEventWrittenHandler? Eg, if I wanted to log events instead of traces. Or, #111 (which introduced this to EventSourceTelemetryModule), discussed needing this for handling nested EventData - similar reasoning could apply to DiagnosticSourceListenerSubscription.OnNext & event.Value

Regression: EventSourceTelemetryModule not setting custom payload properties

Return back Endpoint Validation tests (if needed)

There are three endpoint validation tests TelemetryIsAcceptedByValidateEndpoint that send items directly to AI Endpoint to validate against endpoint-accepted schema. Those tests are now disabled because Ikey is no longer valid.
Let's review their necessity and add them back with the appropriate iKeys.

Track MappedDiagnosticsLogicalContext

If you need to have custom context variables that will flow across async points, you can use NLog to have the MappedDiagnosticsLogicalContext class and the ${mdlc} layout render.

It would be great if this would be sent to AI as well. The same as for properties.

More info
Note. This is for NLog.Contrib but NLog 4.2 now has this as well

Can the log level be configured for Microsoft.ApplicationInsights.TraceListener?

When using the Microsoft.ApplicationInsights.TraceListener NuGet package the following lines are added to the Web.config file:

<system.diagnostics>
    <trace autoflush="true" indentsize="0">
      <listeners>
        <add name="myAppInsightsListener" type="Microsoft.ApplicationInsights.TraceListener.ApplicationInsightsTraceListener, Microsoft.ApplicationInsights.TraceListener" />
      </listeners>
    </trace>
  </system.diagnostics>

By default it looks like it collects all traces (including informational). Is there a configuration change that can be used to change the log level (e.g. warning)?

NLog adapter doesn't support global instrumentation key

I noticed that my AI wasn't receiving log messages from NLog even though I had set the instrumentation key programmatically in my Application_Start block:

TelemetryConfiguration.Active.InstrumentationKey = AppSettings.AppInsightsKey;

If I set a breakpoint inside the InitializeTarget, the global key is set before the target initializes, but the telemetryClient.Context.InstrumentationKey is blank still.

Shouldn't the log adapters fallback on checking the active configuration?

i.e.

base.InitializeTarget();
this.telemetryClient = new TelemetryClient();
if (!string.IsNullOrEmpty(TelemetryConfiguration.Active.InstrumentationKey))
{
    this.telemetryClient.Context.InstrumentationKey = TelemetryConfiguration.Active.InstrumentationKey;
}
if (!string.IsNullOrEmpty(this.InstrumentationKey))
{
    this.telemetryClient.Context.InstrumentationKey = this.InstrumentationKey;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.