前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >迁移 Azure Application Insights 到 .NET Core 3.0

迁移 Azure Application Insights 到 .NET Core 3.0

作者头像
Edi Wang
发布2019-09-03 17:41:17
9610
发布2019-09-03 17:41:17
举报
文章被收录于专栏:汪宇杰博客汪宇杰博客

导语

.NET Core 3.0 即将在本月的.NET Conf大会上发布正式版,在这之前包括我在内的不少朋友已经迫不及待使用预览版迁移了自己的应用,并爆得体无完肤。好在从Preview 7开始,API已经固定,可以当作正式版的内容去学习。今天我介绍的就是 Azure Application Insights 这块的迁移技巧。

背景

我的老应用程序使用 ASP.NET Core 2.2,配合 Azure Application Insights SDK 2.7 进行云端监控,这也是目前正式版渠道大部分用户的使用情况。应用迁移 .NET Core 3.0 不在本文讨论范围之内,大家可以自行解决。但是 Application Insights 不升级就会编译爆炸,升级了也可能运行爆炸,我们来看看如何不爆。

使用最新版SDK

目前最新版的 SDK 是 2.8.0-beta2,我们必须用这个版本大培 .NET Core 3.0 preview 8

<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.8.0-beta2" />

如果你用的是 beta 1 或者更早的版本,那么你的代码一跑就爆:

https://github.com/microsoft/ApplicationInsights-aspnetcore/issues/957

这个问题已在 beta 2 中修复

https://github.com/microsoft/ApplicationInsights-aspnetcore/pull/959

不要相信工具链

由于目前正式版渠道的 Visual Studio 2019 并不知道如何正确的向 .NET Core 3.0 的项目加入 Application Insights,因此不要用 VS2019 偷懒一键添加,生成的代码会搞乱你的项目,并无法编译。

自己动手,领取福报

首先,你会发现 Program.cs 里的 UseApplicationInsights() 爆了,原因有两个:

  1. ASP.NET Core 3.0 使用 IHostBuilder 替代了 IWebHostBuilder
  2. UseApplicationInsights() 已被标记为 obsolete(大快人心)

关于IHostBuilder的改动,可以看微软官网.NET Core升级文档解决。UseApplicationInsights() 可以直接删除,使用新 API:

在 Startup.cs 的 ConfigureServices() 方法里加入:

services.AddApplicationInsightsTelemetry();

这个方法有个可选参数是 InstrumentationKey,你可以手工传入,也可以不写,不写的话它会依赖环境变量或配置文件里的:

"ApplicationInsights": {

"InstrumentationKey": "你的KEY"

}

我的建议是不要手工传入参数,而依赖配置文件或环境变量,这样可以在 Azure App Services 里直接关联 Application Insights

Razor 页面里的代码可以不用动,和以前一样:

@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet

...

@Html.Raw(JavaScriptSnippet.FullScript)

另外,如果你用了 TelemetryConfiguration.Active,那么还会爆一次。这个 Active 因为大家太喜欢,被obsolete了。

砍刀传送门:https://github.com/microsoft/ApplicationInsights-dotnet/issues/1152

比如原来你的代码希望在非生产环境下关闭遥测,基本上都会这么写,毕竟来源于官网文档:

TelemetryConfiguration.Active.DisableTelemetry = true;

而这个官网文档目前还没更新

https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#disabling-telemetry

而是在另一篇文档里提到了 .NET Core 用 Active 其实一直是无效的:

https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core#configure-the-application-insights-sdk

这东西RTM很久了,坑得一逼也直到现在才给个编译警告……

根据政治正确的万物基于DI定律,现在我们也必须从DI里拿这个TelemetryConfiguration对象。

public void Configure(IApplicationBuilder app, TelemetryConfiguration configuration)

{

configuration.DisableTelemetry = true;

}

清理垃圾

如果你的老应用是用 VS2019 一键梭哈方式全自动添加的 Application Insights,那么会留下这么几个地方可以删除,不会影响遥测数据,满足洁癖控:

.csproj 文件

<ApplicationInsightsResourceId>/subscriptions/****/resourcegroups/****/providers/microsoft.insights/components/****</ApplicationInsightsResourceId> <ApplicationInsightsAnnotationResourceId>/subscriptions/****/resourcegroups/****/providers/microsoft.insights/components/****</ApplicationInsightsAnnotationResourceId>

ConnectedService.json 与 Connected Services 文件夹

\Connected Services\Application Insights\ConnectedService.json

不过要注意,这会让你失去从 VS2019 的 Code Lens 里直接查看 Application Insights 数据的装逼能力。如果你不在意这个逼的装法,那就可以删这些代码。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-09-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 汪宇杰博客 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档