azure-application-insights - 使用依赖注入在派生的 ActionFilterAttribute 中获取 RequestTelemetry(或 ITelemetry)
问题描述
如何使用依赖注入ITelemety
在 ASP.NET Core 自定义中获取对象的对象?ActionFilterAttribute
平台 - .Net Core
需要什么:ITelemetry 对象
示例代码供参考
public class CustomActionFilterAttribute : ActionFilterAttribute
{
private readonly ITelemetry telemetry;
public CustomActionFilterAttribute(ITelemetry telemetry)
{
this.telemetry= telemetry;
}
}
我需要解析ITelemetry
对象以在请求上下文中添加一些全局属性,以便可以将这些属性记录在请求日志中。ITelemetry
使用上面的示例代码,当DI 未解析对象时会引发异常。
在CustomActionFilterAttribute
启动时注入如下所示。
services.AddMvc(options =>
{
options.Filters.Add<CustomActionFilterAttribute>();
});
解决方案
推荐阅读
- mysql - 需要 mariadb 中复杂查询矩阵输出的解决方案
- google-apps-script - 如何根据 Google 表格上的字体颜色计算单元格?
- firebase - 在 Cloud Firestore 安全规则中将字符串转换为 Int
- asp.net - IIS/ASP.NET 接收从外部应用程序到 SOAP 代理服务器的调用
- c# - StringBuilder("...").ToString() 和 new String() 的行为不同。为什么?
- python-2.7 - 使用 Dataflow 发布到 Bigquery
- python - Python Kivy TabbedPanel - 设置自定义标签宽度
- javascript - 本地存储,以及如何延迟 div 可见性
- raku - 从另一个 Perl 6 文件访问 POD
- java - Java中的ArrayList和泛型