Sitelet https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/854
Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

Enable ApplicationInsightsLoggerProvider by default #854

Description

Currently enabling ApplicationInsightsLoggerProvider is an additional step on top on enabling ApplicationInsights.
i.e users have to

  1. Install Additional Nuget Package - Microsoft.Extensions.Logging.ApplicationInsights.
  2. Enable ApplicationInsightsLoggerProvider by calling AddApplicationInsights() extension method on LoggingBuilder.

Proposing to change this behaviour, to automatically enable ApplicationInsightsLoggerProvider, when ApplicationInsights collection is enabled with either services.AddApplicationInsightsTelemetry() or IWebHostBuilder.UseApplicationInsights() methods.

With this change, users no longer have to add additional nuget or do any change to enable ILogger provider for ApplicationInsights.

Notes:

  1. By default, only "Warning" or above LogLevel logs are sent to ApplicationInsightsProvider for all categories. But this can be easily configured by user either in code or using appsettings.json.

For eg:

 // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddApplicationInsightsTelemetry();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddLogging((logging) =>
            {
                // For categories beginning with Microsoft, sent only Error or above
                // logging.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Error);
                // For every other categories, sent only Warning or above
                //logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Warning);
            });            
        }

or in appsettings.json

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Warning",
        "Microsoft" : "Error" 
      }
    },    
    "LogLevel": {
      "Default": "Debug"
    }
  }
  1. This new feature will be available only for NETSTANDARD2.0 or above, as the dependent package Microsoft.Extensions.Logging.ApplicationInsights is only from NETSTANDARD2.0 or above.
    If you need to send ILogger logs to applications from an older apps, please use the AddApplicationInsights extension method on ILoggerFactory as shown here. Please note that this is now a deprecated way.

If old provider was enabled with now obsolete extension method AddApplicationInsights of ILoggerFactory, please remove it - otherwise it'll result in double logging.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions