问题描述
我正在尝试将异常记录到 Application Insights.我通过直接调用 TelemetryClient.TrackException
成功地做到了这一点.但是,我想在我的代码中对此进行抽象,以防将来我想登录到其他平台,所以我只想坚持 ILogger 接口.
I'm trying to log exceptions to Application Insights. I succeeded in doing this by calling TelemetryClient.TrackException
directly. However, I would like to abstract away from this in my code in case I'd want to log to other platforms in the future, so I would like to stick to only the ILogger interface.
我发现您可以使用 ILoggerFactory.AddApplicationInsights
(已实现 here) 但无论我做什么,我都看不到 ApplicationInsights 中显示的日志.
I found that you can use ILoggerFactory.AddApplicationInsights
(as implemented here) but no matter what I did, I don't see the logs showing up in ApplicationInsights with this.
下面是我的代码:
Startup.cs
IConfigurationRoot Configuration { get; set; }
ILoggerFactory LoggerFactory { get; set; }
IServiceProvider ServiceProvider { get; set; }
public Startup( IHostingEnvironment hostingEnvironment, ILoggerFactory loggerFactory )
{
this.LoggerFactory = loggerFactory;
string configurationFilePath = "abc.json";
this.Configuration = new ConfigurationBuilder()
.SetBasePath( hostingEnvironment.ContentRootPath )
.AddJsonFile( configurationFilePath, optional: true, reloadOnChange: true )
.AddEnvironmentVariables()
.Build();
}
public void Configure(
IApplicationBuilder applicationBuilder,
IHostingEnvironment hostingEnvironment,
ILoggerFactory loggerFactory,
IServiceProvider serviceProvider )
{
this.ServiceProvider = serviceProvider;
loggerFactory.AddApplicationInsights( serviceProvider );
applicationBuilder.UseMvc();
}
public void ConfigureServices( IServiceCollection services )
{
services.AddApplicationInsightsTelemetry( this.Configuration );
services.AddMvc( .... // A bunch of options here ... )
}
然后,我尝试像这样在我的控制器中使用它:
Then, I try to use this in my controller like this:
ILogger<XController> Logger { get; set; }
public XController( ILogger<XController> logger )
{
this.Logger = logger;
}
[HttpPost]
[Route( "v3.0/abcd" )]
public async Task PostEvent( [FromBody] XEvent xEvent )
{
this.Logger.LogError( 0, new Exception( "1234" ), "1234" );
}
但是,我根本没有看到与该请求相关的任何异常.如果我用 TelemetryClient.TrackException
替换 Logger.LogError
行(并首先创建 TelemetryClient
),那么我可以看到异常没有任何问题.
However, I don't see any exceptions associated with the request at all. If I replace the Logger.LogError
line with TelemetryClient.TrackException
(and create the TelemetryClient
first), then I can see the exception without any issues.
我不知道我做错了什么.有人可以帮忙吗?
I don't know what I'm doing wrong. Could anyone help?
推荐答案
我终于想通了.有两种方法可以解决我的问题:
I finally figured out. There are two ways to solve my issue:
简单的方法:
我使用的是旧版本的 Application Insights NuGet.具体来说,Microsoft.ApplicationInsights.2.2.0
和 Microsoft.ApplicationInsights.AspNetCore.2.0.0
.
I was using older versions of the Application Insights NuGets. Specifically,
Microsoft.ApplicationInsights.2.2.0
and Microsoft.ApplicationInsights.AspNetCore.2.0.0
.
一旦我升级到Microsoft.ApplicationInsights.2.4.0
和 Microsoft.ApplicationInsights.AspNetCore.2.1.1
,一切正常.您还可以看到 LogXXX
有一个异常作为参数显示为 Exception,而一个没有异常的参数显示为 Trace.
Once I upgrade to
Microsoft.ApplicationInsights.2.4.0
and Microsoft.ApplicationInsights.AspNetCore.2.1.1
, everything works as expected. You also see the LogXXX
with an exception as an argument showing up as Exception, and one without an exception showing up as Trace as expected.
稍微困难的方法:
由于某种原因,Configure
中的 IApplicationBuilder
和 IServiceProvider
没有提供在 AddApplicationInsights中使用的正确服务提供程序code>,所以需要在ConfigureServices中添加provider:
For some reason, the IApplicationBuilder
and IServiceProvider
in Configure
does not provide the correct service provider to use in AddApplicationInsights
, so you need to add the provider in the ConfigureServices:
public void ConfigureServices( IServiceCollection services )
{
IServiceProvider serviceProvider = services.BuildServiceProvider();
this.LoggerFactory.AddApplicationInsights( serviceProvider, Extensions.Logging.LogLevel.Information );
...
}
这意味着您需要将构造函数中的 loggerFactory
保存到属性/字段中,因为它不能通过 ConfigureServices
中的依赖注入获得.
This means that you need to save the loggerFactory
from the constructor into a property/field since it's not available via dependency injection in ConfigureServices
.
我最终做了什么(在我看来可能是目前最好的解决方案):
即使只执行上述任一解决方案都可以解决问题,但我还是决定同时执行.这是因为我也希望能够在 ConfigureServices
中记录错误.如果我将 loggerFactory.UseApplicationInsights
放在 Configure
中,那么我将无法在 ApplicationInsights 上的 ConfigureServices
中看到错误.我也更喜欢同时查看 Traces 和 Exceptions,这是仅在新包版本中提供的功能.
Even though just doing either of the solutions above solves the problem, I decided to do both. This is because I want to be able to log error in ConfigureServices
as well. Were I to put loggerFactory.UseApplicationInsights
in Configure
, then I would not be able to see the error in ConfigureServices
on ApplicationInsights. I also prefer to see both Traces and Exceptions, a feature that only comes with the new package version.
这篇关于将 Application Insights 与 ILoggerFactory 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!