.NET Core 3.0 will be released at this month's .NET Conf conference, many of my friends, including me, couldn't wait to use the preview version to migrate our apps. Fortunately, starting with Preview 7, the API surface is final and can be treated as an RC version. There should be no API difference between preview 7, 8, 9 and RTM. Today I will talk about migration tips for Azure Application Insights.
Background
My old application uses ASP.NET Core 2.2 and monitoring with the Azure Application Insights SDK 2.7, which is currently used by most users in the release channel. App migration to .NET Core 3.0 is beyond the scope of this article, you may check the official document for a complete guide. Application Insights also needs an upgrade to work with an ASP.NET Core 3.0 App. The old version neither compiles nor works on .NET Core 3.0.
Use the latest SDK
The latest Application Insights SDK version is 2.8.0-beta2, we must use this version.
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.8.0-beta2" />
If you are using beta 1 or older versions, your code will throw an exception at runtime: https://github.com/microsoft/ApplicationInsights-aspnetcore/issues/957
This has been fixed in beta 2 https://github.com/microsoft/ApplicationInsights-aspnetcore/pull/959
Tooling support is currently no-go
Visual Studio 2019 release channel doesn't support tooling for Application Insights for .NET Core 3.0 App. You can still see and use the Add Application Insights context menu on your web project, but the outcome is messing up your code, it won't compile or work. For tooling support, we need to use VS2019 preview channel or wait until .NET Core 3.0 release.
Do it yourself
Without tooling, we have to migrate Application Insights on our own.
First, you will find the UseApplicationInsights()
method been obsoleted from Program.cs. .NET Core 3.0 does use IHostBuilder
to replace IWebHostBuilder
in Program.cs by the way, but we don't need to keep the obsoleted API, just remove UseApplicationInsights()
from your code, and use the new API:
In Startup.cs, add this into ConfigureServices()
services.AddApplicationInsightsTelemetry();
This method has an optional parameter which is InstrumentationKey
. But you don't have to set it here, by default it will depend on the environment variable or configuration file:
"ApplicationInsights": {
"InstrumentationKey": "<Your Key>"
}
My advice is not to manually pass in parameters and rely on the environment variables so that you can associate Application Insights directly in Azure App Services.
If your App has razor views, nothing changes for this migration, they stay the same.
@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet
...
@Html.Raw(JavaScriptSnippet.FullScript)
But if you are using TelemetryConfiguration.Active, you'll find it is also has been obsoleted. For example, I used to turn off Application Insights on non-production environments:
TelemetryConfiguration.Active.DisableTelemetry = true;
Which was learned from Azure Docs. However, this document is not updated for .NET Core, this method is only working for .NET Framework Apps. The "Active" is no longer working for .NET Core, which is explained in another official document
Now, we have to use DI to get the instance of TelemetryConfiguration
public void Configure(IApplicationBuilder app, TelemetryConfiguration configuration)
{
configuration.DisableTelemetry = true;
}
A little cleanup
I recently found that some code that Visual Studio generates when adding Application Insights service can be removed without affecting data telemetry in Application Insights.
Delete these from your .csproj file:
<ApplicationInsightsResourceId>/subscriptions/****/resourcegroups/****/providers/microsoft.insights/components/****</ApplicationInsightsResourceId>
<ApplicationInsightsAnnotationResourceId>/subscriptions/****/resourcegroups/****/providers/microsoft.insights/components/****</ApplicationInsightsAnnotationResourceId>
And delete ConnectedService.json and it's Connected Services folder
\Connected Services\Application Insights\ConnectedService.json
Application Insights will continue to work, the only cost is you now lost the ability to view Application Insights data from Code Lens within Visual Studio. If it's not important to you, feel free to remove these codes.
评论