設定 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 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.

開始之前Before you start

您需要:You need:

  • Visual Studio 2013 Update 3 或更新版本。Visual Studio 2013 update 3 or later. 越新版越好。Later is better.
  • Microsoft Azure訂用帳戶。A subscription to Microsoft Azure. 如果您的小組或組織擁有 Azure 訂用帳戶,擁有者就可以使用您的 Microsoft 帳戶將您加入。If your team or organization has an Azure subscription, the owner can add you to it, by using your Microsoft account.

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

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

在 [方案總管] 中以滑鼠右鍵按一下 Web 應用程式,然後選擇 [新增] > [Application Insights 遙測] 或 [設定 Application Insights]。Right-click your web app project in Solution Explorer, and choose Add > Application Insights Telemetry... or Configure Application Insights.

[方案總管] 的螢幕擷取畫面,其中已醒目提示 [新增 Application Insights 遙測]

(在 Visual Studio 2015 中,[新增專案] 對話方塊中也有選項可新增 Application Insights。)(In Visual Studio 2015, there's also an option to add Application Insights in the New Project dialog.)

繼續移至 Application Insights 設定頁面︰Continue to the Application Insights configuration page:

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

a.a. 選取您用來存取 Azure 的帳戶和訂用帳戶。Select the account and subscription that you use to access Azure.

b.b. 在 Azure 中選取資源,以查看來自應用程式的資料。Select the resource in Azure where you want to see the data from your app. 通常︰Usually:

如果您想要設定資源群組,或資料的儲存位置,請按一下 [進行設定]。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.

c.c. 設定可用資料磁碟區限制的上限,以避免產生費用。Set a cap at the free data volume limit, to avoid charges. 在一定的遙測數量之內,Application Insights 免費。Application Insights is free up to a certain volume of telemetry. 建立資源之後,您可以在入口網站中開啟 [功能 + 定價] > [資料量管理] > [每日用量上限],以變更您的選擇。After the resource is created, you can change your selection in the portal by opening Features + pricing > Data volume management > Daily volume cap.

d.d. 按一下 [註冊],繼續設定 Web 應用程式的 Application Insights。Click Register to go ahead and configure Application Insights for your web app. 在偵錯期間和您發佈應用程式之後,遙測會傳送至 Azure 入口網站Telemetry will be sent to the Azure portal, both during debugging and after you have published your app.

e.e. 如果您不想在您偵錯時將遙測傳送到入口網站,而只將 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 will be 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 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, open the Application Insights window. 按一下 [Application Insights] 按鈕,或以滑鼠右鍵按一下 [方案總管] 中的專案﹐選取 [Application Insights]﹐然後按一下 [搜尋即時遙測]。Either click the Application Insights button, or right-click your project in Solution Explorer, select Application Insights, and then click Search Live Telemetry.

在 Visual Studio 的 [Application Insights 搜尋] 視窗中,利用 [偵錯工作階段中的資料] 檢視,查看應用程式的伺服器端所產生的遙測。In the Visual Studio Application Insights Search window, see the Data from Debug session view 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 入口網站並找到它,或以滑鼠右鍵按一下 Visual Studio 中的專案並讓它帶您前往。Either sign in to the Azure portal and find it there, or right-click the project in Visual Studio, and let it take you there.

Visual Studio 的螢幕擷取畫面,顯示如何開啟 Application Insights 入口網站


如果您收到存取錯誤:您是否有一組以上的 Microsoft 認證,且是否以錯誤的組合登入?If you get an access error: Do you have more than one set of Microsoft credentials, and are you signed in with the wrong set? 在入口網站中,登出後重新登入。In the portal, sign out and sign in again.

入口網站會從您的應用程式開啟遙測檢視。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 again, 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

更多遙測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.