[英]Web API + Azure App Insights pass the data from ActionFilter to ITelemetryProcessor
我想自定义我的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? 我想避免诸如设置临时标头之类的黑客行为。 提前致谢。
我不确定这是否有用。 但我希望会。
使用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
编写一个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")
{
...
}
}
对于Asp.Net Core,将IHttpContextAccessor
注入TelemetryInitializer构造函数可以为您提供上下文,如下所示: https : //github.com/Microsoft/ApplicationInsights-aspnetcore/blob/develop/src/Microsoft.ApplicationInsights.AspNetCore/TelemetryInitializers/TelemetryInitializerBase的.cs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.