Nathan Tregillus
Nathan Tregillus

Reputation: 6354

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 

Upvotes: 5

Views: 5084

Answers (2)

cijothomas
cijothomas

Reputation: 3196

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());
}

Upvotes: 3

ZakiMa
ZakiMa

Reputation: 6281

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();
        }
    }
}

Upvotes: 8

Related Questions