Application Insights未显示Trace.TraceInformation消息

时间:2018-06-05 22:16:50

标签: c# .net-core trace azure-application-insights system.diagnostics

我有一个.net核心web api项目。我只是想让我的跟踪语句如下所示出现在应用洞察中:

Trace.TraceInformation("Hello World!");

我在调试时看到输出窗口中的日志,但是在部署之后,我没有在日志中看到任何跟踪语句....为什么?

我已包含Microsoft.ApplicationInsights.AspNetCoreMicrosoft.ApplicationInsights.TraceListener个包。

我知道应用程序见解已设置,因为请求正在出现,并且我从性能指标中收到一条跟踪消息未收集(请参阅下面的跟踪消息):

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 

2 个答案:

答案 0 :(得分:4)

添加TraceListner包后,它将为.NET Full版添加以下部分:

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

由于.NET Core没有自动注册,看起来就像注册ApplicationInsightsTraceListener一样:

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

这是完整的控制台应用程序(也适用于其他类型),它捕获所有三个跟踪(通过TraceError,TraceInformation和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();
        }
    }
}

答案 1 :(得分:2)

Microsoft.ApplicationInsights.TraceListener可以在.NET Core项目中用作SELECT first(value order by time_stamp) as first_value, last(value order by time_stamp) as last_value, CAST(EXTRACT(year FROM time_stamp) AS INTEGER) AS year, CAST(EXTRACT(month FROM time_stamp) AS INTEGER) AS month, CAST(EXTRACT(day FROM time_stamp) AS INTEGER) AS day, CAST(EXTRACT(hour FROM time_stamp) AS INTEGER) AS hour, floor(EXTRACT(minute FROM time_stamp) / 15) AS quarter FROM my_table GROUP BY year, month, day, hour, quarter ORDER BY year, month, day, hour, quarter ,但配置需要手动完成(如上文所示)。

以下是NETSTANDARD1.3课程中的ConfigureServices方法。

Startup