You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework中同时使用ILogger与Application Insights的问题

.NET Framework 4.8中整合ILogger与Application Insights的问题

我之前查过类似问题但没找到有效解决办法。想在保留Visual Studio自动添加的Application Insights功能的同时,用ILogger记录自定义用户日志。这在.NET Core里很容易实现(只需在Startup中添加ILogger配置),但在.NET Framework里卡壳了,可能是我的实现方式有误?

想知道有没有人成功实现过这个场景,还是我应该直接改用TelemetryClient?

我的两个ASP.NET Framework 4.8 API(部署在Azure App Service)

  • App#1:此前通过Visual Studio自动添加了Application Insights,系统遥测日志功能正常,但添加ILogger配置后,自定义用户日志完全无法被记录
  • App#2:仅配置了ILogger,自定义日志可正常记录,但缺少Application Insights的自动日志与遥测功能(因为App#1的问题还没尝试添加Application Insights)

依赖注入配置代码

两个项目均使用Autofac进行依赖注入,且通过扩展支持Microsoft.Extensions.DependencyInjection,配置代码如下:

var builder = new ContainerBuilder();

IServiceCollection services = new ServiceCollection();
services.AddLogging(options =>
{
    options.AddApplicationInsights(configureTelemetryConfiguration: (c) => c.ConnectionString = ConfigurationManager.AppSettings["APPLICATIONINSIGHTS_CONNECTION_STRING"], 
        configureApplicationInsightsLoggerOptions: (o) =>{});
});

builder.Populate(services);

这段配置在App#2中运行正常,所有自定义日志均可被记录。

App#1已安装的NuGet包

App#1安装了以下NuGet包,且applicationInsights.config中有大量配置,系统遥测日志正常,但完全忽略所有自定义日志:

<package id="Microsoft.ApplicationInsights" version="2.22.0" targetFramework="net48" />
<package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.4.0" targetFramework="net48" />
<package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.22.0" targetFramework="net48" />
<package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.22.0" targetFramework="net48" />
<package id="Microsoft.ApplicationInsights.TraceListener" version="2.22.0" targetFramework="net48" />
<package id="Microsoft.ApplicationInsights.Web" version="2.22.0" targetFramework="net48" />
<package id="Microsoft.ApplicationInsights.WindowsServer" version="2.22.0" targetFramework="net48" />
<package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.22.0" targetFramework="net48" />

我已经正确配置了日志过滤规则,排除了过滤导致自定义日志丢失的可能,甚至尝试设置Logging:LogLevel:default = Trace,问题依然存在。请问有什么解决思路?

内容的提问来源于stack exchange,提问作者punkologist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 01:35:30