Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

System.Diagnostics.Trace from referenced libs not showing #589

Closed
spelltwister opened this issue Jan 26, 2018 · 7 comments
Closed

System.Diagnostics.Trace from referenced libs not showing #589

spelltwister opened this issue Jan 26, 2018 · 7 comments
Labels

Comments

@spelltwister
Copy link

I have a bunch of services which emit System.Diagnostics.Trace.* trace messages. When hosted via ASP.NET WebAPI, I simply include the Microsoft.ApplicationInsights.TraceListener package and my custom traces from the service libs are collected in app insights.

However, I cannot figure out how to get these same messages when hosting the services via ASP.NET Core 2.0 WebAPI. What I've tried...

Add Application Insights

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddApplicationInsights(app.ApplicationServices, Microsoft.Extensions.Logging.LogLevel.Information);
     //...
    }

Expected Results

Traces from both the framework and my libraries would be in the logs.

Actual Results

After adding application insights using the extension method, I am able to see the traces emitted by the framework (eg, Request Start, Request End, ...), but none of my custom trace messages are present.

Add Trace Listener Package

I decided to add in the Microsoft.ApplicationInsights.TraceListener package just to see if that would help. I updated the configure method to look like this...

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddTraceSource(new System.Diagnostics.SourceSwitch("AppInsightsTraceSwitch")
        {
            Level = System.Diagnostics.SourceLevels.All
        }, new Microsoft.ApplicationInsights.TraceListener.ApplicationInsightsTraceListener());

        loggerFactory.AddApplicationInsights(app.ApplicationServices, Microsoft.Extensions.Logging.LogLevel.Information);
    }

Expected Results

Traces from both the framework and my libraries would be in the logs.

Actual Results

After updating the configuration, I now see the framework trace messages duplicated in the logs, but none of the custom traces are present still.

Version Info

SDK Version : "Microsoft.ApplicationInsights.AspNetCore version 2.1.1", also tried adding "Microsoft.ApplicationInsights.TraceListener version 2.4.1"
.NET Version : netcoreapp2.0
How Application was onboarded with SDK(VisualStudio/StatusMonitor/Azure Extension) : Visual Studio
OS : Windows 10
Hosting Info (IIS/Azure WebApps/ etc) : Azure App Services or locally on IIS have same behavior
Stackoverflow: https://stackoverflow.com/questions/48389908/application-insights-trace-listener-for-asp-net-core-2-0

@cijothomas
Copy link
Contributor

@spelltwister Hi,
Can you share a small repro for us to investigate?

        loggerFactory.AddApplicationInsights(app.ApplicationServices, Microsoft.Extensions.Logging.LogLevel.Information);

The above line is all you need to enable Trace collection.

@spelltwister
Copy link
Author

I created the example repo here: https://github.com/spelltwister/AspNetCoreAppInsightsTrace. All you have to do is replace the value for instrumentation key in the appsettings.json file. There is only one controller with one method. Executing the method should call the service code which emits traces. None of the traces are captured in the local debug app insights data or in the app insights resource in azure.

@cijothomas
Copy link
Contributor

cijothomas commented Mar 21, 2018

@spelltwister Thanks for sharing more details!
The Tracelistener package does not support .net core.
https://github.com/Microsoft/ApplicationInsights-dotnet-logging/blob/develop/src/TraceListener/TraceListener.csproj

loggerFactory.AddApplicationInsights(app.ApplicationServices, Microsoft.Extensions.Logging.LogLevel.Information);

This enables capture of logs logged via ILogger interface of Asp.net Core (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?tabs=aspnetcore2x)

Can you modify your app to use ILogger as that seem to be the recommended way of logging in asp.net core applications?

@spelltwister
Copy link
Author

How could that be the recommended approach. Then my service libraries have to be dependant on asp.net logging. Is there anyway outside of this package to enable collection of traces? In the meantime, we've taken a dependency on app insights directly in our service libraries in order to get tracing back. I thought the point of tracing was to be able to sink it where ever.

@pharring
Copy link
Member

@spelltwister you could open an issue on the ApplicationInsights-dotnet-logging repo to add .NET Standard 2.0 support for TraceListener. (Note that it has to be netstandard2.0 because TraceListener wasn't in previous netstandard versions). See also microsoft/ApplicationInsights-dotnet-logging#115 which could be expanded to cover TraceListener.

@pharring
Copy link
Member

I created a PR to add a .NET Standard flavor of Microsoft.ApplicationInsights.TraceListener. See microsoft/ApplicationInsights-dotnet-logging#166

@cijothomas
Copy link
Contributor

Thanks Pharring for addressing the issue!
This is now released.
https://github.com/Microsoft/ApplicationInsights-dotnet-logging/releases/tag/2.6.0-beta3

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants