无法使用 IActivityLogger 和 Bot Builder for C# 记录消息

Unable to log messages using IActivityLogger and the Bot Builder for C#

我需要记录我的用户和我的机器人之间的消息。经过一些研究,我发现 sGambolati 解释了如何实现这一点 post:。

但是这种方法似乎对我不起作用。我什至尝试了他提供的相同片段,但我仍然没有在调试输出中看到日志 window。

下面是他的实现代码:

记录器class:

public class DebugActivityLogger : IActivityLogger
{
    public async Task LogAsync(IActivity activity)
    {
        Debug.WriteLine($"From:{activity.From.Id} - To:{activity.Recipient.Id} - Message:{activity.AsMessageActivity()?.Text}");
    }
}

我的Global.asax:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);

        var builder = new ContainerBuilder();
        builder.RegisterType<DebugActivityLogger>()
            .AsImplementedInterfaces()
            .InstancePerDependency();
        builder.Update(Conversation.Container);
    }
}

控制器中的标准回波样本:

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
    if (activity.Type == ActivityTypes.Message)
    {
        ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
        int length = (activity.Text ?? string.Empty).Length;
        Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
        await connector.Conversations.ReplyToActivityAsync(reply);
    }

    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

我错过了什么吗?

IActivityLogger 实施将记录通过 IPostToBot/IBotToUser 实施的 incoming/outcoming 消息。在这种情况下,LogPostToBot and the LogPostToUser 实现负责调用 IActivityLogger 实现。

所有这些介绍,只是为了让您知道,如果您使用 connector.Conversations.ReplyToActivityAsync,您将不会碰到 IActivityLogger。要点击记录器,您需要一个 IDialog 来接收来自用户的消息,并且您需要使用 context.PostAsync 将机器人发送给用户的消息记录下来。

查看 core-Middleware C# 示例以查看此工作原理。