如何在 Visual Studio 中删除 Application InsightsHow to remove Application Insights in Visual Studio

本文介绍如何在 Visual Studio 中删除 ASP.NET 和 ASP.NET Core Application Insights SDK。This article will show you how to remove the ASP.NET and ASP.NET Core Application Insights SDK in Visual Studio.

要删除 Application Insights,需要从应用程序中的 API 删除 NuGet 包和引用。To remove Application Insights, you'll need to remove the NuGet packages and references from the API in your application. 可在 Visual Studio 中使用“包管理控制台”或“管理 NuGet 解决方案”卸载 NuGet 包。You can uninstall NuGet packages by using the Package Management Console or Manage NuGet Solution in Visual Studio. 以下各部分将介绍两种删除 NuGet 包的方法以及项目中自动添加的内容。The following sections will show two ways to remove NuGet Packages and what was automatically added in your project. 务必要确认已删除添加的文件以及代码中对 API 进行调用的区域。Be sure to confirm the files added and areas with in your own code in which you made calls to the API are removed.

使用包管理控制台卸载Uninstall using the Package Management Console

  1. 要打开“包管理器控制台”,请在顶部菜单中依次选择“工具”->“NuGet 包管理器”->“包管理器控制台”。To open the Package Management Console, in the top menu select Tools > NuGet Package Manager > Package Manager Console.

    在顶部菜单中,单击“工具”>“NuGet 包管理器”>“包管理器控制台”

    备注

    如果启用了跟踪信息收集,则需要先卸载 Microsoft.ApplicationInsights.TraceListener。If trace collection is enabled you need to first uninstall Microsoft.ApplicationInsights.TraceListener. 输入 Uninstall-package Microsoft.ApplicationInsights.TraceListener 然后按照以下步骤删除 Microsoft.ApplicationInsights.Web。Enter Uninstall-package Microsoft.ApplicationInsights.TraceListener then follow the step below to remove Microsoft.ApplicationInsights.Web.

  2. 输入以下命令:Uninstall-Package Microsoft.ApplicationInsights.Web -RemoveDependenciesEnter the following command: Uninstall-Package Microsoft.ApplicationInsights.Web -RemoveDependencies

    输入该命令后,将从项目中卸载 Application Insights 包及其所有依赖项。After entering the command, the Application Insights package and all of its dependencies will be uninstalled from the project.

    在控制台中输入命令

使用 Visual Studio NuGet UI 卸载Uninstall using the Visual Studio NuGet UI

  1. 在右侧的解决方案资源管理器中,右键单击“解决方案”,并选择“为解决方案管理 NuGet 包” 。In the Solution Explorer on the right, right click on Solution and select Manage NuGet Packages for Solution.

    然后,会显示一个屏幕,可在其中编辑属于项目的所有 NuGet 包。You'll then see a screen that allows you to edit all the NuGet packages that are part of the project.

    在“解决方案资源管理器”中,右键单击“解决方案”,并选择“管理解决方案的 NuGet 包”

    备注

    如果启用了跟踪信息收集,需要在未选中“删除依赖项”的情况下,先卸载 Microsoft.ApplicationInsights.TraceListener,然后在选中“删除依赖项”的情况下,按照以下步骤卸载 Microsoft.ApplicationInsights.Web。If trace collection is enabled you need to first uninstall Microsoft.ApplicationInsights.TraceListener without remove dependencies selected and then follow the steps below to uninstall Microsoft.ApplicationInsights.Web with remove dependencies selected.

  2. 单击“Microsoft.ApplicationInsights.Web”包。Click on the "Microsoft.ApplicationInsights.Web" package. 在右侧选中“项目”旁边的复选框以选择所有项目。On the right, check the checkbox next to Project to select all projects.

  3. 若要在卸载时删除所有依赖项,请在选定项目所在的部分下选择“选项”下拉按钮。To remove all dependencies when uninstalling, select the Options dropdown button below the section where you selected project.

    在“卸载选项”下,选中“删除依赖项”旁边的复选框 。Under Uninstall Options , select the checkbox next to Remove dependencies.

  4. 选择“卸载” 。Select Uninstall.

    屏幕截图显示了 Microsoft.ApplicationInsights.Web 窗口,其中选中了“删除依赖项”并突出显示了“卸载”。

    随即出现一个对话框,显示要从应用程序中删除的所有依赖项。A dialog box will display that shows all of the dependencies to be removed from the application. 选择“确定”进行卸载。Select ok to uninstall.

    屏幕截图显示了一个对话框,其中包含要删除的依赖项。

  5. 卸载所有内容后,可能仍会在解决方案资源管理器中看到“ApplicationInsights.config”和“AiHandleErrorAttribute.cs”。After everything is uninstalled, you may still see "ApplicationInsights.config" and "AiHandleErrorAttribute.cs" in the Solution Explorer.  可手动删除这两个文件。You can delete the two files manually.

