0

我知道我可以创建一个 ActionFilter,从当前 actionContext 中获取 ModelState。我可以使用 TelemetryClient 或 ILogger(如果它适用于旧 Web api)并手动错误添加到应用程序洞察属性。但随后我将有两次相同的 400 错误 http 请求。被 azure 和 mine 从 filter 截取...

在我还可以访问 ModelState 的地方,我必须实现什么与 Telemtry 相关的接口,这样我就可以挂钩到整个 Telemtry 跟踪过程?

ITelemetryInitializer和ITelemetryProcessor都无法访问 ModelState!

那么正确的方法是什么?

4

1 回答 1

0

除了在 Global 启动中注册 Global ActionFilter 或 ResultFilter 以通过 TelemetryClient 捕获 ModelState 和 Track 请求之外,您没有太多选择(请记住将其声明为静态以避免 TelemetryClient 的多个实例)。要消除内置请求记录的重复请求,您可以在 ApplicationInsights.config 中禁用 RequestTrackingTelemetryModule,如下所示(注释掉)。当然,您现在需要在新的操作过滤器中记录所有请求,除了模型验证错误,因为您现在禁用了内置的。

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
  <TelemetryModules>
    <Add Type="Microsoft.ApplicationInsights.WindowsServer.DeveloperModeWithDebuggerAttachedTelemetryModule, Microsoft.AI.WindowsServer" />
    <!--<Add Type="Microsoft.ApplicationInsights.Web.RequestTrackingTelemetryModule, Microsoft.AI.Web" />-->
  </TelemetryModules>
  <TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel" />
  <TelemetryInitializers>
    <Add Type="Microsoft.ApplicationInsights.Web.OperationNameTelemetryInitializer, Microsoft.AI.Web" />
    <Add Type="Microsoft.ApplicationInsights.Web.OperationIdTelemetryInitializer, Microsoft.AI.Web" />
  </TelemetryInitializers>
  <InstrumentationKey>00001111-2222-3333-4444-555566667777</InstrumentationKey>
</ApplicationInsights>
于 2020-09-02T13:44:03.183 回答