weixin_39823299
weixin_39823299
2020-12-02 10:02

How to register ApplicationInsights ILoggingProvider in the ILoggingBuilder?

Navigating through outdated documentation, it seems that the latest way to configure logging on .net core is similar to that:


host = new WebHostBuilder()
    .UseKestrel()
    .UseIISIntegration()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseConfiguration(conf)
    .UseApplicationInsights()
    .ConfigureLogging(builder =>
    {
        builder.AddFilter("Microsoft", LogLevel.Error);
        builder.AddProvider(new CustomConsoleLogProvider());
    })
    .UseStartup<startup>()
    .Build();
</startup>

However, it seems that this is not possible with ApplicationInsights which requires a ILoggerFactory instead of the ILoggingBuilder.

I then added


loggerFactory.AddApplicationInsights(prov, LogLevel.Information);

In the Configure of my Startup.cs. This seems to work fine but is it not outdated ?

该提问来源于开源项目:microsoft/ApplicationInsights-aspnetcore

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

10条回答

  • weixin_39568597 weixin_39568597 5月前

    I just migrated a project over to 2.0 and am having the same thing. It looks like there is no extension method on ILoggingBuilder. Would love to have it 😄

    点赞 评论 复制链接分享
  • weixin_39636608 weixin_39636608 5月前

    +1. All new ASP.NET Core documentation recommends registering logging providers via ILoggingBuilder in program.cs (as opposed to in startup.cs). It would be pretty straight-forward to work around not having an extension method except that ApplicationInsightsLoggingProvider is internal, so it can't be added directly.

    点赞 评论 复制链接分享
  • weixin_39685459 weixin_39685459 5月前

    Hi , Does this still look like it'll make 2.6.0?

    Looking forward to being able to send logging from ConfigureServices to App Insights. I have been bit by some errant DI registrations lately and being able to send App Insights logs from ConfigureServices would be a big help.

    Thanks!

    点赞 评论 复制链接分享
  • weixin_39966053 weixin_39966053 5月前

    Its less likely to land in 2.6.0.

    点赞 评论 复制链接分享
  • weixin_39966053 weixin_39966053 5月前

    This is going to addressed by the following PR in progress now.. https://github.com/Microsoft/ApplicationInsights-dotnet-logging/pull/239

    点赞 评论 复制链接分享
  • weixin_39966053 weixin_39966053 5月前

    Closing this as this is solved by using https://www.nuget.org/packages/Microsoft.Extensions.Logging.ApplicationInsights.

    点赞 评论 复制链接分享
  • weixin_39581945 weixin_39581945 5月前

    The extension method AddApplicationInsights() on ILoggerFactory had a min log level parameter which I was using but the extension method on ILoggingBuilder does not. Or to be more precise the ApplicationInsightsLoggerOptions do not have this option.

    How to work around this?

    点赞 评论 复制链接分享
  • weixin_39966053 weixin_39966053 5月前

    LogLevel based filtering in part of logging infra itself.

    //configure LogLevel Trace or above is sent to ApplicationInsights for all categories. loggingBuilder.AddFilter("", LogLevel.Trace); loggingBuilder.AddApplicationInsights("--YourAIKeyHere--");

    See this doc: https://github.com/Microsoft/ApplicationInsights-dotnet-logging/blob/develop/src/ILogger/Readme.md

    点赞 评论 复制链接分享
  • weixin_39581945 weixin_39581945 5月前

    I saw this one coming.

    So a feature has been removed with this "new design"... I have different logging providers, why shouldn't I be able to granular control their behavior regarding for example their log level thresholds?

    Oh, I am sorry.

    This looks promising:

    logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);

    点赞 评论 复制链接分享
  • weixin_39966053 weixin_39966053 5月前

    Ability to control/filter per logging provider is offered by the logging infra itself, not application insights.

    点赞 评论 复制链接分享

相关推荐