无法使用IActivityLogger和Bot Builder for C#记录消息 [英] Unable to log messages using IActivityLogger and the Bot Builder for C#
问题描述
我需要记录我的用户和我的机器人之间的消息.经过一番研究,我发现sGambolati的这篇文章解释了如何实现此目的: https://stackoverflow.com/a/42145416 .
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:
记录器类:
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:
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;
}
我错过了什么吗?
推荐答案
IActivityLogger
实现将记录通过IPostToBot
/IBotToUser
实现的传入/传出消息.在这种情况下, LogPostToBot 和
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.
所有这些介绍只是为了让您知道,如果您使用connector.Conversations.ReplyToActivityAsync
,您将不会点击IActivityLogger
.要命中记录器,您将需要一个IDialog
来接收来自用户的消息,并且需要使用context.PostAsync
将来自机器人的消息记录到用户.
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.
看看 core-Middleware C#示例以了解其工作原理.
Take a look to the core-Middleware C# sample to see this working.
这篇关于无法使用IActivityLogger和Bot Builder for C#记录消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!