Web API + Azure App Insights 将数据从 ActionFilter 传递到 ITelemetryProcessor
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 ?我想避免像设置临时 header 这样的黑客攻击等等。提前致谢。
在您有权访问 HttpContext 的位置编写 TelemetryInitializer。
//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")
...
}
//遥测处理器
public void Process(ITelemetry item)
{
var request = item as RequestTelemetry;
//read the flag here and terminate processing
if(req.Properties["MyActionFilter"] == "somthing")
{
...
}
}
对于 Asp.Net 核心,将 IHttpContextAccessor
注入 TelemetryInitializer 构造函数可以获得上下文,如此处完成:
https://github.com/Microsoft/ApplicationInsights-aspnetcore/blob/develop/src/Microsoft.ApplicationInsights.AspNetCore/TelemetryInitializers/TelemetryInitializerBase.cs
我不确定这是否有用。但我希望会。
使用Activity.Current
public void Initialize(ITelemetry telemetry)
{
Activity current = Activity.Current;
if (current == null)
{
current = (Activity)HttpContext.Current?.Items["__AspnetActivity__"];
//put your code here
}
}
参考这个
我想自定义我的 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 ?我想避免像设置临时 header 这样的黑客攻击等等。提前致谢。
在您有权访问 HttpContext 的位置编写 TelemetryInitializer。
//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")
...
}
//遥测处理器
public void Process(ITelemetry item)
{
var request = item as RequestTelemetry;
//read the flag here and terminate processing
if(req.Properties["MyActionFilter"] == "somthing")
{
...
}
}
对于 Asp.Net 核心,将 IHttpContextAccessor
注入 TelemetryInitializer 构造函数可以获得上下文,如此处完成:
https://github.com/Microsoft/ApplicationInsights-aspnetcore/blob/develop/src/Microsoft.ApplicationInsights.AspNetCore/TelemetryInitializers/TelemetryInitializerBase.cs
我不确定这是否有用。但我希望会。
使用Activity.Current
public void Initialize(ITelemetry telemetry)
{
Activity current = Activity.Current;
if (current == null)
{
current = (Activity)HttpContext.Current?.Items["__AspnetActivity__"];
//put your code here
}
}
参考这个