添加 Application Insights 时创建的内容What is created when you add Application Insights

将 Application Insights 添加到项目时,它会创建文件并在某些文件中添加代码。When you add Application Insights to your project, it creates files and adds code to some of your files. 仅卸载 NuGet 包并不会总是丢弃文件和代码。Solely uninstalling the NuGet Packages will not always discard the files and code. 若要完全删除 Application Insights,应检查并手动删除添加的代码或文件以及在项目中添加的所有 API 调用。To fully remove Application Insights, you should check and manually delete the added code or files along with any API calls you added in your project.

将 Application Insights 遥测添加到 Visual Studio ASP.NET 项目时,它会添加以下文件:When you add Application Insights Telemetry to a Visual Studio ASP.NET project, it adds the following files:

  • ApplicationInsights.configApplicationInsights.config
  • AiHandleErrorAttribute.csAiHandleErrorAttribute.cs

添加了以下代码片段:The following pieces of code are added:

  • [项目名称].csproj[Your project's name].csproj

     <ApplicationInsightsResourceId>/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Default-ApplicationInsights-ChinaNorth/providers/microsoft.insights/components/WebApplication4</ApplicationInsightsResourceId>
    
  • Packages.configPackages.config

    <packages>
    ...
    
      <package id="Microsoft.ApplicationInsights" version="2.12.0" targetFramework="net472" />
      <package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.4.0" targetFramework="net472" />
      <package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.12.0" targetFramework="net472" />
      <package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.12.0" targetFramework="net472" />
      <package id="Microsoft.ApplicationInsights.Web" version="2.12.0" targetFramework="net472" />
      <package id="Microsoft.ApplicationInsights.WindowsServer" version="2.12.0" targetFramework="net472" />
      <package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.12.0" targetFramework="net472" />
    
      <package id="Microsoft.AspNet.TelemetryCorrelation" version="1.0.7" targetFramework="net472" />
    
      <package id="System.Buffers" version="4.4.0" targetFramework="net472" />
      <package id="System.Diagnostics.DiagnosticSource" version="4.6.0" targetFramework="net472" />
      <package id="System.Memory" version="4.5.3" targetFramework="net472" />
      <package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net472" />
      <package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net472" />
    ...
    </packages>
    
  • Layout.cshtmlLayout.cshtml

    如果项目包含一个 Layout.cshtml 文件,会添加以下代码。If your project has a Layout.cshtml file the code below is added.

    <head>
    ...
        <script type = 'text/javascript' >
            var appInsights=window.appInsights||function(config)
            {
                function r(config){ t[config] = function(){ var i = arguments; t.queue.push(function(){ t[config].apply(t, i)})} }
                var t = { config:config},u=document,e=window,o='script',s=u.createElement(o),i,f;for(s.src=config.url||'//az416426.vo.msecnd.net/scripts/a/ai.0.js',u.getElementsByTagName(o)[0].parentNode.appendChild(s),t.cookie=u.cookie,t.queue=[],i=['Event','Exception','Metric','PageView','Trace','Ajax'];i.length;)r('track'+i.pop());return r('setAuthenticatedUserContext'),r('clearAuthenticatedUserContext'),config.disableExceptionTracking||(i='onerror',r('_'+i),f=e[i],e[i]=function(config, r, u, e, o) { var s = f && f(config, r, u, e, o); return s !== !0 && t['_' + i](config, r, u, e, o),s}),t
            }({
                instrumentationKey:'00000000-0000-0000-0000-000000000000'
            });
    
            window.appInsights=appInsights;
            appInsights.trackPageView();
        </script>
    ...
    </head>
    
  • ConnectedService.jsonConnectedService.json

    {
      "ProviderId": "Microsoft.ApplicationInsights.ConnectedService.ConnectedServiceProvider",
      "Version": "16.0.0.0",
      "GettingStartedDocument": {
        "Uri": "https://go.microsoft.com/fwlink/?LinkID=613413"
      }
    }
    
  • FilterConfig.csFilterConfig.cs

            public static void RegisterGlobalFilters(GlobalFilterCollection filters)
            {
                filters.Add(new ErrorHandler.AiHandleErrorAttribute());// This line was added
            }
    

后续步骤Next steps