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

混江龙づ霸主 提交于 2019-12-11 07:18:02

问题


I need to log the messages between my users and my bot. After some research I found this post by sGambolati explaining how this can be achieved: https://stackoverflow.com/a/42145416.

However this approach doesn't seems to be working for me. I even tried the same snippets he provided and I still don't see the log in the debug output window.

Here's the code according to his implementation:

The logger 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}");
    }
}

My 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);
    }
}

The standard echo sample in the controller:

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;
}

Am I missing something?


回答1:


An IActivityLogger implementation will log incoming/outcoming messages going through an IPostToBot/IBotToUser implementation. In this case, the LogPostToBot and the LogPostToUser implementations are the responsible of calling the IActivityLogger implementation.

All this introduction, is just to let you know that if you use the connector.Conversations.ReplyToActivityAsync you won't be hitting the IActivityLogger. To hit the logger, you will need a IDialog that received the message from the user and you need to use context.PostAsync to log the messages from the bot to the user.

Take a look to the core-Middleware C# sample to see this working.



来源:https://stackoverflow.com/questions/43347912/unable-to-log-messages-using-iactivitylogger-and-the-bot-builder-for-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!