Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Insights Not showing Trace.TraceInformation messages

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 
like image 736
Nathan Tregillus Avatar asked Jun 05 '18 22:06

Nathan Tregillus


1 Answers

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();
        }
    }
}
like image 127
ZakiMa Avatar answered Sep 28 '22 00:09

ZakiMa