microsoft / ApplicationInsights-dotnet-logging

.NET Logging adaptors
106 stars 49 forks source link

ILogger: filters in appsettings.json does not work? #270

Closed qrli closed 5 years ago

qrli commented 5 years ago

No matter how I change "Logging" settings in appsettings.json, it has no effect. Other logger providers work as expected though. It seems only adding the following code works.

logging.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Warning);
logging.AddFilter<ApplicationInsightsLoggerProvider>("System", LogLevel.Warning);
RamjotSingh commented 5 years ago

Can you share the full registration code?

qrli commented 5 years ago
            var builder = WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build())
                .UseShutdownTimeout(TimeSpan.FromSeconds(30))
                .UseApplicationInsights()
                .ConfigureLogging((hostingContext, logging) =>
                {
                    logging.AddApplicationInsights();
                    logging.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Warning);
                    logging.AddFilter<ApplicationInsightsLoggerProvider>("System", LogLevel.Warning);
                })

.NET Core SDK version: 2.1.403

qrli commented 5 years ago

Ah I found the cause. My logging settings are from the ASP.NET Core 1.x days, while 2.0 slightly changed the structure. After fixing that, it works fine.

Another issue misled me is that Visual Studio Appliation Insights panel seems to always show more telemetry, also duplicated telemetry, than it actually sends when running outside.

You can close this. Sorry and thanks for your time. :-)

cijothomas commented 5 years ago

@qrli Debugging from Visual Studio does extra injection - you can set ASPNETCORE_PREVENTHOSTINGSTARTUP = true to disable this. Or if you are in one of the newer VS versions, you can disable this in setting also.