5

I have a .net core web api project. I am simply trying to get my trace statements like below to appear in app insights:

Trace.TraceInformation("Hello World!");

I see the log in my output window when I am debugging, but after I deploy, I am not seeing any of my trace statements in the logs.... Why?

I have the Microsoft.ApplicationInsights.AspNetCore, and Microsoft.ApplicationInsights.TraceListener packages included.

I know App insights is setup, because the requests are appearing, and I am getting one trace message from the performance metrics not getting collected (see trace message below):

AI: Error collecting 3 of the configured performance counters. Please check the configuration.
Counter \ASP.NET Applications(??APP_W3SVC_PROC??)\Requests/Sec: Failed to perform the first read for performance counter. Please make sure it exists. Category: ASP.NET Applications, counter: Requests/Sec, instance MyAPI.exe
Counter \ASP.NET Applications(??APP_W3SVC_PROC??)\Request Execution Time: Failed to perform the first read for performance counter. Please make sure it exists. Category: ASP.NET Applications, counter: Request Execution Time, instance MyAPI.exe
Counter \ASP.NET Applications(??APP_W3SVC_PROC??)\Requests In Application Queue: Failed to perform the first read for performance counter. Please make sure it exists. Category: ASP.NET Applications, counter: Requests In Application Queue, instance 
Nathan Tregillus
  • 6,006
  • 3
  • 52
  • 91
  • Not sure if this is the problem, but how do you configure your app insights? Did you set value to APPINSIGHTS_INSTRUMENTATIONKEY? – Jonatan Dragon Jun 09 '18 at 22:35

2 Answers2

8

Upon adding TraceListner package it adds for .NET Full version the following section:

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

Since there is no auto-registration for .NET Core, it looks like it is a matter of registering ApplicationInsightsTraceListener:

Trace.Listeners.Add(new ApplicationInsightsTraceListener());

Here is the complete console app (should work for other types as well) which captures all three traces (through TraceError, TraceInformation and TrackTrace):

using System;
using System.Diagnostics;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.TraceListener;

namespace CoreConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            TelemetryConfiguration.Active.InstrumentationKey =
                "<your ikey>";

            Console.WriteLine("Hello World!");

            Trace.Listeners.Add(new ApplicationInsightsTraceListener());
            Trace.TraceError("my error");
            Trace.TraceInformation("my information");

            TelemetryClient client = new TelemetryClient();
            client.TrackTrace("Demo application starting up.");

            Console.ReadKey();
        }
    }
}
ZakiMa
  • 5,637
  • 1
  • 24
  • 48
3

Microsoft.ApplicationInsights.TraceListener can be used in .NET Core projects as it targets NETSTANDARD1.3, but configuration needs to be done manually (as indicated in above post).

The following is my ConfigureServices methods in Startup class.

public void ConfigureServices(IServiceCollection services)
{
    services.AddApplicationInsightsTelemetry("ikey"); /* This enables entire application insights auto-collection. If you don't want anything but the traces, then ikey can be set in TelemetryConfiguration.Active.InstrumentationKey as the above console app example. */
    services.AddMvc();
    Trace.Listeners.Add(new ApplicationInsightsTraceListener());
}
Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
cijothomas
  • 2,818
  • 1
  • 13
  • 25