Capturing ASP.NET Framework RawUrl with Azure Application Insights
Azure Application Insights is an Application Performance Management (APM) tool providing insights into the state of your application. By default, Application Insights will capture a lot of data about your ASP.NET applications including HTTP Requests made to your website. Unfortunately, the URL captured by Application Insights doesn't always match the URL originally requested by the client.
In many ASP.NET applications, especially CMS's like DNN, the path of the HTTP request is internally rewritten by the time Application Insights records the data. This results in the rewritten URL to be captured and not the original URL. You can simulate this scenario with a simple Context.RewritePath inside of the Application_BeginRequest method of the global.asax.cs file:
In this sample, when /about is requested, the path is rewritten to /home/about which will resolve to the HomeController.About action. When you browse to /about and look at the recorded HTTP request inside of Application Insights, the URL will be https://domain.tld/home/about instead of https://domain.tld/about. The rewritten URL is useful information, but you may also need the original URL for proper debugging/analyzing.
Luckily, there's a property called RawUrl on the request that will always have the original path including the querystring. Application Insights provides an extensibility point for you to capture additional data using the ITelemetryInitializer interface. You can capture the original URL by implementing ITelemetryInitializer and adding the RawUrl data to the telemetry properties:
Whenever an HTTP Request telemetry is initialized, this initializer will grab the current HTTP Request and capture the RawUrl and the RawUrlFqdn. The RawUrlFqdn will include the protocol, full domain, port, path, and querystring.
For Application Insights to use the RawUrlTelemetryInitializer, you must add a reference to the class in ApplicationInsights.config:
This is what the resulting data looks like when sent to Application Insights:
Notice how the url, properties.Rawurl, and properties.RawUrlFqdn are all capture providing a more complete picture of the request:
| url | https://localhost:44308/home/about |
| properties.RawUrlFqdn | https://localhost:44308/about |
| properties.RawUrl | /about |
Summary #
Using the ITelemetryInitializer extensibility point in Application Insights, you can capture additional data. Using the RawUrl on the HttpContext.Request, you can capture the original URL in addition to the rewritten URL which is captured by default.