Created
April 25, 2018 22:28
-
-
Save tomasr/cb8e68b5aacfde8568fd9d1e981f504e to your computer and use it in GitHub Desktop.
AppInsightsRouteOperationNameAttribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
namespace WebApplication4 | |
{ | |
public class AppInsightsRouteOperationNameAttribute : ActionFilterAttribute | |
{ | |
public override void OnActionExecuting(ActionExecutingContext filterContext) | |
{ | |
var httpContext = filterContext.HttpContext.ApplicationInstance.Context; | |
var requestTelemetry = httpContext.GetRequestTelemetry(); | |
var method = httpContext.Request.HttpMethod; | |
var origRouteValues = filterContext.RouteData.Values; | |
var routeValues = new RouteValueDictionary(); | |
foreach ( var k in origRouteValues.Keys ) | |
{ | |
if ( k == "controller" || k == "action" || k == "area" ) | |
{ | |
routeValues[k] = origRouteValues[k]; | |
} else | |
{ | |
routeValues[k] = $"[{k}]"; | |
} | |
} | |
var path = "/" + filterContext.RouteData.Route.GetVirtualPath(filterContext.RequestContext, routeValues).VirtualPath; | |
requestTelemetry.Name = $"{method} {path}"; | |
base.OnActionExecuting(filterContext); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment