Application Insights 未收到任何数据

Application Insights Not Receiving Any Data

我正在尝试在我使用的 .NET Core 应用程序和 Azure Application Insights 之间建立连接。该应用程序本身是一个 API 应用程序,其中后端分为多个服务层。

根据我在网上找到的代码,看来这应该是让它工作的最低限度:

TelemetryClient telemetry = new TelemetryClient(TelemetryConfiguration.CreateDefault());
telemetry.InstrumentationKey = "<my instrumentation key>";
telemetry.TrackEvent("Hello event");
telemetry.TrackPageView("Hello event page view");
telemetry.TrackException(new Exception());
telemetry.TrackTrace("Hello trace message");

我能够在没有任何已知问题的情况下通过上面的代码(即没有调试器失败,或没有显示错误)。但是,在 Chrome Inspector 的“网络”选项卡上进行检查时,我可以看到正在调用我的 API 函数,但没有向 Application Insights 发送跟踪调用。根据 https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-troubleshoot-no-data,我应该看到数据被发送到 dc.services.visualstudio.com。

谁能解释一下这是如何工作的,或者我是否遗漏了什么?

在网络核心 API 中,配置应用洞察设置的推荐方法是通过服务集合上的 AddApplicationInsightsTelemetry 方法,例如

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{       
    services.AddApplicationInsightsTelemetry();
}

有关更多示例,请参阅 https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core

如果您查看此方法的作用,它会为您在底层容器中注册许多组件,这意味着您现在可以从容器创建的实例访问 TelemetryClient 和 TelemetryConfiguration 对象

如果您想对 TelemetryConfiguration 对象执行额外的配置,您可以将其添加到您的 Configure 方法中,例如

public void Configure(IApplicationBuilder app, IHostingEnvironment env, TelemetryConfiguration config)
{
    // do whatever you want to the config object here
}

在您的任何库代码中,您现在应该指定您的对象依赖于 TelemetryClient 对象,而不是在库本身中创建它们,从而允许主机进程为您注入实例,例如

public class MyLibraryClass
{
    TelemetryClient _telemetryClient

    public MyLibraryClass(TelemetryClient telemetryClient)
    {
        _telemetryClient = telemetryClient;     
    }   

    public void Foo()
    {
        _telemetryClient.TrackTrace("Foo");
    }
}

AI SDK 将缓冲和批处理正在检测的进程中的遥测事件。这意味着每次使用 SDK 方法(例如 TrackTrace、TrackEvent 等)不会立即导致对收集端点的 HTTP 调用。当缓冲区已满或缓冲区间隔过去时发送数据,以先发生者为准,

如果愿意,您可以通过将 DeveloperMode 标志传递给 AddApplicationInsightsTelemetry 来覆盖此行为,例如

services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions() {DeveloperMode = true});

这将在每次遥测事件后传输,如果您希望从数据中获得更快的反馈,这将很有用,但显然不是一种非常有效的数据发送方式 - 不要忘记将其关闭!

给post一个问题的答案。还有一些我认为不相关的信息,比如我使用 Service Stack for .NET Core,所以我也post在这里查看这些信息。

Logging in Service Stack的实现方式一般是在Program和Startup文件中定义,然后保存到一个LogFactory中。服务堆栈指出,在 .NET Core 中,它们将所有日志记录工作委托给 .NET Core 中使用的内置 NetCoreLogFactory class - NetCoreLogFactory 可以通过将 ILoggerFactory 传递给它来使用几乎任何日志记录接口,这是定义的在 Program.cs.

我在Program.cs中配置日志接口的代码是:

    public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging(logging => {
                logging.ClearProviders();
                logging.SetMinimumLevel(LogLevel.Trace);
            })
            .UseNLog()
            .UseStartup<Startup>()
            .Build();

在Startup.cs中:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseServiceStack(new AppHost
            {
                AppSettings = new NetCoreAppSettings(Configuration)
            });

            LogManager.LogFactory = new NetCoreLogFactory(loggerFactory, true);
        }

然后服务层可以通过调用 LogManager.GetLogger(GetType()) 来调用此定义。接下来的问题是如何定义 LogFactory。

我曾尝试使用 Microsoft 自己的 Application Insights 日志记录,但最后我决定使用 NLog,并将 Application Insights 用作目标,如上面的代码所示。

代码现在可以运行了,我可以看到数据进入 Application Insights。