How to use Diagnostic Settings with Windows App Services – Logging traces to Event hub/Log Analytics

Prerequisites
- Windows App Services on premium SKU
- The Example Code
- A Log Analytics workspace or Event hub
Link to download the Example Code
Please note that if you are using your own code, and it does not work. it is likely a code related issue. That is why I’m providing known working code.

Deploy your Code
Once you’ve deployed your code ensure the site is loading. If you see an error, check that your .net version is set to .net 9 under the Configuration blade in the portal.

Setup Event Hub
You can skip this step if you’re only using Log Analytics workspaces.
I would suggest creating your own table in the Event Hub. You can tell the diagnostics settings of the App Service to create one. However, I’m not sure when it decides to make the table. Based on testing it will create a table named ‘insights-logs-appserviceapplogs’ after a restart and a few hundred Requests to the site.

You can name the Event Hub anything that fits the requirements, and I would suggest keeping some retention time unless you know you don’t need it.

Send Logging to Event Hub/Log Analytics
From this point it’s time to point the App Service to send App Logs to the Event Hub. Open up the App Service > Diagnostic Settings > Add Diagnostic Setting.

From here ensure you check App Service Application Logs > And select at least 1 destination to send them. I’ve setup both Log Analytics and Event Hub.

Once you’ve saved the Diagnostic Setting, give the App Service a restart.
Then Test that Everything’s working
You can manually load the site to test that this is working. However, Web apps have a built in functionality for load testing which I’ll use.
Note: The app service doesn’t export until it hits a threshold of logs so if you’re loading your site manually, refresh it 150+ times and wait around 15min.
Open the App Service > Load Testing > Create Test

Now Name your test and Add a request URI.

From here after we name the request, we can leave everything else default because the logging we are looking for is generated on the root directory ‘/’. Go ahead and save that by clicking ‘Review + Create’ > Create.

Wait
The logs normally take a little time to be exported. If your manually testing your site, ensure you restarted it after saving the diagnostic settings and refreshed your page 150+ times as there is a threshold for data before it exports it.
Once you’ve sent some logs, open up your EventHub and check your table for your data!

Alternatively you can check for your logs in the Log Analytics workspace if you’ve set that up!
