【问题标题】:How to log every web api request in Insights with .NET 4.6.1如何使用 .NET 4.6.1 在 Insights 中记录每个 Web api 请求
【发布时间】:2021-10-11 04:13:50
【问题描述】:

我有一个.NET Framework 4.6.1 解决方案,其中有这个global.asax.cs

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    { Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = EnvironmentHelper.InstrumentationKey;

        HttpConfiguration config = GlobalConfiguration.Configuration;
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }

    protected void Application_Error(Object sender, EventArgs e)
    {
        _telemetry.TrackException(Server.GetLastError());
    }
}

这个WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.Services.Add(typeof(IExceptionLogger), new InsightsExceptionLogger());
    }
}

这个记录器类:

public class InsightsExceptionLogger : ExceptionLogger
{
    public override void Log(ExceptionLoggerContext context)
    {
        if (context != null && context.Exception != null)
        {
            var ai = new TelemetryClient();
            ai.TrackException(context.Exception);
        }

        base.Log(context);
    }
}

这是一个控制器和方法的例子:

public class SomeController : ApiController
{
    [HttpPost, Route("api/v1/Something")]
    public async Task<IHttpActionResult> Something()
    {

问题是我在 Insights 中根本没有收到任何请求。

我需要做什么才能在 Insights 中记录这些 API 调用? (假设不需要简单地添加.TrackRequest()。)

【问题讨论】:

    标签: asp.net-web-api azure-application-insights .net-4.6.1


    【解决方案1】:

    创建一个Request Telemetry initializer 类并添加以下代码。根据请求遥测属性,您可以选择要添加到应用程序见解中的 property

    在请求遥测初始化器类创建后添加

    <Add Type="webapptostoreloginappinsights.ReqTelemetryInitializer,webapptostoreloginappinsights"/>
    

    在遥测处理器下的 applicationinsights.config 文件中

    确保在 global.asax.cs 中调用请求遥测初始化器类

    尝试运行现在能够在 Application Insights 中查看 api 请求的代码

    【讨论】:

      【解决方案2】:

      我最终做的是这个......

      添加包:

      <package id="Microsoft.ApplicationInsights" version="2.18.0" targetFramework="net472" />
      <package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.4.0" targetFramework="net472" />
      <package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.18.0" targetFramework="net472" />
      <package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.18.0" targetFramework="net472" />
      <package id="Microsoft.ApplicationInsights.Web" version="2.18.0" targetFramework="net472" />
      <package id="Microsoft.ApplicationInsights.WindowsServer" version="2.18.0" targetFramework="net472" />
      <package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.18.0" targetFramework="net472" />
      <package id="Microsoft.AspNet.TelemetryCorrelation" version="1.0.8" targetFramework="net472" />
      

      确保web.config &lt;system.web&gt; 包含以下内容:

      <httpModules>
        <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" />
        <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
      </httpModules>
      

      在此过程中,ApplicationInsights.config 文件已创建,部署后应用程序开始在 Insights 中记录 Reuqest 条目。

      【讨论】:

        猜你喜欢
        • 2015-09-15
        • 2018-01-10
        • 2023-02-11
        • 1970-01-01
        • 2014-06-15
        • 2014-12-31
        • 2020-03-30
        • 2013-11-19
        • 1970-01-01
        相关资源
        最近更新 更多