使用 Visual Studio 針對 Azure App Service 中的應用程式進行疑難排解Troubleshoot an app in Azure App Service using Visual Studio

總覽Overview

本教學課程示範如何使用 Visual Studio 工具,協助針對 App Service 中的應用程式進行偵錯,方法是以偵錯模式從遠端執行,或者檢視應用程式記錄與 Web 伺服器記錄。This tutorial shows how to use Visual Studio tools to help debug an app in App Service, by running in debug mode remotely or by viewing application logs and web server logs.

您將了解:You'll learn:

  • Visual Studio 中提供的應用程式管理功能有哪些。Which app management functions are available in Visual Studio.
  • 如何使用 Visual Studio 遠端檢視,對遠端應用程式進行快速變更。How to use Visual Studio remote view to make quick changes in a remote app.
  • 在 Azure 中執行專案時,如何同時針對應用程式和 WebJob 從遠端執行偵錯模式。How to run debug mode remotely while a project is running in Azure, both for an app and for a WebJob.
  • 如何建立應用程式追蹤記錄,並在應用程式建立記錄時加以檢視。How to create application trace logs and view them while the application is creating them.
  • 如何檢視 Web 伺服器記錄,包括詳細的錯誤訊息與失敗的要求追蹤。How to view web server logs, including detailed error messages and failed request tracing.
  • 如何將診斷記錄傳送至 Azure 儲存體帳戶並在該處檢視。How to send diagnostic logs to an Azure Storage account and view them there.

如果您有 Visual Studio Ultimate,您也可以使用 IntelliTrace 進行偵錯。If you have Visual Studio Ultimate, you can also use IntelliTrace for debugging. 本教學課程未涵蓋 IntelliTrace。IntelliTrace is not covered in this tutorial.

必要條件Prerequisites

本教學課程可運用於開發環境、Web 專案與您在在 Azure App Service 中建立 ASP.NET 應用程式中所設定的 App Service 應用程式。This tutorial works with the development environment, web project, and App Service app that you set up in Create an ASP.NET app in Azure App Service. 針對 Webjob 區段, 您將需要在開始使用 AZURE WEBJOBS SDK中建立的應用程式。For the WebJobs sections, you'll need the application that you create in Get Started with the Azure WebJobs SDK.

本教學課程中所提供的程式碼範例適用於 C# MVC Web 應用程式,但是疑難排解程序則是與 Visual Basic 和 Web Form 應用程式一樣。The code samples shown in this tutorial are for a C# MVC web application, but the troubleshooting procedures are the same for Visual Basic and Web Forms applications.

本教學課程假設您使用 Visual Studio 2019。The tutorial assumes you're using Visual Studio 2019.

串流記錄功能僅適用於鎖定 .NET Framework 4 或更新版本的應用程式。The streaming logs feature only works for applications that target .NET Framework 4 or later.

應用程式組態與管理App configuration and management

Visual Studio 可讓您存取 Azure 入口網站中可用的應用程式管理功能與組態設定的子集。Visual Studio provides access to a subset of the app management functions and configuration settings available in the Azure portal. 本節將說明使用伺服器總管可用的項目。In this section, you'll see what's available by using Server Explorer. 若要查看最新的 Azure 整合功能,也請試試 雲端總管To see the latest Azure integration features, try out Cloud Explorer also. 您可以同時從 [檢視] 功能表開啟這兩個視窗。You can open both windows from the View menu.

  1. 如果您尚未在 Visual Studio 中登入 Azure,請在 [伺服器總管] 中以滑鼠右鍵按一下 [Azure],然後選取 [連接到 Microsoft Azure 訂用帳戶]。If you aren't already signed in to Azure in Visual Studio, right-click Azure and select Connect to Microsoft Azure Subscription in Server Explorer.

    替代方式為安裝可讓您存取帳戶的管理憑證。An alternative is to install a management certificate that enables access to your account. 如果您選擇安裝憑證,請以滑鼠右鍵按一下 [伺服器總管] 中的 [Azure] 節點,然後選取內容功能表中的 [管理與篩選訂用帳戶]。If you choose to install a certificate, right-click the Azure node in Server Explorer, and then select Manage and Filter Subscriptions in the context menu. 在 [管理 Microsoft Azure 訂用帳戶] 對話方塊中,按一下 [憑證] 索引標籤,再按一下 [匯入]。In the Manage Microsoft Azure Subscriptions dialog box, click the Certificates tab, and then click Import. 請依照指示下載,然後匯入您 Azure 帳戶的訂用帳戶檔案 (亦稱為 .publishsettings 檔案)。Follow the directions to download and then import a subscription file (also called a .publishsettings file) for your Azure account.

    注意

    如果您下載了訂用帳戶檔案,請將其儲存到原始程式碼目錄以外的資料夾 (例如在 Downloads 資料夾),然後在匯入完成後刪除該檔案。If you download a subscription file, save it to a folder outside your source code directories (for example, in the Downloads folder), and then delete it once the import has completed. 惡意使用者一旦能夠存取此訂閱檔案,就能夠編輯、建立和刪除您的 Azure 服務。A malicious user who gains access to the subscription file can edit, create, and delete your Azure services.

    如需從 Visual Studio 連線至 Azure 資源的詳細資訊,請參閱 管理帳戶、訂閱和系統管理角色For more information about connecting to Azure resources from Visual Studio, see Manage Accounts, Subscriptions, and Administrative Roles.

  2. 在 [伺服器總管] 中,展開 [Azure],然後展開 [App Service]。In Server Explorer, expand Azure and expand App Service.

  3. 展開包含應用程式的資源群組 (該應用程式是您在在 Azure App Service 中建立 ASP.NET 應用程式中建立的),然後使用滑鼠右鍵按一下應用程式節點,然後按一下 [檢視設定]。Expand the resource group that includes the app that you created in Create an ASP.NET app in Azure App Service, and then right-click the app node and click View Settings.

    在伺服器總管中檢視設定

    [Azure Web App] 索引標籤隨即顯示,而您會看到 Visual Studio 中所提供的應用程式管理與組態工作。The Azure Web App tab appears, and you can see there the app management and configuration tasks that are available in Visual Studio.

    Azure Web 應用程式視窗

    在本教學課程中,您將使用記錄與追蹤下拉式清單。In this tutorial, you'll use the logging and tracing drop-downs. 您也會使用遠端偵錯功能,但是將以不同的方式來加以啟用。You'll also use remote debugging but you'll use a different method to enable it.

    如需此視窗中 [應用程式設定] 與 [連接字串] 方塊的詳細資訊,請參閱 Azure App Service:應用程式字串與連接字串的運作方式For information about the App Settings and Connection Strings boxes in this window, see Azure App Service: How Application Strings and Connection Strings Work.

    若您想要執行無法在此視窗中完成的應用程式管理工作,請按一下 [在管理入口網站中開啟],以開啟 Azure 入口網站的瀏覽器視窗。If you want to perform an app management task that can't be done in this window, click Open in Management Portal to open a browser window to the Azure portal.

