Azure Service Fabric应用日志未显示在Application Insights中求助
你遇到的核心问题是:Visual Studio模板生成的ServiceEventSource是通过ETW(Event Tracing for Windows)输出日志的,而Application Insights默认不会自动捕获自定义ETW事件——这就是为什么事件查看器能看到日志,但AI收不到的原因。其他平台事件能正常接收,说明你的AI基础配置(Instrumentation Key、集群网络)是没问题的,只需要添加对自定义ETW事件的收集配置即可。
下面是具体的修复步骤:
1. 安装必要的NuGet包
首先,在你的Service Fabric服务项目中安装Microsoft.ApplicationInsights.EventSourceListener包,这个包专门用于将ETW事件转发到Application Insights:
Install-Package Microsoft.ApplicationInsights.EventSourceListener
或者直接在NuGet包管理器中搜索并安装该包。
2. 修改ApplicationInsights.config,添加ETW事件收集模块
打开你的ApplicationInsights.config,在<TelemetryModules>节点下添加EventSourceTelemetryModule的配置,指定要收集的ServiceEventSource名称:
首先,找到你的ServiceEventSource类上方的EventSource特性名称(默认模板生成的格式类似[EventSource(Name = "YourAssemblyName-ServiceEventSource")],比如你的项目是ConfigUploadService,名称大概率是ConfigUploadService-ServiceEventSource)。
然后添加以下配置:
<TelemetryModules> <!-- 保留你已有的模块 --> <Add Type="Microsoft.ApplicationInsights.ServiceFabric.Module.ServiceRemotingRequestTrackingTelemetryModule, Microsoft.AI.ServiceFabric.Native"/> <Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryModule, Microsoft.AI.PerfCounterCollector"/> <!-- 新增的ETW事件收集模块 --> <Add Type="Microsoft.ApplicationInsights.EventSourceListener.EventSourceTelemetryModule, Microsoft.ApplicationInsights.EventSourceListener"> <Sources> <!-- 替换成你的ServiceEventSource的Name属性值 --> <Add Name="ConfigUploadService-ServiceEventSource" Level="Information"/> <!-- 可选:如果需要收集系统级Service Fabric ETW事件,可以添加 --> <!-- <Add Name="Microsoft-ServiceFabric-Services" Level="Information"/> --> </Sources> </Add> </TelemetryModules>
关键说明:
Name字段必须和你的ServiceEventSource类上EventSource特性的Name值完全一致,否则无法捕获事件。Level字段设置你要收集的日志级别,比如Information对应你示例中的ServiceMessage事件(模板中ServiceMessage的EventLevel是Information),如果需要更详细的日志可以设为Verbose。
3. 验证配置并重启服务
完成配置后,重新部署你的Service Fabric服务,触发日志输出(比如调用ServiceEventSource.Current.ServiceMessage),等待几分钟后查看Application Insights的“日志”界面,通过以下Kusto查询就能看到对应的日志:
traces | where message contains "Config deployment is disabled."
额外检查点
如果还是没收到日志,可以做以下检查:
- 确认
ServiceEventSource的EventSource名称没有拼写错误。 - 本地调试时确保以管理员身份运行Visual Studio(需要权限读取ETW事件)。
- 查看Application Insights的“失败的遥测”界面,是否有发送失败的记录。
内容的提问来源于stack exchange,提问作者ashwini verma

