Nat*_*lus 4 c# trace system.diagnostics azure-application-insights .net-core
我有一个.net核心Web API项目。我只是想让我的以下跟踪语句出现在应用程序见解中:
Trace.TraceInformation("Hello World!");
Run Code Online (Sandbox Code Playgroud)
在调试时,我在输出窗口中看到日志,但是在部署后,在日志中没有看到任何跟踪语句。...为什么?
我有和Microsoft.ApplicationInsights.AspNetCore,Microsoft.ApplicationInsights.TraceListener包括包装。
我知道App洞察力已经设置好了,因为正在出现请求,并且我从未收集的性能指标中得到了一条跟踪消息(请参阅下面的跟踪消息):
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
Run Code Online (Sandbox Code Playgroud)
添加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>
Run Code Online (Sandbox Code Playgroud)
由于.NET Core没有自动注册,因此看起来就像注册ApplicationInsightsTraceListener一样:
Trace.Listeners.Add(new ApplicationInsightsTraceListener());
Run Code Online (Sandbox Code Playgroud)
这是完整的控制台应用程序(也应适用于其他类型),它捕获了所有三个跟踪(通过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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1472 次 |
| 最近记录: |