在伺服器總管中存取應用程式檔案Access app files in Server Explorer

部署 Web 專案時,通常會將 Web.config 檔案中的 customErrors 旗標設為 OnRemoteOnly,這表示出現問題時,您將不會收到有用的錯誤訊息。You typically deploy a web project with the customErrors flag in the Web.config file set to On or RemoteOnly, which means you don't get a helpful error message when something goes wrong. 對許多錯誤而言,您只會看到如下列之一的頁面:For many errors, all you get is a page like one of the following ones:

'/' 應用程式中有伺服器錯誤:Server Error in '/' Application:

Unhelpful error page

發生錯誤:An error occurred:

Unhelpful error page

網站無法顯示頁面The website cannot display the page

Unhelpful error page

要找到錯誤原因最簡單的方式,往往就是啟用詳細的錯誤訊息,而以上第一個螢幕擷取畫面說明的是其做法。Frequently the easiest way to find the cause of the error is to enable detailed error messages, which the first of the preceding screenshots explains how to do. 該做法需要在部署的 Web.config 檔案中進行變更。That requires a change in the deployed Web.config file. 您可以編輯專案中的 Web.config 檔案並重新部署專案,或建立 Web.config 轉換並部署偵錯組建,但還有更快的方法:在 [方案總管] 中使用 [遠端檢視] 功能,直接檢視及編輯遠端應用程式中的檔案。You could edit the Web.config file in the project and redeploy the project, or create a Web.config transform and deploy a debug build, but there's a quicker way: in Solution Explorer, you can directly view and edit files in the remote app by using the remote view feature.

  1. 在 [伺服器總管] 中,依序展開 [Azure]、[App Service] 和應用程式所在的資源群組,然後展開應用程式的節點。In Server Explorer, expand Azure, expand App Service, expand the resource group that your app is located in, and then expand the node for your app.

    您會看到可供您存取應用程式內容檔案與記錄檔的節點。You see nodes that give you access to the app's content files and log files.

  2. 展開 [檔案] 節點,然後按兩下 Web.config 檔案。Expand the Files node, and double-click the Web.config file.

    開啟 Web.config

    Visual Studio 會從遠端應用程式開啟 Web.config 檔案,然後在標題列中的檔案名稱旁邊顯示 [遠端]。Visual Studio opens the Web.config file from the remote app and shows [Remote] next to the file name in the title bar.

  3. 將下列字行新增至 system.web 元素:Add the following line to the system.web element:

    <customErrors mode="Off"></customErrors>

    編輯 Web.config

  4. 重新整理顯示沒有幫助的錯誤訊息的瀏覽器,現在您就會看到詳細的錯誤訊息,例如以下範例:Refresh the browser that is showing the unhelpful error message, and now you get a detailed error message, such as the following example:

    詳細的錯誤訊息

    (顯示的錯誤是透過將紅色字行新增至 Views\Home\Index.cshtml 中來加以建立。)(The error shown was created by adding the line shown in red to Views\Home\Index.cshtml.)

能夠讀取與編輯您的 App Service 應用程式上的檔案,使得疑難排解作業更為輕鬆,編輯 Web.config 檔案僅僅是其中一個範例案例。Editing the Web.config file is only one example of scenarios in which the ability to read and edit files on your App Service app make troubleshooting easier.

遠端偵錯應用程式Remote debugging apps

如果詳細的錯誤訊息無法提供足夠的資訊,而且您無法在本機重現錯誤,另一個疑難排解的方式則是在遠端於偵錯模式中執行。If the detailed error message doesn't provide enough information, and you can't re-create the error locally, another way to troubleshoot is to run in debug mode remotely. 您可以設定中斷點、直接操作記憶體、逐步執行程式碼,甚至變更程式碼路徑。You can set breakpoints, manipulate memory directly, step through code, and even change the code path.

遠端偵錯無法在 Visual Studio 的 Express 版本中運作。Remote debugging does not work in Express editions of Visual Studio.

本節示範如何使用您在在 Azure App Service 中建立 ASP.NET 應用程式中建立的專案進行遠端偵錯。This section shows how to debug remotely using the project you create in Create an ASP.NET app in Azure App Service.

  1. 開啟您在在 Azure App Service 中建立 ASP.NET 應用程式中建立的 Web 專案。Open the web project that you created in Create an ASP.NET app in Azure App Service.

  2. 開啟 Controllers\HomeController.csOpen Controllers\HomeController.cs.

  3. 刪除 About() 方法,然後插入以下程式碼加以取代。Delete the About() method and insert the following code in its place.

    public ActionResult About()
    {
        string currentTime = DateTime.Now.ToLongTimeString();
        ViewBag.Message = "The current time is " + currentTime;
        return View();
    }
    
  4. ViewBag.Message這行設定中斷點Set a breakpoint on the ViewBag.Message line.

  5. 在 [方案總管] 中,於專案上按一下滑鼠右鍵,再按一下 [發行]。In Solution Explorer, right-click the project, and click Publish.

  6. 在 [設定檔] 下拉式清單中,選取您在在 Azure App Service 中建立 ASP.NET 應用程式中所使用的同一設定檔。In the Profile drop-down list, select the same profile that you used in Create an ASP.NET app in Azure App Service. 然後,按一下 [設定]。Then, click Settings.

  7. 在 [發佈] 對話方塊中,按一下 [設定] 索引標籤,然後將 [設定] 變更為 [偵錯],接著按一下 [儲存]。In the Publish dialog, click the Settings tab, and then change Configuration to Debug, and then click Save.

    於偵錯模式中發行

  8. 按一下 [發行]。Click Publish. 當部署完成且您的瀏覽器開啟至應用程式的 Azure URL 之後,請關閉瀏覽器。After deployment finishes and your browser opens to the Azure URL of your app, close the browser.

  9. 在 [伺服器總管] 中,以滑鼠右鍵按一下您的應用程式,接著按一下 [連結偵錯工具]。In Server Explorer, right-click your app, and then click Attach Debugger.

    連結偵錯工具

    瀏覽器會自動開啟至您在 Azure 中執行的首頁。The browser automatically opens to your home page running in Azure. 您可能需要等候 20 秒左右的時間,讓 Azure 設定要偵錯的伺服器。You might have to wait 20 seconds or so while Azure sets up the server for debugging. 通常只有當您第一次在應用程式上執行偵錯模式的 48 小時內,才會出現這個延遲現象。This delay only happens the first time you run in debug mode on an app in a 48-hour period. 當您於相同期間內再次啟動偵錯時,就不會發生延遲。When you start debugging again in the same period, there isn't a delay.

    注意

    如果您有任何啟動偵錯工具的問題,請嘗試使用 Cloud Explorer 執行,而不是伺服器總管If you have any trouble starting the debugger, try to do it by using Cloud Explorer instead of Server Explorer.

  10. 按一下功能表中的 [關於] 。Click About in the menu.

    Visual Studio 會在中斷點處停止,而程式碼是在 Azure 中執行,而不是在您的本機電腦上執行。Visual Studio stops on the breakpoint, and the code is running in Azure, not on your local computer.

  11. 將滑鼠移至 currentTime 變數上,以查看時間值。Hover over the currentTime variable to see the time value.

    檢視 Azure 偵錯模式中執行的變數

    您在 Azure 伺服器時間上看到的時間,可能與您的本機電腦所在的時區不同。The time you see is the Azure server time, which may be in a different time zone than your local computer.

  12. currentTime 變數輸入新的值,例如「現在於 Azure 中執行」。Enter a new value for the currentTime variable, such as "Now running in Azure".

  13. 按 F5 繼續執行。Press F5 to continue running.

    Azure 中執行的 [關於] 頁面會顯示您輸入到 currentTime 變數中的新值。The About page running in Azure displays the new value that you entered into the currentTime variable.

    含有新值的關於頁面

