.NET 4.7.2 WinForms如何实现类似ASP.NET Core WebAPI的默认遥测追踪?
.NET 4.7.2 WinForms 配置Application Insights自动遥测追踪
1. 安装正确的NuGet包
Microsoft.ApplicationInsights.WindowsServer是为传统ASP.NET设计的,WinForms项目需要安装适配桌面应用的包组合:
Microsoft.ApplicationInsights:核心遥测SDKMicrosoft.ApplicationInsights.WindowsServer.TelemetryChannel:负责遥测数据传输Microsoft.ApplicationInsights.DependencyCollector:自动追踪数据库、HTTP调用等依赖项Microsoft.ApplicationInsights.PerfCounterCollector(可选):收集系统性能计数器数据
可通过NuGet包管理器控制台执行以下命令安装:
Install-Package Microsoft.ApplicationInsights Install-Package Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel Install-Package Microsoft.ApplicationInsights.DependencyCollector Install-Package Microsoft.ApplicationInsights.PerfCounterCollector
2. 手动创建并配置applicationInsights.config
WinForms项目不会自动生成该配置文件,需手动添加:
- 右键项目 → 添加 → 新建项 → 选择「应用程序配置文件」,命名为
applicationInsights.config - 替换以下代码中的
InstrumentationKey为你的Application Insights仪表板密钥:
<?xml version="1.0" encoding="utf-8"?> <ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings"> <InstrumentationKey>你的仪表板密钥</InstrumentationKey> <TelemetryInitializers> <Add Type="Microsoft.ApplicationInsights.WindowsServer.AzureRoleEnvironmentTelemetryInitializer, Microsoft.ApplicationInsights.WindowsServer"/> <Add Type="Microsoft.ApplicationInsights.WindowsServer.DomainNameRoleInstanceTelemetryInitializer, Microsoft.ApplicationInsights.WindowsServer"/> <Add Type="Microsoft.ApplicationInsights.WindowsServer.BuildInfoConfigComponentVersionTelemetryInitializer, Microsoft.ApplicationInsights.WindowsServer"/> </TelemetryInitializers> <TelemetryModules> <Add Type="Microsoft.ApplicationInsights.WindowsServer.DeveloperModeWithDebuggerAttachedTelemetryModule, Microsoft.ApplicationInsights.WindowsServer"/> <Add Type="Microsoft.ApplicationInsights.WindowsServer.UnhandledExceptionTelemetryModule, Microsoft.ApplicationInsights.WindowsServer"/> <Add Type="Microsoft.ApplicationInsights.WindowsServer.UnobservedExceptionTelemetryModule, Microsoft.ApplicationInsights.WindowsServer"/> <!-- 依赖项追踪配置 --> <Add Type="Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule, Microsoft.ApplicationInsights.DependencyCollector"> <ExcludeComponentCorrelationHttpHeadersOnDomains> <Add>core.windows.net</Add> <Add>localhost</Add> <Add>127.0.0.1</Add> </ExcludeComponentCorrelationHttpHeadersOnDomains> </Add> <!-- 性能计数器配置 --> <Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.PerformanceCollectorModule, Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector"> <Counters> <Add PerformanceCounter="\Process(??APP_WIN32_PROC??)\Handle Count" ReportAs="Process handle count"/> <Add PerformanceCounter="\Memory\Available Bytes" ReportAs="Available memory"/> </Counters> </Add> </TelemetryModules> <TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel"/> </ApplicationInsights>
3. 启动时初始化遥测
打开Program.cs,在Application.Run前添加遥测初始化逻辑,确保全局生效:
using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.Extensibility; namespace YourWinFormsApp { static class Program { [STAThread] static void Main() { // 从配置文件加载遥测配置 TelemetryConfiguration.Active = TelemetryConfiguration.CreateFromConfiguration(); // 初始化全局遥测客户端 var telemetryClient = new TelemetryClient(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); // 应用关闭时强制刷新未发送的遥测数据 telemetryClient.Flush(); // 预留传输时间,避免进程提前结束导致数据丢失 System.Threading.Thread.Sleep(1000); } } }
4. 验证追踪效果
- 运行应用后执行依赖操作(如数据库查询、HTTP请求),或触发未处理异常
- 等待3-5分钟后查看Application Insights仪表板,在「依赖项」「异常」「性能计数器」面板中可看到自动收集的数据
- 若数据未出现,排查方向:
- 确认仪表板密钥配置正确
- 检查网络是否允许应用访问Application Insights服务(无防火墙/代理拦截)
- 调试模式下,
DeveloperModeWithDebuggerAttachedTelemetryModule会实时发送数据,可立即查看
补充说明
WinForms没有ASP.NET Core的请求管道,因此不会自动生成「请求」遥测,但可以手动将窗体操作、按钮点击等业务逻辑标记为请求;依赖项、异常、性能数据会自动追踪。
内容的提问来源于stack exchange,提问作者mof
相关产品推荐
相关产品推荐

