简体   繁体   English

Application Insights 未收到任何数据

[英]Application Insights Not Receiving Any Data

I'm trying to set up a connection between a .NET Core app that I am using, and Azure Application Insights.我正在尝试在我正在使用的 .NET Core 应用程序和 Azure Application Insights 之间建立连接。 The app itself is an API app, where the back end is split into a number of service layers.该应用程序本身是一个 API 应用程序,其中后端分为多个服务层。

Based on code that I've found online, it appears that this should be the bare minimum to get it to work:根据我在网上找到的代码,看来这应该是让它工作的最低要求:

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");

I am able to go past the code above without any known issues (ie no debugger failures, or no displayed errors).我能够通过上面的代码 go 没有任何已知问题(即没有调试器故障,或没有显示错误)。 However, inspecting on the Network tab in Chrome Inspector, I can see the call to my API function being made, but no tracking call sent to Application Insights.但是,在 Chrome Inspector 的“网络”选项卡上进行检查时,我可以看到对我的 API function 的调用,但没有发送到 Application Insights 的跟踪调用。 According to https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-troubleshoot-no-data , I should be seeing data being sent to dc.services.visualstudio.com.根据https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-troubleshoot-no-data ,我应该看到数据正在发送到 dc.services.visualstudio.com。

Can anyone please shed some light on how this works, or if I am missing anything?谁能解释一下这是如何工作的,或者我是否遗漏了什么?

In a net core API, the recommended way to configure your app insights setup is via the AddApplicationInsightsTelemetry method on the services collection, eg在网络核心 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();
}

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

If you look at what this method does, it will register a number of components for you with the underlying container, meaning you can now access TelemetryClient and TelemetryConfiguration objects from container created instances如果您查看此方法的作用,它将为您在底层容器中注册许多组件,这意味着您现在可以从容器创建的实例中访问 TelemetryClient 和 TelemetryConfiguration 对象

If you want to perform additional configuration to the TelemetryConfiguration object, you can have that added to your Configure method like,如果要对 TelemetryConfiguration object 执行其他配置,可以将其添加到 Configure 方法中,例如,

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

In any of your library code, you should now specify that your object takes a dependency on a TelemetryClient object rather than creating them within the lib itself, allowing the host process to inject instances for you, eg在您的任何库代码中,您现在应该指定您的 object 依赖于 TelemetryClient object 而不是在库本身中创建它们,从而允许主机进程为您注入实例,例如

public class MyLibraryClass
{
    TelemetryClient _telemetryClient

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

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

The AI SDK will buffer and batch telemetry events in the process being instrumented. AI SDK 将在被检测的过程中缓冲和批处理遥测事件。 This means that each usage of an SDK method - such as TrackTrace, TrackEvent etc - does not immediately result in an HTTP call to the collection endpoint.这意味着每次使用 SDK 方法(例如 TrackTrace、TrackEvent 等)不会立即导致对收集端点的 HTTP 调用。 Data is transmitted when the buffer is full or the buffer interval passes, whichever happens first,当缓冲区已满或缓冲区间隔过去时传输数据,以先发生者为准,

You can override this behaviour if you like, by passing in a DeveloperMode flag to AddApplicationInsightsTelemetry, like,如果您愿意,可以通过将 DeveloperMode 标志传递给 AddApplicationInsightsTelemetry 来覆盖此行为,例如,

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

This will transmit after every telemetry event, which is useful if you want faster feedback from your data but obviously not a very efficient way of sending data - don't forget to turn it off!这将在每次遥测事件后传输,如果您希望从数据中获得更快的反馈,这很有用,但显然不是一种非常有效的数据发送方式 - 不要忘记将其关闭!

To post an answer to the question.发布问题的答案。 There's a few more information I did not think would have been relevant, such as me using Service Stack for .NET Core, so I am posting the information here as well.还有一些我认为不相关的信息,例如我使用 .NET Core 的服务堆栈,所以我也在此处发布信息。

The way Logging in Service Stack is implemented is that it is generally defined in the Program and Startup files, then saved to a LogFactory. Logging in Service Stack 的实现方式一般是在 Program 和 Startup 文件中定义,然后保存到 LogFactory 中。 Service Stack states that in .NET Core, they delegate all logging work to the in-built NetCoreLogFactory class used in .NET Core - NetCoreLogFactory can use pretty much any logging interface by passing in an ILoggerFactory to it, which is defined in Program.cs.服务堆栈指出,在 .NET 核心中,他们将所有日志记录工作委托给 .NET 核心中使用的内置 NetCoreLogFactory class - NetCoreLogFactory 可以使用几乎任何通过在 IcsLogFactory 中定义的日志记录接口,

The code I use to configure the logging interface in Program.cs is:我用来在 Program.cs 中配置日志接口的代码是:

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

And in Startup.cs:在 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);
        }

The service layers can then call upon this definition by calling LogManager.GetLogger(GetType()).然后,服务层可以通过调用 LogManager.GetLogger(GetType()) 来调用此定义。 The issue then is how the LogFactory would be defined.那么问题是如何定义 LogFactory。

I have tried to use Microsoft's own Application Insights logging, but in the end I settled with going for NLog with using Application Insights as a target, as shown in the above code.我曾尝试使用 Microsoft 自己的 Application Insights 日志记录,但最后我决定使用 NLog 并使用 Application Insights 作为目标,如上面的代码所示。

The code works now and I can see data going into Application Insights.该代码现在可以运行,我可以看到数据进入 Application Insights。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM