Application Insights不接收任何数据

我正在尝试在我正在使用的.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。根据{{​​3}},我应该看到数据正在发送到dc.services.visualstudio.com。

任何人都可以阐明一下它是如何工作的,或者我是否缺少任何东西?

qazwsxedcr123 回答:Application Insights不接收任何数据

在网络核心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对象,而不是在lib本身中创建它们,从而允许宿主进程为您注入实例,例如

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

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

,

发布问题的答案。还有一些我认为不相关的信息,例如我使用Service Stack for .NET Core,所以我也在此处发布了这些信息。

实现“在服务堆栈中登录”的方式是通常在“程序”和“启动”文件中定义它,然后将其保存到LogFactory。 Service Stack指出,在.NET Core中,它们将所有日志记录工作委派给.NET Core中使用的内置NetCoreLogFactory类-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,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日志记录,但最终我选择了以Application Insights为目标的NLog,如上面的代码所示。

代码现在可以正常工作了,我可以看到数据进入了Application Insights。

本文链接:https://www.f2er.com/3140096.html

大家都在问