Search code examples
c#azureasp.net-web-apiazure-application-insightsactionfilterattribute

Web API + Azure App Insights pass the data from ActionFilter to ITelemetryProcessor


I'd like to customize my Application Insights logging behavior. So I'd like to set some sort of flag in my ActionFilter and then read that flag in ITelemetryProcessor.

public class MyCustomFilterAttribute: ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext filterContext)
    {
        //perform some logic and set the flag here
    }
}

and then

public class TelemetryFilter : ITelemetryProcessor
{
    public void Process(ITelemetry item)
    {
        var request = item as RequestTelemetry;
        //read the flag here and terminate processing
    }
}

Is that possible ? Is there some sort of TempData that's shared between those two types ? I'd like to avoid kind of hacks like setting temporary header and so on. Thanks in advance.


Solution

  • I'm not sure this will be useful. But I hope will be.

    Using Activity.Current

        public void Initialize(ITelemetry telemetry)
        {
            Activity current = Activity.Current;
    
            if (current == null)
            {
                current = (Activity)HttpContext.Current?.Items["__AspnetActivity__"];
    //put your code here
            }
    }
    

    Refer this SO