如何从Azure Bot获取聊天机器人消息日志?已连Application Insights无结果
Azure Bot Framework(Python)日志排查指南
一、Application Insights 常用查询语句
以下是针对Bot场景的Kusto查询,可直接在Application Insights的日志查询页面执行:
1. 查询Bot请求日志
requests | where cloud_RoleName contains "<你的Bot名称>" or operation_Name contains "Bot" | order by timestamp desc | take 20
2. 查询跟踪日志(含自定义日志)
traces | where message contains "EchoBot" or operation_Name contains "Bot" | order by timestamp desc | take 20
3. 查询异常日志
exceptions | where operation_Name contains "Bot" | order by timestamp desc | take 10
4. 关联请求与跟踪日志(排查请求全链路)
requests | where operation_Name contains "Bot" | join kind=inner traces on operation_Id | order by timestamp desc | take 20
二、其他排查方案
1. 使用Log Stream实时查看日志
- 进入Azure Portal的Bot资源,左侧菜单找到日志流,选择应用服务日志流,可实时查看Bot的运行日志(包括Python代码的print输出、框架原生日志)
- 若日志流无输出,需先开启应用服务日志:
- 进入Bot对应的应用服务,左侧菜单监控 > 应用服务日志
- 开启HTTP日志和详细错误日志,设置存储路径后保存,重启应用服务
2. 验证Application Insights配置正确性
- 确认Bot代码中已正确集成SDK:
以OpenTelemetry为例,代码中需添加:from azure.monitor.opentelemetry import configure_azure_monitor configure_azure_monitor(connection_string="<你的Application Insights连接字符串>") - 检查Bot资源的配置页面,确认Application Insights的连接字符串/Instrumentation Key已正确配置,且保存后重启了Bot
3. 检查Bot代码的日志输出逻辑
- 在Echo Bot的消息处理方法中添加明确的日志语句:
import logging logger = logging.getLogger(__name__) async def on_message_activity(self, turn_context: TurnContext): logger.info(f"Received message: {turn_context.activity.text}") await turn_context.send_activity(f"You said: {turn_context.activity.text}") - 确保日志级别设置为
INFO或DEBUG,避免低级别日志被过滤
4. 考虑数据延迟因素
Application Insights的数据通常有5-10分钟的延迟,修改配置或重启Bot后,建议等待一段时间再查询日志
内容的提问来源于stack exchange,提问作者bejandor
相关产品推荐
相关产品推荐

