.NET Framework API部署至Azure App Service后Serilog无法写入App Insights
针对你遇到的.NET Framework API部署到Azure App Service后,Serilog日志无法传入Application Insights的问题,给你几个排查和解决方向:
检查App Service与App Insights的关联配置
不要只依赖代码里的Instrumentation Key,登录Azure门户查看你的App Service:- 进入App Service的「Application Insights」配置页,确认已关联正确的App Insights资源
- 检查应用设置里的
APPINSIGHTS_INSTRUMENTATIONKEY,确保和你代码中使用的一致,该配置会覆盖代码里硬编码的Key,若存在不一致会导致日志发送失败
调整Serilog的Telemetry转换器
你当前使用的TraceTelemetryConverter生成的Trace类型日志,在Azure环境中可能被默认过滤。试试换成EventTelemetryConverter,修改配置代码:var logConfig = new LoggerConfiguration() .ReadFrom.AppSettings() .WriteTo.ApplicationInsights(instrumentationKey, new EventTelemetryConverter()); Log.Logger = logConfig.CreateLogger();或者针对错误日志使用
ExceptionTelemetryConverter,确保日志能被App Insights正确识别。确认日志级别配置
检查AppSettings里的Serilog配置,是否设置了过高的最低日志级别,导致Error级别的日志被过滤。可以在代码中显式指定最低级别:var logConfig = new LoggerConfiguration() .MinimumLevel.Error() .ReadFrom.AppSettings() .WriteTo.ApplicationInsights(instrumentationKey, new EventTelemetryConverter()); Log.Logger = logConfig.CreateLogger();验证基础连接是否正常
跳过Serilog,直接用TelemetryClient发送测试日志,排查是Serilog配置问题还是App Service到App Insights的连接问题:var telemetryClient = new Microsoft.ApplicationInsights.TelemetryClient { InstrumentationKey = instrumentationKey }; telemetryClient.TrackEvent("Direct Test Event from TelemetryClient"); telemetryClient.Flush();如果这条日志能出现在App Insights中,说明问题出在Serilog的配置或集成上;如果不能,检查App Service的网络权限、App Insights的采样规则。
排查日志传输错误
开启App Service的日志流(Azure门户→App Service→日志→日志流),查看是否有Serilog或Application Insights相关的报错,比如权限不足、网络连接失败等信息,这些能直接定位问题原因。检查必需的NuGet包
确保你的项目已安装Microsoft.ApplicationInsights.Web包,这是.NET Framework Web应用集成App Insights的核心依赖,缺少该包可能导致日志无法正常传输。
内容的提问来源于stack exchange,提问作者Steve

