Debugging Windows based App Service Startup in Azure

· 4 min read
Debugging Windows based App Service Startup in Azure
Photo by Nubelson Fernandes / Unsplash

Azure Windows based App Services are a fantastic option for hosting native .Net applications in the cloud. In service of the hosted applications, Microsoft provides a number of extremely valuable tools that assist in debugging, performance analysis, transaction tracing, etc. However, one area where they fall flat is on debugging app service startup issues.

Application Insights, Application Log Streams, Website Log Streams all work very well to help diagnose issues with your running application. However, they all require the service to be actively up and logging. What happens when your application crashes on startup before intitializing any logging services? Since the App Service host doesn't log or report any errors that occur on service startup, diagnosing these failures can be difficult.

There are 2 options that I’ve found:

  1. Kudu advanced tools
  2. App Services Editor

Kudu

Kudu is a set of tools that allow you to dig into your service at a much deeper level than what is provided in the Azure dashboard. This includes, but is not limited to:

  • Open a CMD or Powershell window to view the file system directly. 
  • View environment variables and other configuration properties
  • Edit/Update/Delete files or directories (like appsettings.json or web.config)
  • View running processes on the service

Note: In order to have full use of the Kudu tools suite, your app will need to be deployed with the `RUN_FROM_PACKAGE` configuration set to 0, or you can deploy to the service directly from Visual Studio. This results in your code being directly written into the wwwroot directory, in read/write mode, allowing you to access and modify individual files. For context, `RUN_FROM_PACKAGE` set to 1 causes your app to be deployed as a read-only zip file, which obviously limits what you are able to do. Most critically, this prevents you from updating appsettings.json or web.config, which can be important for enabling/disabling features dynamically or enabling informative error messages on the client.

To debug app startup in Kudu:

  1. Kudu is accessed via the sidebar in your app service dashboard, and is contained within the “Development Tools” section. It’s labeled as “Advanced Tools”.
  1. From there, click “Go”:
  1. Open up a CMD Debug Console

  1. A console window should open, placing you in the C:\home directory on launch
  2. Run `cd site/wwwroot` to bring you to the directory that contains your application files
  3. You’ll notice that the contents of the directory you’re in is shown in a tree view above the console window. Identify the exe or dll file of your project.
  4. From within the console window, run the <application>.exe of your project, and you should see startup output – which should include detailed errors identifying what is preventing successful startup.
    1. Alternatively, you can run dotnet <Application>.dll to initialize service startup

Note: This is also a very concise way to view console logging messages generated by the site as it runs. 

App Services Editor

App Services Editor is a new “beta” feature, at the time of writing, also accessed through the sidebar of the Azure Portal App Service Dashboard. It provides yet more tools to the developer, and documentation can be found on the landing page of the service.

  1. To open, select “App Service Editor (preview)” from within the Development Tools section.
  1. Select “Open Editor”
  1. After opening the editor, you will see a window with a sidebar as well as a main display with the title “Quick Start”
  1. Find your <application>.exe in the left bar, right click, and select “Run from Console
  1. This will open a virtual console in the main window, where you can then watch your application initialize, view startup errors, as well as monitor logging activity as the application runs.

While debugging issues during app startup can be challenging, they are not insurmountable. Hopefully the above tips can help you diagnose any current or future problem you may encounter.