設定 ASP.NET 網站的 Application InsightsSet up Application Insights for your ASP.NET website

此程序會設定 ASP.NET web 應用程式以將遙測傳送至 Azure Application Insights 服務。This procedure configures your ASP.NET web app to send telemetry to the Azure Application Insights service. 這適用於在您擁有的內部部署 IIS 伺服器或雲端中託管的 ASP.NET 應用程式。It works for ASP.NET apps that are hosted either in your own IIS server on-premises or in the Cloud. 取得圖表和功能強大的查詢語言可幫助您了解您應用程式的效能以及人員如何使用它,再加上發生失敗或效能問題時的自動警示。You get charts and a powerful query language that help you understand the performance of your app and how people are using it, plus automatic alerts on failures or performance issues. 許多開發人員發現這些功能本身就很棒,但是您也可以視需要擴充和自訂遙測。Many developers find these features great as they are, but you can also extend and customize the telemetry if you need to.

在 Visual Studio 中只需按幾下滑鼠即可進行安裝。Setup takes just a few clicks in Visual Studio. 您可以選擇限制遙測的磁碟區來避免產生費用。You have the option to avoid charges by limiting the volume of telemetry. 這可讓您實驗和偵錯,或使用不多的使用者監視網站。This allows you to experiment and debug, or to monitor a site with not many users. 當您決定要繼續監視您的生產網站時,很容易在稍後提升限制。When you decide you want to go ahead and monitor your production site, it's easy to raise the limit later.


若要將 Application Insights 新增至您的 ASP.NET 網站,您必須:To add Application Insights to your ASP.NET website, you need to:

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

步驟 1:新增 Application Insights SDKStep 1: Add the Application Insights SDK


新增 Application Insights 的程序會因 ASP.NET 範本類型而異。The process to add Application Insights varies by ASP.NET template type. 如果您使用 [空白] 或 [Azure 行動裝置應用程式] 範本,請選取 [專案] > [新增 Application Insights 遙測]。If you are using the Empty or Azure Mobile App template select Project > Add Application Insights Telemetry. 如需其他所有 ASP.NET 範本,請參閱下面的指示。For all other ASP.NET templates consult the instructions below.

在 [方案總管] 中以滑鼠右鍵按一下 Web 應用程式名稱,然後選擇 [設定 Application Insights]。Right-click your web app name in the Solution Explorer, and choose Configure Application Insights

醒目提示 [設定 Application Insights] 的 [方案總管] 螢幕擷取畫面

(根據您的 Application Insights SDK 版本,系統可能會提示您升級至最新的 SDK 版本。(Depending on your Application Insights SDK version you may be prompted to upgrade to the latest SDK release. 如果出現提示,請選取 [更新 SDK]。)If prompted, select Update SDK.)

螢幕擷取畫面:有新版的 Microsoft Application Insights SDK 可供使用。

Application Insights 設定畫面:Application Insights Configuration screen:

選取 [開始免費使用]。Select Start Free.

[向 Application Insights 註冊您的應用程式] 頁面的螢幕擷取畫面

如果您想要設定資源群組,或資料的儲存位置,請按一下 [進行設定]。If you want to set the resource group or the location where your data is stored, click Configure settings. 資源群組用來控制資料的存取。Resource groups are used to control access to data. 例如,如果您有數個應用程式組成相同系統時,您可能會將其 Application Insights 資料放在相同的資源群組中。For example, if you have several apps that form part of the same system, you might put their Application Insights data in the same resource group.

選取 [註冊]。Select Register.

[向 Application Insights 註冊您的應用程式] 頁面的螢幕擷取畫面

在偵錯期間和您發佈應用程式之後,遙測會傳送至 Azure 入口網站Telemetry will be sent to the Azure portal, both during debugging and after you have published your app.


如果您不想在您偵錯時將遙測傳送到入口網站,而只將 Application Insights SDK 新增至您的應用程式,但不在入口網站中設定資源。If you don't want to send telemetry to the portal while you're debugging, just add the Application Insights SDK to your app but don't configure a resource in the portal. 您在偵錯時可在 Visual Studio 中檢視遙測。You are able to see telemetry in Visual Studio while you are debugging. 稍後,您可以回到此設定頁面,或等到部署您的應用程式之後,在執行階段開啟遙測Later, you can return to this configuration page, or you could wait until after you have deployed your app and switch on telemetry at run time.

步驟 2︰執行您的應用程式Step 2: Run your app

按 F5 執行您的應用程式。Run your app with F5. 開啟不同的頁面來產生一些遙測。Open different pages to generate some telemetry.

在 Visual Studio 中,您會看見已記錄的事件計數。In Visual Studio, you will see a count of the events that have been logged.

Visual Studio 的螢幕擷取畫面。

步驟 3:查看遙測Step 3: See your telemetry

您可以在 Visual Studio 或 Application Insights Web 入口網站中看到遙測。You can see your telemetry either in Visual Studio or in the Application Insights web portal. 在 Visual Studio 中搜尋遙測可協助您偵錯應用程式。Search telemetry in Visual Studio to help you debug your app. 當系統作用中時監視效能及 web 入口網站的使用量。Monitor performance and usage in the web portal when your system is live.

在 Visual Studio 中查看遙測See your telemetry in Visual Studio

在 Visual Studio 中,若要檢視 Application Insights 資料:In Visual Studio, to view Application Insights data. 選取 [方案總管] > [已連線的服務] > 以滑鼠右鍵按一下 [Application Insights],然後按一下 [搜尋即時遙測]。Select Solution Explorer > Connected Services > right-click Application Insights, and then click Search Live Telemetry.

