gpt4 book ai didi

c# - Application Insights 不显示 Trace.TraceInformation 消息

转载 作者:太空狗 更新时间:2023-10-30 00:37:27 26 4
gpt4 key购买 nike

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

Trace.TraceInformation("Hello World!");

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

我有 Microsoft.ApplicationInsights.AspNetCoreMicrosoft.ApplicationInsights.TraceListener 包。

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

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

最佳答案

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

关于c# - Application Insights 不显示 Trace.TraceInformation 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50709695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com