Using Application Insights with ILoggerFactory

后端 未结 5 1945
攒了一身酷
攒了一身酷 2020-12-31 08:44

I\'m trying to log exceptions to Application Insights. I succeeded in doing this by calling TelemetryClient.TrackException directly. However, I would like to ab

相关标签:
5条回答
  • 2020-12-31 08:57

    Here is the documentation of App Insights with ILogger. For me it worked however got tons of Trace log so recommend you to apply custom filters:

    builder.AddApplicationInsights("ikey");
    
    builder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>
                ("", LogLevel.Information);
    builder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>
                ("Microsoft", LogLevel.Warning);
    
    0 讨论(0)
  • 2020-12-31 09:10

    Unfortunately the SDK was updated to fire Exception Telemetry only recently (see commit), and the change was not published yet.
    The only routes I see right now is to either leave the explicit call to TelemetryClient.TrackException in your code, or wrap it all with your own implementation of ILogger - both as temporary solutions until the official SDK support this

    0 讨论(0)
  • 2020-12-31 09:13

    There is now first-class support for logging to Application Insights, using the Microsoft.Extensions.Logging.ApplicationInsights provider.

    0 讨论(0)
  • 2020-12-31 09:18

    According to your description, I suggest you could try below codes to enable ILogger log the error to the ApplicationInsights.

    You could directly use loggerFactory.AddApplicationInsights() method to enable the ApplicationInsights ILogger.

    More details, you could refer to below codes:

    The startup class:

    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }
    
        public IConfigurationRoot Configuration { get; }
    
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddApplicationInsightsTelemetry(Configuration);
            // Add framework services.
            services.AddMvc();
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
    
            loggerFactory.AddApplicationInsights(app.ApplicationServices,LogLevel.Warning);
    
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
    
            app.UseStaticFiles();
    
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
    

    The appsettings.json:

    {
    
      "ApplicationInsights": {
        "InstrumentationKey": "yourkey"
      }
    }
    

    Result:


    Update:

    The record find in the search feature.

    0 讨论(0)
  • 2020-12-31 09:19

    I finally figured out. There are two ways to solve my issue:

    Easy way:

    I was using older versions of the Application Insights NuGets. Specifically, Microsoft.ApplicationInsights.2.2.0 and Microsoft.ApplicationInsights.AspNetCore.2.0.0.

    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.

    Slightly more difficult way:

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

    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.

    What I ended up doing (probably the best solution for now in my opinion):

    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.

    0 讨论(0)
提交回复
热议问题