在 Visual Studio 的 [Application Insights 搜尋] 視窗中,您可以在應用程式的資料中查看應用程式的伺服器端所產生的遙測。In the Visual Studio Application Insights Search window, you will see the data from your application for telemetry generated in the server side of your app. 試驗篩選器,然後按一下任何事件以查看更多詳細資料。Experiment with the filters, and click any event to see more detail.

[Application Insights] 視窗中 [偵錯工作階段中的資料] 檢視的螢幕擷取畫面。


如果您沒有看到任何資料,請確定時間範圍正確,然後按一下 [搜尋] 圖示。If you don't see any data, make sure the time range is correct, and click the Search icon.

深入了解 Visual Studio 中的 Application Insights 工具Learn more about Application Insights tools in Visual Studio.

請參閱 web 入口網站中的遙測See telemetry in web portal

您也可以在 Application Insights Web 入口網站中看到遙測 (除非您選擇只安裝 SDK)。You can also see telemetry in the Application Insights web portal (unless you chose to install only the SDK). 此入口網站中的圖表、分析工具和跨元件檢視比 Visual Studio 還多。The portal has more charts, analytic tools, and cross-component views than Visual Studio. 此入口網站也提供警示。The portal also provides alerts.

開啟 Application Insights 資源。Open your Application Insights resource. 您可以登入 Azure 入口網站加以尋找,或選取 [方案總管] > [已連線的服務] > 以滑鼠右鍵按一下 [Application Insights] > [開啟 Application Insights 入口網站],而移至該處。Either sign into the Azure portal and find it there, or select Solution Explorer > Connected Services > right-click Application Insights > Open Application Insights Portal and let it take you there.

入口網站會從您的應用程式開啟遙測檢視。The portal opens on a view of the telemetry from your app.

Application Insights 概觀頁面的螢幕擷取畫面

在入口網站中,按一下任何圖格或圖表以查看詳細資料。In the portal, click any tile or chart to see more detail.

深入了解在 Azure 入口網站中使用 Application InsightsLearn more about using Application Insights in the Azure portal.

步驟 4:發佈您的應用程式Step 4: Publish your app

將您的應用程式發佈至 IIS 伺服器或 Azure。Publish your app to your IIS server or to Azure. 監看 即時計量串流 以確定一切順利執行。Watch Live Metrics Stream to make sure everything is running smoothly.

您的遙測會累積在 Application Insights 入口網站,您可以在此監視計量,搜尋您的遙測,以及設定儀表板Your telemetry builds up in the Application Insights portal, where you can monitor metrics, search your telemetry, and set up dashboards. 您也可以使用功能強大的 Log Analytics 查詢語言,分析使用狀況和效能或尋找特定事件。You can also use the powerful Log Analytics query language to analyze usage and performance, or to find specific events.

您也可以繼續在 Visual Studio 中,以診斷搜尋和趨勢等工具來分析您的遙測。You can also continue to analyze your telemetry in Visual Studio, with tools such as diagnostic search and trends.


如果應用程式傳送足夠的遙測資料達到節流限制,則會切換為自動取樣If your app sends enough telemetry to approach the throttling limits, automatic sampling switches on. 取樣可減少從應用程式傳送的遙測數量,同時為供診斷之用保留相互關聯的資料。Sampling reduces the quantity of telemetry sent from your app, while preserving correlated data for diagnostic purposes.

您全都準備好了You're all set

恭喜!Congratulations! 您在應用程式中安裝了 Application Insights 套件,並將其設定為將遙測傳送至 Azure 上的 Application Insights 服務。You installed the Application Insights package in your app, and configured it to send telemetry to the Application Insights service on Azure.


接收您應用程式遙測的 Azure 資源會由檢測金鑰識別。The Azure resource that receives your app's telemetry is identified by an instrumentation key. 您可以在 ApplicationInsights.config 檔案中找到此金鑰。You'll find this key in the ApplicationInsights.config file.

升級至未來的 SDK 版本Upgrade to future SDK versions

若要升級至新版的 SDK,請開啟 NuGet 套件管理員,並篩選已安裝的套件。To upgrade to a new release of the SDK, open the NuGet package manager, and filter on installed packages. 選取 Microsoft.ApplicationInsights.Web,然後選擇 [升級]。Select Microsoft.ApplicationInsights.Web, and choose Upgrade.

如果您已對 ApplicationInsights.config 進行任何的自訂,請在升級前儲存複本。If you made any customizations to ApplicationInsights.config, save a copy of it before you upgrade. 然後,將您的變更合併至新版本中。Then, merge your changes into the new version.


後續步驟Next steps

如果您對下列內容感興趣,請查看其他主題︰There are alternative topics to look at if you are interested in:

更多遙測More telemetry



  • 可用性測試:建立測試,以確保網路上看得見您的網站。Availability tests: Create tests to make sure your site is visible on the web.
  • 智慧型診斷︰這些測試會自動執行,您不需要採取任何動作來設定它們。Smart diagnostics: These tests run automatically, so you don't have to do anything to set them up. 它們會讓您知道應用程式是否有不尋常的失敗要求率。They tell you if your app has an unusual rate of failed requests.
  • 計量警示:設定這些警示,當計量超出臨界值時警告您。Metric alerts: Set these to warn you if a metric crosses a threshold. 您可以在撰寫於程式碼中的自訂度量上設定它們。You can set them on custom metrics that you code into your app.