C# Application Insights未接收任何数据
我正在尝试在我正在使用的.NET核心应用程序和Azure Application Insights之间建立连接。该应用程序本身是一个API应用程序,其后端被划分为多个服务层 根据我在网上找到的代码,这似乎是让它工作的最低要求:C# Application Insights未接收任何数据,c#,azure,.net-core,azure-application-insights,C#,Azure,.net Core,Azure Application Insights,我正在尝试在我正在使用的.NET核心应用程序和Azure Application Insights之间建立连接。该应用程序本身是一个API应用程序,其后端被划分为多个服务层 根据我在网上找到的代码,这似乎是让它工作的最低要求: TelemetryClient telemetry = new TelemetryClient(TelemetryConfiguration.CreateDefault()); telemetry.InstrumentationKey = "<my instrume
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");
遥测客户端遥测=新的遥测客户端(遥测配置.CreateDefault());
telemetry.InstrumentationKey=“”;
TrackEvent(“Hello事件”);
TrackPageView(“Hello事件页面视图”);
TrackException(newexception());
TrackTrace(“你好跟踪消息”);
我能够在没有任何已知问题(即没有调试器故障或显示错误)的情况下通过上述代码。但是,在Chrome Inspector的网络选项卡上检查时,我可以看到对我的API函数的调用,但没有向Application Insights发送跟踪调用。根据,我应该看到数据被发送到dc.services.visualstudio.com
任何人都可以解释一下这是如何工作的,或者如果我遗漏了什么吗?在net core API中,配置app insights设置的推荐方法是通过服务集合上的AddApplicationInsightsTelemetry方法,例如
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry();
}
有关更多示例,请参见
如果您查看此方法的功能,它将为您在底层容器中注册许多组件,这意味着您现在可以从容器创建的实例访问遥测客户端和遥测配置对象
如果要对遥测配置对象执行其他配置,可以将其添加到配置方法中,如
public void Configure(IApplicationBuilder app, IHostingEnvironment env, TelemetryConfiguration config)
{
// do whatever you want to the config object here
}
在任何库代码中,您现在都应该指定对象依赖于遥测客户端对象,而不是在库本身中创建它们,从而允许主机进程为您注入实例,例如
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});
这将在每次遥测事件后传输,如果您希望从数据中获得更快的反馈,这将非常有用,但显然不是一种非常有效的发送数据的方式-不要忘记关闭它 在net core API中,配置app insights设置的推荐方法是通过服务集合上的AddApplicationInsightsTelemetry方法,例如:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry();
}
有关更多示例,请参见
如果您查看此方法的功能,它将为您在底层容器中注册许多组件,这意味着您现在可以从容器创建的实例访问遥测客户端和遥测配置对象
如果要对遥测配置对象执行其他配置,可以将其添加到配置方法中,如
public void Configure(IApplicationBuilder app, IHostingEnvironment env, TelemetryConfiguration config)
{
// do whatever you want to the config object here
}
在任何库代码中,您现在都应该指定对象依赖于遥测客户端对象,而不是在库本身中创建它们,从而允许主机进程为您注入实例,例如
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});
这将在每次遥测事件后传输,如果您希望从数据中获得更快的反馈,这将非常有用,但显然不是一种非常有效的发送数据的方式-不要忘记关闭它 发布问题的答案。还有一些我认为不相关的信息,比如我使用.NETCore的服务堆栈,所以我也在这里发布这些信息 服务堆栈中日志记录的实现方式是,它通常在程序和启动文件中定义,然后保存到LogFactory。服务堆栈声明,在.NET Core中,它们将所有日志工作委托给.NET Core中使用的内置NetCoreLogFactory类-NetCoreLogFactory可以通过向其传递在Program.cs中定义的ILoggerFactory来使用几乎任何日志接口 我用于在Program.cs中配置日志记录接口的代码是:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(logging => {
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
})
.UseNLog()
.UseStartup<Startup>()
.Build();
然后,服务层可以通过调用LogManager.GetLogger(GetType())来调用此定义。接下来的问题是如何定义LogFactory
我曾尝试使用Microsoft自己的Application Insights日志记录,但最终我决定使用NLog,将Application Insights作为目标,如上面的代码所示
代码现在可以工作了,我可以看到数据进入到应用程序洞察中。发布问题的答案。还有一些我认为不相关的信息,比如我使用.NETCore的服务堆栈,所以我也在这里发布这些信息 服务堆栈中日志记录的实现方式是,它通常在程序和启动文件中定义,然后保存到LogFactory。服务堆栈声明,在.NET Core中,它们将所有日志工作委托给.NET Core中使用的内置NetCoreLogFactory类-NetCoreLogFactory可以通过向其传递在Program.cs中定义的ILoggerFactory来使用几乎任何日志接口 我用于在Program.cs中配置日志记录接口的代码是:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(logging => {
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
})
.UseNLog()
.UseStartup<Startup>()
.Build();
然后,服务层可以通过调用LogManage调用此定义