将应用程序见解与ILoggerFactory一起使用 [英] Using Application Insights with ILoggerFactory

查看:236
本文介绍了将应用程序见解与ILoggerFactory一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将异常记录到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(如已实现

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 NuGets.具体来说, Microsoft.ApplicationInsights.2.2.0Microsoft.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.0Microsoft.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中的IApplicationBuilderIServiceProvider没有提供在AddApplicationInsights中使用的正确服务提供者,因此您需要在ConfigureServices中添加提供者:

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中看到错误.我还希望同时看到跟踪"和异常",这是仅在新软件包版本中附带的功能.

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.

这篇关于将应用程序见解与ILoggerFactory一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