You need to enable JavaScript to run this app. 如何将Microsoft Bot Framework V3用户与机器人日志迁移至V4 C# Core Bot
优惠活动
大模型
产品
解决方案
定价
更多

如何将Microsoft Bot Framework V3用户与机器人日志迁移至V4 C# Core Bot

Microsoft Bot Framework C# SDK V4 交互日志迁移实现方案

核心实现逻辑

Bot Framework V4 取消了V3版本内置的默认活动日志存储能力,改为通过中间件管道全局拦截所有出入站活动的方式实现日志采集,无需修改业务逻辑即可完成日志功能的迁移,灵活性高于V3的实现方式。

具体实现步骤

  • 第一步:实现自定义日志中间件
    实现IMiddleware接口,在OnTurnAsync方法中分别拦截入站用户活动、出站机器人回复活动,复用V3版本已有的日志存储逻辑即可。参考代码如下:
    public class ChatLogMiddleware : IMiddleware
    {
        public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default)
        {
            // 记录入站用户活动
            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                // 此处可直接复用V3版本的日志实体结构、存储逻辑
                var logEntity = new ChatLogEntity
                {
                    ActivityId = turnContext.Activity.Id,
                    SenderId = turnContext.Activity.From.Id,
                    SenderName = turnContext.Activity.From.Name,
                    ConversationId = turnContext.Activity.Conversation.Id,
                    Content = turnContext.Activity.Text,
                    Timestamp = turnContext.Activity.Timestamp,
                    Direction = "Inbound"
                };
                // 调用V3版本已有存储方法即可
                ChatLogStorage.Save(logEntity);
            }
    
            // 注册出站活动拦截钩子,捕获机器人所有回复内容
            turnContext.OnSendActivities(async (ctx, activities, nextSend) =>
            {
                var responses = await nextSend();
                foreach (var activity in activities.Where(a => a.Type == ActivityTypes.Message))
                {
                    var logEntity = new ChatLogEntity
                    {
                        ActivityId = activity.Id,
                        SenderId = activity.From.Id,
                        SenderName = activity.From.Name,
                        ConversationId = activity.Conversation.Id,
                        Content = activity.Text,
                        Timestamp = activity.Timestamp,
                        Direction = "Outbound"
                    };
                    ChatLogStorage.Save(logEntity);
                }
                return responses;
            });
    
            // 执行管道后续中间件、业务逻辑
            await next(cancellationToken);
        }
    }
    
  • 第二步:全局注入中间件
    在服务配置代码中注册实现的日志中间件,确保中间件优先级最高,可拦截所有活动请求:
    .NET 6之前版本(Startup.cs)参考代码:
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers().AddNewtonsoftJson();
        services.AddBot<CoreBot>(options =>
        {
            options.CredentialProvider = new ConfigurationCredentialProvider(Configuration);
            // 注入日志中间件
            options.Middleware.Add(new ChatLogMiddleware());
        });
    }
    
    .NET 6+版本(Program.cs)参考代码:
    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddControllers().AddNewtonsoftJson();
    builder.Services.AddBot<CoreBot>(options =>
    {
        options.CredentialProvider = new ConfigurationCredentialProvider(builder.Configuration);
        options.Middleware.Add(new ChatLogMiddleware());
    });
    
    var app = builder.Build();
    app.UseRouting();
    app.UseAuthorization();
    app.MapControllers();
    app.Run();
    

注意事项

  • 如需记录消息之外的其他活动类型(成员加入、事件触发、技能调用等),移除代码中Activity.Type == ActivityTypes.Message的判断即可捕获所有类型的活动。
  • 如需完全对齐V3版本的日志存储结构,可直接序列化整个Activity对象存入存储介质,无需单独拼接字段,与V3的存储格式完全兼容。
  • 无需自定义日志格式的场景,可直接使用官方提供的TranscriptLoggerMiddleware搭配BlobTranscriptStore、CosmosDbTranscriptStore等官方存储实现,无需自行编写中间件逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 09:06:04