Веб-API + Azure App Insights передают данные из ActionFilter в ITelemetryProcessor - PullRequest
0 голосов
/ 03 июля 2018

Я хочу настроить поведение при ведении журнала Application Insights. Поэтому я хотел бы установить какой-либо флаг в моем ActionFilter , а затем прочитать этот флаг в ITelemetryProcessor .

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

, а затем

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

Это возможно? Есть ли какие-то данные TempData, которые разделяются между этими двумя типами? Я бы хотел избежать таких хаков, как установка временного заголовка и так далее. Заранее спасибо.

Ответы [ 2 ]

0 голосов
/ 09 июля 2018

Я не уверен, что это будет полезно. Но я надеюсь, что будет.

Использование Activity.Current

    public void Initialize(ITelemetry telemetry)
    {
        Activity current = Activity.Current;

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

См. Это SO

0 голосов
/ 07 июля 2018

Напишите TelemetryInitializer, где у вас есть доступ к 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://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-filtering-sampling#itelemetryprocessor-and-itelemetryinitializer

Для Asp.Net Core, добавление IHttpContextAccessor в конструктор TelemetryInitializer может получить контекст, как это сделано здесь: https://github.com/Microsoft/ApplicationInsights-aspnetcore/blob/develop/src/Microsoft.ApplicationInsights.AspNetCore/TelemetryInitializers/TelemetryInitializerBase.cs

...