遠端偵錯 WebJobsRemote debugging WebJobs

本節說明如何使用您在 開始使用 Azure WebJobs SDK中建立的專案和應用程式進行遠端偵錯。This section shows how to debug remotely using the project and app you create in Get Started with the Azure WebJobs SDK.

本節顯示的功能僅適用於 Visual Studio 2013 含 Update 4 或更新版本。The features shown in this section are available only in Visual Studio 2013 with Update 4 or later.

遠端偵錯僅適用於連續的 WebJobs。Remote debugging only works with continuous WebJobs. 排程與隨選 WebJobs 不支援偵錯。Scheduled and on-demand WebJobs don't support debugging.

  1. 開啟您在開始使用 AZURE WEBJOBS SDK中建立的 Web 專案。Open the web project that you created in Get Started with the Azure WebJobs SDK.

  2. 在 ContosoAdsWebJob 專案中,開啟 Functions.csIn the ContosoAdsWebJob project, open Functions.cs.

  3. GnerateThumbnail 方法的第一個陳述式上設定中斷點Set a breakpoint on the first statement in the GnerateThumbnail method.

    設定中斷點

  4. 在 [方案總管] 中,以滑鼠右鍵按一下 Web 專案 (不是 WebJob 專案),然後按一下 [發佈]。In Solution Explorer, right-click the web project (not the WebJob project), and click Publish.

  5. 在 [設定檔] 下拉式清單中,選取您在 開始使用 Azure WebJobs SDK中所使用的相同設定檔。In the Profile drop-down list, select the same profile that you used in Get Started with the Azure WebJobs SDK.

  6. 按一下 [設定] 索引標籤,然後將 [組態] 變更為 [偵錯],然後按一下 [發行]。Click the Settings tab, and change Configuration to Debug, and then click Publish.

    Visual Studio 會部署 Web 和 WebJob 專案,且在瀏覽器中開啟您應用程式的 Azure URL。Visual Studio deploys the web and WebJob projects, and your browser opens to the Azure URL of your app.

  7. 在 [伺服器總管] 中,依序展開 [Azure] > [App Service] > 您的資源群組 > 您的應用程式 > [WebJobs] > [連續],然後以滑鼠右鍵按一下 [ContosoAdsWebJob]。In Server Explorer, expand Azure > App Service > your resource group > your app > WebJobs > Continuous, and then right-click ContosoAdsWebJob.

  8. 按一下 [連結偵錯工具]。Click Attach Debugger.

    連結偵錯工具

    瀏覽器會自動開啟至您在 Azure 中執行的首頁。The browser automatically opens to your home page running in Azure. 您可能需要等候 20 秒左右的時間,讓 Azure 設定要偵錯的伺服器。You might have to wait 20 seconds or so while Azure sets up the server for debugging. 通常只有當您第一次在應用程式上執行偵錯模式的 48 小時內,才會出現這個延遲現象。This delay only happens the first time you run in debug mode on an app in a 48-hour period. 當您於相同期間內再次啟動偵錯時,就不會發生延遲。When you start debugging again in the same period, there isn't a delay.

  9. 在開啟至 Contoso 廣告首頁的 Web 瀏覽器中,建立新的廣告。In the web browser that is opened to the Contoso Ads home page, create a new ad.

    建立廣告會建立由 WebJob 挑選並處理的佇列訊息。Creating an ad causes a queue message to be created, which is picked up by the WebJob and processed. 當 WebJobs SDK 呼叫函式來處理佇列訊息時,程式碼便會觸及中斷點。When the WebJobs SDK calls the function to process the queue message, the code hits your breakpoint.

  10. 當偵錯工具在中斷點出現中斷時,您可以檢查並變更在雲端執行程式時的變數值。When the debugger breaks at your breakpoint, you can examine and change variable values while the program is running the cloud. 在下圖中,偵錯工具顯示已傳遞到 GenerateThumbnail 方法之 blobInfo 物件的內容。In the following illustration, the debugger shows the contents of the blobInfo object that was passed to the GenerateThumbnail method.

    偵錯工具中的 blobInfo 物件

  11. 按 F5 繼續執行。Press F5 to continue running.

    GenerateThumbnail 方法完成建立縮圖。The GenerateThumbnail method finishes creating the thumbnail.

  12. 在瀏覽器中重新整理索引頁面,您便會看見縮圖。In the browser, refresh the Index page and you see the thumbnail.

  13. 在 Visual Studio 中,按 SHIFT+F5 停止偵錯。In Visual Studio, press SHIFT+F5 to stop debugging.

  14. 在 [伺服器總管] 中,以滑鼠右鍵在 [伺服器總管] 中,以滑鼠右鍵按一下 ContosoAdsWebJob 節點,然後按一下 [檢視儀表板]。In Server Explorer, right-click the ContosoAdsWebJob node and click View Dashboard.

  15. 使用您的 Azure 認證登入,然後按一下 WebJob 名稱,可前往 WebJob 的頁面。Sign in with your Azure credentials, and then click the WebJob name to go to the page for your WebJob.

    按一下 ContosoAdsWebJob

    儀表板會顯示最近執行的 GenerateThumbnail 函式。The Dashboard shows that the GenerateThumbnail function executed recently.

    (下次按一下 [檢視儀表板] 時,您無需登入,瀏覽器便會直接進入您的 WebJob 頁面。)(The next time you click View Dashboard, you don't have to sign in, and the browser goes directly to the page for your WebJob.)

  16. 按一下函數名稱可查看執行函數的詳細資料。Click the function name to see details about the function execution.

    函式詳細資料

如果函數會撰寫記錄,您可以按一下 [ToggleOutput] 查看這些記錄。If your function wrote logs, you could click ToggleOutput to see them.

遠端偵錯注意事項Notes about remote debugging

  • 不建議在生產環境中執行偵錯模式。Running in debug mode in production is not recommended. 如果您的生產應用程式並未調升規模到多個伺服器執行個體,偵錯就會防止 Web 伺服器回應其他要求。If your production app is not scaled out to multiple server instances, debugging prevents the web server from responding to other requests. 如果您確實有多個 Web 伺服器執行個體,當您連結至偵錯工具時,會取得一個隨機執行個體,而且您無法確保後續瀏覽器要求會傳送到相同的執行個體。If you do have multiple web server instances, when you attach to the debugger, you get a random instance, and you have no way to ensure that subsequent browser requests go to the same instance. 此外,通常我們不會將偵錯組建部署到生產環境中,而且版本組建的編譯器最佳化作業將會使系統無法逐行顯示您的來源程式碼中所發生的事情。Also, you typically don't deploy a debug build to production, and compiler optimizations for release builds might make it impossible to show what is happening line by line in your source code. 針對生產環境問題的疑難排解,您的最佳資源將是應用程式追蹤與 Web 伺服器記錄。For troubleshooting production problems, your best resource is application tracing and web server logs.

  • 進行遠端偵錯時,避免長時間在中斷點停止運作。Avoid long stops at breakpoints when remote debugging. Azure 會將停止超過幾分鐘的處理序視為無回應的處理序,並將其關閉。Azure treats a process that is stopped for longer than a few minutes as an unresponsive process, and shuts it down.

  • 在偵錯期間,伺服器會將資料傳送至 Visual Studio,進而影響頻寬付費情況。While you're debugging, the server is sending data to Visual Studio, which could affect bandwidth charges. 如需關於頻寬費率的詳細資訊,請參閱 Azure 定價For information about bandwidth rates, see Azure Pricing.

  • 確保 Web.config 檔案裡 compilation 元素中的 debug 屬性設為 true。Make sure that the debug attribute of the compilation element in the Web.config file is set to true. 在發行偵錯組建組態時,該值預設會設為 true。It is set to true by default when you publish a debug build configuration.

    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
    
  • 如果您發現偵錯工具不會逐步執行您要進行偵錯的程式碼,可能需要變更 [Just My Code] 設定。If you find that the debugger doesn't step into the code that you want to debug, you might have to change the Just My Code setting. 如需詳細資訊,請參閱指定是否使用 Visual Studio 中的 Just My Code 只對使用者程式碼進行偵錯For more information, see Specify whether to debug only user code using Just My Code in Visual Studio.

  • 當您啟用遠端偵錯功能時,伺服器上會啟動計時器,並在 48 小時後自動關閉此功能。A timer starts on the server when you enable the remote debugging feature, and after 48 hours the feature is automatically turned off. 此 48 小時的限制是為了安全性與效能起見而設計的功能。This 48-hour limit is done for security and performance reasons. 若需要,您可以輕鬆開啟這項功能,次數不限。You can easily turn the feature back on as many times as you like. 當您不需要偵錯時,建議您將其保持為停用。We recommend leaving it disabled when you are not actively debugging.

  • 您可以手動將偵錯工具附加至任何處理序,不僅止於應用程式處理序 (w3wp.exe)。You can manually attach the debugger to any process, not only the app process (w3wp.exe). 如需如何在 Visual Studio 中使用偵錯模式的詳細資訊,請參閱 Visual Studio 偵錯For more information about how to use debug mode in Visual Studio, see Debugging in Visual Studio.

診斷記錄概觀Diagnostic logs overview

在 App Service 應用程式中執行的 ASP.NET 應用程式,可建立下列各種記錄:An ASP.NET application that runs in an App Service app can create the following kinds of logs:

  • 應用程式追蹤記錄Application tracing logs
    此應用程式會呼叫 System.Diagnostics.Trace 類別的方法來建立這些記錄。The application creates these logs by calling methods of the System.Diagnostics.Trace class.
  • Web 伺服器記錄Web server logs
    Web 伺服器會為每個通往應用程式的 HTTP 要求建立記錄項目。The web server creates a log entry for every HTTP request to the app.
  • 詳細的錯誤訊息記錄Detailed error message logs
    Web 伺服器會針對失敗的 HTTP 要求 (產生狀態碼 400 或以上的要求) 建立含有一些額外資訊的 HTML 頁面。The web server creates an HTML page with some additional information for failed HTTP requests (requests that result in status code 400 or greater).
  • 失敗要求追蹤記錄Failed request tracing logs
    Web 伺服器會針對失敗的 HTTP 要求建立含有詳細追蹤資訊的 XML 檔案。The web server creates an XML file with detailed tracing information for failed HTTP requests. Web 伺服器會一併提供 XSL 檔案,在瀏覽器中格式化 XML。The web server also provides an XSL file to format the XML in a browser.

記錄功能會影響應用程式效能,因此 Azure 可讓您視需要啟用或停用每一種記錄類型。Logging affects app performance, so Azure gives you the ability to enable or disable each type of log as needed. 對於應用程式記錄,您可以指定只寫入高於特定嚴重性層級的記錄。For application logs, you can specify that only logs above a certain severity level should be written. 當您建立新的應用程式時,預設會停用所有記錄功能。When you create a new app, by default all logging is disabled.

記錄會寫入應用程式的檔案系統中 LogFiles 資料夾內的檔案,而且可以透過 FTP 存取此檔案。Logs are written to files in a LogFiles folder in the file system of your app and are accessible via FTP. Web 伺服器記錄與應用程式記錄也可以寫入至 Azure 儲存體帳戶。Web server logs and application logs can also be written to an Azure Storage account. 您可以在儲存體帳戶中保留大於檔案系統可容納的記錄數量。You can retain a greater volume of logs in a storage account than is possible in the file system. 當您使用檔案系統時,最大的記錄大小為 100 MB。You're limited to a maximum of 100 megabytes of logs when you use the file system. (檔案系統記錄僅適用於短期保留之用。(File system logs are only for short-term retention. Azure 會在達到此上限之後刪除舊的記錄檔以騰出空間給新的記錄檔使用。)Azure deletes old log files to make room for new ones after the limit is reached.)

建立並檢視應用程式追蹤記錄Create and view application trace logs

在本節中,您將會執行下列工作:In this section, you do the following tasks:

  • 將追蹤陳述式新增至您在開始使用 Azure 和 ASP.NET 中建立的 Web 專案。Add tracing statements to the web project that you created in Get started with Azure and ASP.NET.
  • 當您在本機上執行專案時檢視記錄。View the logs when you run the project locally.
  • 依原樣檢視 Azure 中執行的應用程式所產生的記錄。View the logs as they are generated by the application running in Azure.

如需如何在 WebJobs 中建立應用程式記錄的詳細資訊,請參閱 如何運用 WebJobs SDK 來使用 Azure 佇列儲存體 - 如何寫入記錄For information about how to create application logs in WebJobs, see How to work with Azure queue storage using the WebJobs SDK - How to write logs. 下列有關在 Azure 中檢視記錄和控制記錄儲存方式的指示也同樣適用於 WebJobs 所建立的應用程式記錄。The following instructions for viewing logs and controlling how they're stored in Azure apply also to application logs created by WebJobs.

將追蹤陳述式新增至應用程式Add tracing statements to the application

  1. 開啟 Controllers\HomeController.cs,然後使用下列程式碼來取代IndexAboutContact 方法,以便為 System.Diagnostics 加入 Trace 陳述式與 using 陳述式:Open Controllers\HomeController.cs, and replace the Index, About, and Contact methods with the following code in order to add Trace statements and a using statement for System.Diagnostics:

    public ActionResult Index()
    {
        Trace.WriteLine("Entering Index method");
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
        Trace.TraceInformation("Displaying the Index page at " + DateTime.Now.ToLongTimeString());
        Trace.WriteLine("Leaving Index method");
        return View();
    }
    
    public ActionResult About()
    {
        Trace.WriteLine("Entering About method");
        ViewBag.Message = "Your app description page.";
        Trace.TraceWarning("Transient error on the About page at " + DateTime.Now.ToShortTimeString());
        Trace.WriteLine("Leaving About method");
        return View();
    }
    
    public ActionResult Contact()
    {
        Trace.WriteLine("Entering Contact method");
        ViewBag.Message = "Your contact page.";
        Trace.TraceError("Fatal error on the Contact page at " + DateTime.Now.ToLongTimeString());
        Trace.WriteLine("Leaving Contact method");
        return View();
    }        
    
  2. using System.Diagnostics; 陳述式新增至檔案頂端。Add a using System.Diagnostics; statement to the top of the file.

在本機檢視追蹤輸出View the tracing output locally

  1. 按 F5 以在偵錯模式中執行應用程式。Press F5 to run the application in debug mode.

    預設的追蹤接聽程式會將所有追蹤輸出連同其他「偵錯」輸出,一起寫入到 [輸出] 視窗。The default trace listener writes all trace output to the Output window, along with other Debug output. 下圖顯示您加入 Index 方法的追蹤陳述式的輸出。The following illustration shows the output from the trace statements that you added to the Index method.

    偵錯視窗中的追蹤

    以下步驟說明如何在網頁中檢視追蹤輸出,而不需在偵錯模式中編譯。The following steps show how to view trace output in a web page, without compiling in debug mode.

  2. 開啟應用程式 Web.config 檔 (專案資料夾中的那個) 並將 <system.diagnostics> 元素新增至檔案結尾 </configuration> 元素關閉處前:Open the application Web.config file (the one located in the project folder) and add a <system.diagnostics> element at the end of the file just before the closing </configuration> element:

    <system.diagnostics>
    <trace>
      <listeners>
        <add name="WebPageTraceListener"
            type="System.Web.WebPageTraceListener,
            System.Web,
            Version=4.0.0.0,
            Culture=neutral,
            PublicKeyToken=b03f5f7f11d50a3a" />
      </listeners>
    </trace>
    </system.diagnostics>
    

WebPageTraceListener 可讓您藉由瀏覽至 /trace.axd 來檢視追蹤輸出。The WebPageTraceListener lets you view trace output by browsing to /trace.axd.

  1. 在 Web.config 檔案的 <system.web> 下方,加入追蹤元素,如以下範例所示:Add a trace element under <system.web> in the Web.config file, such as the following example:

    <trace enabled="true" writeToDiagnosticsTrace="true" mostRecent="true" pageOutput="false" />
    
  2. 按 CTRL+F5 執行應用程式。Press CTRL+F5 to run the application.

  3. 在瀏覽器視窗的網址列中, 將trace.axd新增至 URL, 然後按 ENTER (url 類似http://localhost:53370/trace.axd)。In the address bar of the browser window, add trace.axd to the URL, and then press Enter (the URL is similar to http://localhost:53370/trace.axd).

  4. 在 [應用程式追蹤] 頁面上,按一下第一行 (不是 BrowserLink 行) 上的 [檢視詳細資料]。On the Application Trace page, click View Details on the first line (not the BrowserLink line).

    trace.axd

    [要求詳細資訊] 頁面隨即顯示,而且在 [追蹤資訊] 區段中,您會看到先前加入 Index 方法的追蹤陳述式輸出。The Request Details page appears, and in the Trace Information section you see the output from the trace statements that you added to the Index method.

    trace.axd

    根據預設, trace.axd 只能在本機使用。By default, trace.axd is only available locally. 如果您想從遠端應用程式使用它,可以將 localOnly="false" 加入 Web.config 檔案中的 trace 元素,如以下範例所示:If you wanted to make it available from a remote app, you could add localOnly="false" to the trace element in the Web.config file, as shown in the following example:

     <trace enabled="true" writeToDiagnosticsTrace="true" localOnly="false" mostRecent="true" pageOutput="false" />
    

    不過基於安全性考量,建議您不要在生產應用程式中啟用 trace.axdHowever, enabling trace.axd in a production app is not recommended for security reasons. 在以下各節中,您會看到讀取 App Service 應用程式中追蹤記錄的更輕鬆方式。In the following sections, you'll see an easier way to read tracing logs in an App Service app.

在 Azure 中檢視追蹤輸出View the tracing output in Azure

  1. 在 [方案總管] 中,於 Web 專案上按一下滑鼠右鍵,再按一下 [發行]。In Solution Explorer, right-click the web project and click Publish.

  2. 在 [發佈 Web] 對話方塊中,按一下 [發佈]。In the Publish Web dialog box, click Publish.

    當 Visual Studio 成功發行您的更新後,將會開啟瀏覽器視窗至您的首頁 (假設您並未清除 [連線] 索引標籤上的 [目的地 URL])。After Visual Studio publishes your update, it opens a browser window to your home page (assuming you didn't clear Destination URL on the Connection tab).

  3. 在 [伺服器總管] 中,以滑鼠右鍵按一下您的應用程式,然後選取 [檢視串流記錄]。In Server Explorer, right-click your app and select View Streaming Logs.

    在內容功能表中檢視串流記錄

    [輸出] 視窗會顯示您已連線至記錄串流服務,並每一分鐘將沒有要顯示的記錄新增一行通知文字。The Output window shows that you are connected to the log-streaming service, and adds a notification line each minute that goes by without a log to display.

    在內容功能表中檢視串流記錄

  4. 在顯示您的應用程式首頁的瀏覽器視窗中,按一下 [連絡人]。In the browser window that shows your application home page, click Contact.

    幾秒鐘後,您加入 Contact 方法的錯誤層級追蹤輸出便會顯示在 [輸出] 視窗。Within a few seconds, the output from the error-level trace you added to the Contact method appears in the Output window.

    輸出視窗中的錯誤追蹤

    Visual Studio 只會顯示錯誤層級追蹤,因為那是您在啟用記錄監視服務時的預設設定。Visual Studio is only showing error-level traces because that is the default setting when you enable the log monitoring service. 建立新的 App Service 應用程式時,預設會停用所有記錄功能,正如同稍早您在開啟設定頁面時所見:When you create a new App Service app, all logging is disabled by default, as you saw when you opened the settings page earlier:

    應用程式記錄功能關閉

    不過,當您選取 [檢視串流記錄] 時,Visual Studio 會自動將 [Application Logging (File System)] 變更為 [錯誤],代表回報的會是錯誤層級記錄。However, when you selected View Streaming Logs, Visual Studio automatically changed Application Logging(File System) to Error, which means error-level logs get reported. 為了查看所有的追蹤記錄,您可以將此設定變更為 [詳細資訊]。In order to see all of your tracing logs, you can change this setting to Verbose. 當您選取低於錯誤的嚴重性層級時,將一併回報較高嚴重性層級的所有記錄。When you select a severity level lower than error, all logs for higher severity levels are also reported. 因此當您選取詳細資訊時,您會同時看到資訊、警告與錯誤記錄。So when you select verbose, you also see information, warning, and error logs.

  5. 在 [伺服器總管] 中,以滑鼠右鍵按一下應用程式,然後按一下 [檢視設定] (如同您稍早所做的動作)。In Server Explorer, right-click the app, and then click View Settings as you did earlier.

  6. 將 [Application Logging (File System)] 變更為 [詳細資訊],然後按一下 [儲存]。Change Application Logging (File System) to Verbose, and then click Save.

    將追蹤層級設定為詳細資訊

  7. 在顯示您的 [連絡人] 頁面的瀏覽器視窗中,依序按一下 [首頁]、[關於]、[連絡人]。In the browser window that is now showing your Contact page, click Home, then click About, and then click Contact.

    在幾秒鐘內,[輸出] 視窗就會顯示您的所有追蹤輸出。Within a few seconds, the Output window shows all of your tracing output.

    詳細資訊追蹤輸出

    在本節中,您已使用應用程式設定來啟用及停用記錄功能。In this section, you enabled and disabled logging by using app settings. 您也可以修改 Web.config 檔案,來啟用與停用追蹤接聽程式。You can also enable and disable trace listeners by modifying the Web.config file. 不過,修改 Web.config 檔案會導致應用程式網域回收,而透過應用程式設定來啟用記錄功能則不會有這種現象。However, modifying the Web.config file causes the app domain to recycle, while enabling logging via the app configuration doesn't do that. 如果此問題需要經過長時間才會重現或是間歇性出現,則回收應用程式網域可能「修正」此問題,並強迫您等候問題再次發生。If the problem takes a long time to reproduce, or is intermittent, recycling the app domain might "fix" it and force you to wait until it happens again. 在 Azure 中啟用診斷功能可讓您立即開始擷取錯誤資訊,而不需回收應用程式定義域。Enabling diagnostics in Azure lets you start capturing error information immediately without recycling the app domain.

輸出視窗功能Output window features

[輸出] 視窗的 [Microsoft Azure 記錄] 索引標籤具有多個按鈕與一個文字方塊:The Microsoft Azure Logs tab of the Output Window has several buttons and a text box:

記錄索引標籤按鈕

這些物件可執行下列功能:These perform the following functions:

  • 清除 [輸出] 視窗。Clear the Output window.
  • 啟用或停用自動換行。Enable or disable word wrap.
  • 啟動或停止監視記錄。Start or stop monitoring logs.
  • 指定要監視的記錄。Specify which logs to monitor.
  • 下載記錄。Download logs.
  • 依據搜尋字串或規則運算式篩選記錄。Filter logs based on a search string or a regular expression.
  • 關閉 [輸出] 視窗。Close the Output window.

如果您輸入搜尋字串或是規則運算式,則 Visual Studio 會篩選用戶端的記錄資訊。If you enter a search string or regular expression, Visual Studio filters logging information at the client. 亦即,您可以等到 [輸出] 視窗顯示記錄後輸入條件,這樣您不需重新產生記錄便能直接變更篩選條件。That means you can enter the criteria after the logs are displayed in the Output window and you can change filtering criteria without having to regenerate the logs.

檢視 Web 伺服器記錄View web server logs

Web 伺服器記錄會記下應用程式的所有 HTTP 活動。Web server logs record all HTTP activity for the app. 為了在 [輸出] 視窗中看見這些記錄,您必須針對應用程式啟用它們,然後告訴 Visual Studio 您想要監視它們。In order to see them in the Output window, you must enable them for the app and tell Visual Studio that you want to monitor them.

  1. 在您從 [伺服器總管] 開啟的 [Azure Web 應用程式設定] 索引標籤中,將 [Web 伺服器記錄] 變更為 [開啟],然後按一下 [儲存]。In the Azure Web App Configuration tab that you opened from Server Explorer, change Web Server Logging to On, and then click Save.

    啟用 Web 伺服器記錄

  2. 在 [輸出] 視窗中,按一下 [指定要監視的 Microsoft Azure 記錄] 按鈕。In the Output Window, click the Specify which Microsoft Azure logs to monitor button.

    指定要監視的 Azure 記錄

  3. 在 [Microsoft Azure 記錄選項] 對話方塊中,選取 [Web 伺服器記錄],然後按一下 [確定]。In the Microsoft Azure Logging Options dialog box, select Web server logs, and then click OK.

    監視 Web 伺服器記錄

  4. 在顯示應用程式的瀏覽器視窗中,依序按一下 [首頁]、[關於] 和 [連絡人]。In the browser window that shows the app, click Home, then click About, and then click Contact.

    通常會先產生應用程式記錄,然後才是 Web 伺服器記錄。The application logs generally appear first, followed by the web server logs. 您可能需要等候一小段時間,記錄才會顯示。You might have to wait a while for the logs to appear.

    輸出視窗中的 Web 伺服器記錄

根據預設,當您第一次使用 Visual Studio 啟用 Web 伺服器記錄時,Azure 會將記錄寫入檔案系統。By default, when you first enable web server logs by using Visual Studio, Azure writes the logs to the file system. 作為替代方式,您可以使用 Azure 入口網站來指定應該將 Web 伺服器記錄寫入儲存體帳戶中的某個 Blob 容器。As an alternative, you can use the Azure portal to specify that web server logs should be written to a blob container in a storage account.

如果您使用入口網站對 Azure 儲存體帳戶啟用 Web 伺服器記錄功能,然後在 Visual Studio 中停用記錄功能,當您在 Visual Studio 中重新啟用記錄功能時,將會還原您的儲存體帳戶設定。If you use the portal to enable web server logging to an Azure storage account, and then disable logging in Visual Studio, when you re-enable logging in Visual Studio your storage account settings are restored.

檢視詳細的錯誤訊息記錄View detailed error message logs

針對導致錯誤回應代碼 (400 或以上) 之 HTTP 要求,詳細的錯誤訊息記錄可提供部分額外資訊。Detailed error logs provide some additional information about HTTP requests that result in error response codes (400 or above). 為了在 [輸出] 視窗中看見這些記錄,您必須針對應用程式啟用它們,然後告訴 Visual Studio 您想要監視它們。In order to see them in the Output window, you have to enable them for the app and tell Visual Studio that you want to monitor them.

  1. 在您從 [伺服器總管] 開啟的 [Azure Web 應用程式組態] 索引標籤中,將 [詳細錯誤訊息] 變更為 [開啟],然後按一下 [儲存]。In the Azure Web App Configuration tab that you opened from Server Explorer, change Detailed Error Messages to On, and then click Save.

    啟用詳細的錯誤訊息

  2. 在 [輸出] 視窗中,按一下 [指定要監視的 Microsoft Azure 記錄] 按鈕。In the Output Window, click the Specify which Microsoft Azure logs to monitor button.

  3. 在 [Microsoft Azure 記錄選項] 對話方塊中,按一下 [所有記錄],然後按一下 [確定]。In the Microsoft Azure Logging Options dialog box, click All logs, and then click OK.

    監視所有記錄

  4. 在瀏覽器視窗的網址列中,為 URL 加入額外的字元以引發 404 錯誤 (例如, http://localhost:53370/Home/Contactx),然後按 Enter 鍵。In the address bar of the browser window, add an extra character to the URL to cause a 404 error (for example, http://localhost:53370/Home/Contactx), and press Enter.

    幾秒鐘之後,詳細的錯誤記錄就會顯示在 Visual Studio 的 [輸出] 視窗。After several seconds, the detailed error log appears in the Visual Studio Output window.

    輸出視窗:詳細的錯誤記錄

    按住 Ctrl 鍵並按一下該連結,可在瀏覽器中查看格式化的記錄輸出:Control+click the link to see the log output formatted in a browser:

    瀏覽器視窗:詳細的錯誤記錄

下載檔案系統記錄Download file system logs

任何您可在 [輸出] 視窗中監視的記錄,也能下載為 .zip 檔案。Any logs that you can monitor in the Output window can also be downloaded as a .zip file.

  1. 在 [輸出] 視窗中,按一下 [Download Streaming Logs]。In the Output window, click Download Streaming Logs.

    記錄索引標籤按鈕

    [檔案總管] 會開啟至您的 Downloads 資料夾,並選取下載的檔案。File Explorer opens to your Downloads folder with the downloaded file selected.

    下載的檔案

  2. .zip 檔案解壓縮後,您會看到下列資料夾結構:Extract the .zip file, and you see the following folder structure:

    下載的檔案

    • 應用程式追蹤記錄位於 LogFiles\Application 資料夾的 .txt 檔案中。Application tracing logs are in .txt files in the LogFiles\Application folder.

    • Web 伺服器記錄位於 LogFiles\http\RawLogs 資料夾的 .log 檔案中。Web server logs are in .log files in the LogFiles\http\RawLogs folder. 您可以使用 記錄檔剖析器 (英文) 之類的工具來檢視與操作這些檔案。You can use a tool such as Log Parser to view and manipulate these files.

    • 詳細的錯誤訊息記錄位於 LogFiles\DetailedErrors 資料夾的 .html 檔案中。Detailed error message logs are in .html files in the LogFiles\DetailedErrors folder.

      (deployments 資料夾用於存放來源控制發行功能所建立的檔案,它與 Visual Studio 發行功能沒有任何關聯。(The deployments folder is for files created by source control publishing; it doesn't have anything related to Visual Studio publishing. Git 資料夾則用於存放與來源控制發行功能相關的追蹤記錄,以及記錄檔案串流服務。)The Git folder is for traces related to source control publishing and the log file streaming service.)

檢視失敗要求追蹤記錄View failed request tracing logs

當您需要了解 IIS 如何處理 HTTP 要求的詳細資料時,例如在 URL 重新寫入或是出現驗證問題等情況,失敗要求追蹤記錄就會很有用。Failed request tracing logs are useful when you need to understand the details of how IIS is handling an HTTP request, in scenarios such as URL rewriting or authentication problems.

App Service 應用程式會使用 IIS 7.0 及更新版本所提供的相同失敗要求追蹤功能。App Service apps use the same failed request tracing functionality that has been available with IIS 7.0 and later. 但您無法存取用來設定哪些錯誤會被記錄下來的 IIS 設定。You don't have access to the IIS settings that configure which errors get logged, however. 當您啟用失敗要求追蹤時,將擷取所有錯誤。When you enable failed request tracing, all errors are captured.

您可以使用 Visual Studio 來啟用失敗要求追蹤,但是您無法在 Visual Studio 中加以檢視。You can enable failed request tracing by using Visual Studio, but you can't view them in Visual Studio. 這些記錄是 XML 檔案。These logs are XML files. 串流記錄服務只會監視被當作可在純文字模式中讀取的檔案:.txt、.html 與 .log 檔案。The streaming log service only monitors files that are deemed readable in plain text mode: .txt, .html, and .log files.

您可以直接在瀏覽器中透過 FTP,或是在本機使用 FTP 工具將記錄下載到本機電腦中之後檢視失敗要求追蹤記錄。You can view failed request tracing logs in a browser directly via FTP or locally after using an FTP tool to download them to your local computer. 在本節中,您將在瀏覽器中直接檢視它們。In this section, you'll view them in a browser directly.

  1. 在您從 [伺服器總管] 開啟的 [Azure Web 應用程式] 視窗之 [組態] 索引標籤中,將 [失敗要求追蹤] 變更為 [開啟],然後按一下 [儲存]。In the Configuration tab of the Azure Web App window that you opened from Server Explorer, change Failed Request Tracing to On, and then click Save.

    啟用失敗要求追蹤

  2. 在顯示應用程式的瀏覽器視窗網址列中,將額外字元新增至 URL 並按 Enter 鍵以引發 404 錯誤。In the address bar of the browser window that shows the app, add an extra character to the URL and click Enter to cause a 404 error.

    這麼做會讓系統建立失敗要求追蹤記錄,以下步驟將說明如何檢視或下載記錄。This causes a failed request tracing log to be created, and the following steps show how to view or download the log.

  3. 在 Visual Studio 中,於 [Azure Web 應用程式] 視窗的 [組態] 索引標籤中按一下 [在管理入口網站中開啟]。In Visual Studio, in the Configuration tab of the Azure Web App window, click Open in Management Portal.

  4. Azure 入口網站適用於您應用程式的 [設定] 頁面中,按一下 [部署認證],然後輸入新的使用者名稱和密碼。In the Azure portal Settings page for your app, click Deployment credentials, and then enter a new user name and password.

    新的 FTP 使用者名稱與密碼

    注意

    當您登入時,必須使用此完整使用者名稱並在前面加上應用程式名稱。When you log in, you have to use the full user name with the app name prefixed to it. 例如,若您輸入使用者名稱 "myid",而網站為 "myexample",則會登入為 "myexample\myid"。For example, if you enter "myid" as a user name and the site is "myexample", you log in as "myexample\myid".

  5. 在新的瀏覽器視窗中,前往您應用程式之 [概觀] 頁面的 [FTP 主機名稱] 或 [FTPS 主機名稱] 下方所示的 URL。In a new browser window, go to the URL that is shown under FTP hostname or FTPS hostname in the Overview page for your app.

  6. 使用您稍早建立的 FTP 認證登入 (包括使用者名稱的應用程式名稱前置詞)。Sign in using the FTP credentials that you created earlier (including the app name prefix for the user name).

    瀏覽器會顯示應用程式的根資料夾。The browser shows the root folder of the app.

  7. 開啟 LogFiles 資料夾。Open the LogFiles folder.

    開啟 LogFiles 資料夾

  8. 開啟名為 W3SVC 並加上數值的資料夾。Open the folder that is named W3SVC plus a numeric value.

    開啟 W3SVC 資料夾

    該資料夾包含了一些 XML 檔案 (內含任何您在啟用失敗要求追蹤功能後所記錄的錯誤),以及一個可供瀏覽器用來格式化該 XML 檔案的 XSL 檔案。The folder contains XML files for any errors that have been logged after you enabled failed request tracing, and an XSL file that a browser can use to format the XML.

    W3SVC 資料夾

  9. 按一下 XML 檔案,以取得您想要檢視其追蹤資訊的失敗要求。Click the XML file for the failed request that you want to see tracing information for.

    下圖顯示錯誤範例追蹤資訊的片段。The following illustration shows part of the tracing information for a sample error.

    瀏覽器中的失敗要求追蹤

後續步驟Next Steps

在了解 Visual Studio 如何讓您輕鬆地檢視 App Service 應用程式所建立的記錄之後,You've seen how Visual Studio makes it easy to view logs created by an App Service app. 下列各節提供相關主題的更多資源連結:The following sections provide links to more resources on related topics:

  • App Service 疑難排解App Service troubleshooting
  • Visual Studio 偵錯Debugging in Visual Studio
  • 在 Azure 中遠端偵錯Remote debugging in Azure
  • 在 ASP.NET 應用程式中追蹤Tracing in ASP.NET applications
  • 分析 Web 伺服器記錄Analyzing web server logs
  • 分析失敗要求追蹤記錄Analyzing failed request tracing logs
  • 偵錯雲端服務Debugging Cloud Services

App Service 疑難排解App Service troubleshooting

如需在 Azure App Service 中疑難排解應用程式的詳細資訊,請參閱下列資源:For more information about troubleshooting apps in Azure App Service, see the following resources:

如需特定疑難排解問題的說明,請在下列任一個論壇中開啟一段討論串:For help with a specific troubleshooting question, start a thread in one of the following forums:

Visual Studio 偵錯Debugging in Visual Studio

如需如何在 Visual Studio 中使用偵錯模式的詳細資訊,請參閱在 Visual Studio 中偵錯Visual Studio 2010 的偵錯祕訣 (英文)。For more information about how to use debug mode in Visual Studio, see Debugging in Visual Studio and Debugging Tips with Visual Studio 2010.

在 Azure 中遠端偵錯Remote debugging in Azure

如需針對 App Service 應用程式與 WebJob 進行遠端偵錯的詳細資訊,請參閱下列資源:For more information about remote debugging for App Service apps and WebJobs, see the following resources:

如果您的應用程式使用 Azure Web API 或行動服務後端,而您需要加以偵錯,請參閱在 Visual Studio 中對 .NET 後端進行偵錯If your app uses an Azure Web API or Mobile Services back-end and you need to debug that, see Debugging .NET Backend in Visual Studio.

在 ASP.NET 應用程式中追蹤Tracing in ASP.NET applications

網際網路上找不到關於 ASP.NET 追蹤功能詳盡且具時效性的說明。There are no thorough and up-to-date introductions to ASP.NET tracing available on the Internet. 您所能做的,就是從專為 Web Form 所撰寫的一些舊有簡介資料下手,因為 MVC 是最近才問世的技術,並以著重在特定議題的較新的部落格文章來做為補充。The best you can do is get started with old introductory materials written for Web Forms because MVC didn't exist yet, and supplement that with newer blog posts that focus on specific issues. 以下資源是您開始了解這項技術的一些好去處:Some good places to start are the following resources:

針對錯誤記錄,做為撰寫自己的追蹤程式碼的替代方法,便是使用開放原始碼的記錄架構,例如 ELMAHFor error logging, an alternative to writing your own tracing code is to use an open-source logging framework such as ELMAH. 如需詳細資訊,請參閱 Scott Hanselman 關於 ELMAH 的部落格文章(英文)。For more information, see Scott Hanselman's blog posts about ELMAH.

此外,您不需要使用 ASP.NET 或 System.Diagnostics 追蹤從 Azure 取得串流記錄。Also, you don't need to use ASP.NET or System.Diagnostics tracing to get streaming logs from Azure. App Service 應用程式串流記錄服務會串流它在 [LogFiles] 資料夾所找到的任何 .txt.html.log 檔案。The App Service app streaming log service streams any .txt, .html, or .log file that it finds in the LogFiles folder. 因此,您可以建立自己的記錄系統以寫入應用程式的檔案系統,而您的檔案會自動進行串流與下載。Therefore, you could create your own logging system that writes to the file system of the app, and your file is automatically streamed and downloaded. 您只需撰寫會在 d:\home\logfiles 資料夾中建立相關檔案的應用程式碼。All you have to do is write application code that creates files in the d:\home\logfiles folder.

分析 Web 伺服器記錄Analyzing web server logs

如需分析 Web 伺服器記錄的詳細資訊,請參閱下列資源:For more information about analyzing web server logs, see the following resources:

分析失敗要求追蹤記錄Analyzing failed request tracing logs

Microsoft TechNet 網站內的 使用失敗要求追蹤 (英文) 小節可能有助您了解如何使用這些記錄。The Microsoft TechNet website includes a Using Failed Request Tracing section, which may be helpful for understanding how to use these logs. 不過,本文主要著重在 IIS 內設定失敗要求追蹤功能,這是您無法在 Azure App Service 中執行的功能。However, this documentation focuses mainly on configuring failed request tracing in IIS, which you can't do in Azure App Service.