3

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.

mickl
  • 48,568
  • 9
  • 60
  • 89

2 Answers2

3

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

Jayendran
  • 9,638
  • 8
  • 60
  • 103
  • 1
    yes it could be a solution, thank you. I was wondering if there is something built-in in Azure AI but `HttpContext.Current` can solve this problem – mickl Jul 09 '18 at 08:22
2

Write a TelemetryInitializer where you have access to HttpContext.

//TelemetryInitializer

public void Initialize(ITelemetry telemetry)
    {
     var ctx = HttpContext.Current; // Telemetry Initialzer runs in same thread as the request.
     var request = item as RequestTelemetry;
     req.Properties.Add("MyActionFilter", "MyActionFilterValue")
     ...
    }

//TelemetryProcessor

public void Process(ITelemetry item)
    {
        var request = item as RequestTelemetry;
        //read the flag here and terminate processing
        if(req.Properties["MyActionFilter"] == "somthing")
        {
        ...
        }
    }

https://learn.microsoft.com/en-us/azure/application-insights/app-insights-api-filtering-sampling#itelemetryprocessor-and-itelemetryinitializer

For Asp.Net Core, injecting IHttpContextAccessor to the TelemetryInitializer constructor can get you context, as done here: https://github.com/Microsoft/ApplicationInsights-aspnetcore/blob/develop/src/Microsoft.ApplicationInsights.AspNetCore/TelemetryInitializers/TelemetryInitializerBase.cs

cijothomas
  • 2,818
  • 1
  • 13
  • 25
  • Thanks, it looks like something I'm looking for. The thing is that I'm not using .NET Core and currently installed NuGet package ( `version="2.4.0" targetFramework="net452"`) is not able to recognize `IHttpContextAccessor` is it something .NET Core specific ? – mickl Jul 09 '18 at 07:37
  • For regular (non .net core) you can access `HttpContext.Current`, from a `TelemetryInitializer` as they are run in the same thread as the request. You can inspect the context and set some properties on the Telemetry from the initializer. And later use a `TelemetryProcessor` to take action based on the property set by Initializer. – cijothomas Jul 09 '18 at 23:18