【问题标题】:Application Insights Not showing Trace.TraceInformation messagesApplication Insights 不显示 Trace.TraceInformation 消息
【发布时间】:2018-11-15 12:10:52
【问题描述】:

我有一个 .net core 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 

【问题讨论】:

  • 不确定这是否是问题所在,但您如何配置您的应用洞察力?您是否将值设置为 APPINSIGHTS_INSTRUMENTATIONKEY?

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


【解决方案1】:

添加 TraceListner 包后,它会为 .NET 完整版添加以下部分:

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

【讨论】:

  • 太棒了。我会试试这个
【解决方案2】:

Microsoft.ApplicationInsights.TraceListener 可以在 .NET Core 项目中使用,因为它以 NETSTANDARD1.3 为目标,但需要手动完成配置(如上文所述)。

以下是我在Startup 类中的ConfigureServices 方法。

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

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    • 2016-01-05
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 2020-09-13
    相关资源
    最近更新 更多