adamasan
adamasan

Reputation: 1202

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

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?

Upvotes: 1

Views: 493

Answers (1)

Ezequiel Jadib
Ezequiel Jadib

Reputation: 14787

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.

Upvotes: 2

Related Questions