如何在 botframework C# 的 DebugActivityLogger 中获取 cosmosDB 或 storageAccount 连接
How to get cosmosDB or storageAccount connection in DebugActivityLogger of botframework C#
我想将我的对话历史记录保存到 Azure Cosmos DB 或 storageAccount。
- 关于 Azure Cosmos DB
好像真的很好用IActivityLogger。但我不知道如何获得 Global.asax.cs 中连接的数据库连接。或者最好在IActivityLogger?
中重新获取
- 关于存储帐户
根据this,SDK还提供了一种使用storageAccount保存对话历史记录的方法。但是 Activity 已经被压缩了。我想将未压缩的真实对话[JSON]保存到storageAccount。
我正在尝试向具有压缩数据的 Azure table 存储插入一行。
但是 Activity0 始终为空。
public class ActivityEntity : TableEntity
{
/// <summary>
/// Empty constructor.
/// </summary>
public ActivityEntity()
{ }
/// <summary>
/// Construct from an IActivity.
/// </summary>
/// <param name="activity"></param>
public ActivityEntity(byte[] Activity)
{
PartitionKey = "111";
RowKey = "11";
From = "111";
Recipient = "111";
Activity0 = Activity;
Version = 3.0;
}
/// <summary>
/// Version number for the underlying activity.
/// </summary>
public double Version { get; set; }
/// <summary>
/// Channel identifier for sender.
/// </summary>
public string From { get; set; }
/// <summary>
/// Channel identifier for receiver.
/// </summary>
public string Recipient { get; set; }
/// <summary>
/// Logged activity.
/// </summary>
[IgnoreProperty]
public byte[] Activity0 { get; set; }
/// <summary>
/// Generate a partition key given <paramref name="channelId"/> and <paramref name="conversationId"/>.
/// </summary>
/// <param name="channelId">Channel where activity happened.</param>
/// <param name="conversationId">Conversation where activity happened.</param>
/// <returns>Partition key.</returns>
public static string GeneratePartitionKey(string channelId, string conversationId)
{
return $"{channelId}|{conversationId}";
}
/// <summary>
/// Generate row key for ascending <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">Timestamp of activity.</param>
/// <returns></returns>
public static string GenerateRowKey(DateTime timestamp)
{
return $"{timestamp.Ticks:D19}";
}
}
class Program
{
public static object CloudConfigurationManager { get; private set; }
static void Main(string[] args)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=storagetesthuafu;AccountKey=iQ0EZexm3wbpWZqly2HtVH0/CZKRyMY9l2b0g20AQkUz7BX0BFLuBinMyYLe8Ow/zOA7vJqAMSxSHllT3JTL2g==;EndpointSuffix=core.windows.net");
// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
// Create the CloudTable object that represents the "TemperatureData" table.
CloudTable table = tableClient.GetTableReference("messagelog");
TableQuery<BotDataRow> query = new TableQuery<BotDataRow>();
var data = table.ExecuteQuery(query);
var dataarry = data.ToArray();
var aa = dataarry.First();
var activity = aa.Activity0;
var after = Decompress(activity);
CloudTable tableTEST = tableClient.GetTableReference("messagelog");
byte[] bb = Encoding.UTF8.GetBytes(after);
ActivityEntity customer4 = new ActivityEntity(bb);
// Create the InsertOrReplace TableOperation.
TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(customer4);
// added to the table.
table.Execute(insertOrReplaceOperation);
}
创建一个使用 DocumentDb 的 IActivityLogger 实现不需要太多代码。下面是一个使用 Azure Cosmos DB 模拟器:
的示例
class DocumentDbActivityLogger : IActivityLogger
{
const string DbKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
const string DbUri = "https://localhost:8081";
const string DbId = "ActivityLogsDb";
const string CollectionId = "ActivityLogsColleciton";
async Task IActivityLogger.LogAsync(IActivity activity)
{
try
{
var message = activity.AsMessageActivity();
if (message != null)
{
using (var documentClient = new DocumentClient(new Uri(DbUri), DbKey))
{
await documentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DbId, CollectionId), message);
}
}
}
catch (Exception e)
{
System.Diagnostics.Debug.Print(e.ToString());
}
}
}
然后在Global.asax.cs中注册:
Conversation.UpdateContainer(builder =>
{
builder.RegisterType<DocumentDbActivityLogger>().AsImplementedInterfaces().InstancePerDependency();
}
我想将我的对话历史记录保存到 Azure Cosmos DB 或 storageAccount。
- 关于 Azure Cosmos DB
好像真的很好用IActivityLogger。但我不知道如何获得 Global.asax.cs 中连接的数据库连接。或者最好在IActivityLogger?
中重新获取- 关于存储帐户
根据this,SDK还提供了一种使用storageAccount保存对话历史记录的方法。但是 Activity 已经被压缩了。我想将未压缩的真实对话[JSON]保存到storageAccount。
我正在尝试向具有压缩数据的 Azure table 存储插入一行。 但是 Activity0 始终为空。
public class ActivityEntity : TableEntity
{
/// <summary>
/// Empty constructor.
/// </summary>
public ActivityEntity()
{ }
/// <summary>
/// Construct from an IActivity.
/// </summary>
/// <param name="activity"></param>
public ActivityEntity(byte[] Activity)
{
PartitionKey = "111";
RowKey = "11";
From = "111";
Recipient = "111";
Activity0 = Activity;
Version = 3.0;
}
/// <summary>
/// Version number for the underlying activity.
/// </summary>
public double Version { get; set; }
/// <summary>
/// Channel identifier for sender.
/// </summary>
public string From { get; set; }
/// <summary>
/// Channel identifier for receiver.
/// </summary>
public string Recipient { get; set; }
/// <summary>
/// Logged activity.
/// </summary>
[IgnoreProperty]
public byte[] Activity0 { get; set; }
/// <summary>
/// Generate a partition key given <paramref name="channelId"/> and <paramref name="conversationId"/>.
/// </summary>
/// <param name="channelId">Channel where activity happened.</param>
/// <param name="conversationId">Conversation where activity happened.</param>
/// <returns>Partition key.</returns>
public static string GeneratePartitionKey(string channelId, string conversationId)
{
return $"{channelId}|{conversationId}";
}
/// <summary>
/// Generate row key for ascending <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">Timestamp of activity.</param>
/// <returns></returns>
public static string GenerateRowKey(DateTime timestamp)
{
return $"{timestamp.Ticks:D19}";
}
}
class Program
{
public static object CloudConfigurationManager { get; private set; }
static void Main(string[] args)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=storagetesthuafu;AccountKey=iQ0EZexm3wbpWZqly2HtVH0/CZKRyMY9l2b0g20AQkUz7BX0BFLuBinMyYLe8Ow/zOA7vJqAMSxSHllT3JTL2g==;EndpointSuffix=core.windows.net");
// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
// Create the CloudTable object that represents the "TemperatureData" table.
CloudTable table = tableClient.GetTableReference("messagelog");
TableQuery<BotDataRow> query = new TableQuery<BotDataRow>();
var data = table.ExecuteQuery(query);
var dataarry = data.ToArray();
var aa = dataarry.First();
var activity = aa.Activity0;
var after = Decompress(activity);
CloudTable tableTEST = tableClient.GetTableReference("messagelog");
byte[] bb = Encoding.UTF8.GetBytes(after);
ActivityEntity customer4 = new ActivityEntity(bb);
// Create the InsertOrReplace TableOperation.
TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(customer4);
// added to the table.
table.Execute(insertOrReplaceOperation);
}
创建一个使用 DocumentDb 的 IActivityLogger 实现不需要太多代码。下面是一个使用 Azure Cosmos DB 模拟器:
的示例class DocumentDbActivityLogger : IActivityLogger
{
const string DbKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
const string DbUri = "https://localhost:8081";
const string DbId = "ActivityLogsDb";
const string CollectionId = "ActivityLogsColleciton";
async Task IActivityLogger.LogAsync(IActivity activity)
{
try
{
var message = activity.AsMessageActivity();
if (message != null)
{
using (var documentClient = new DocumentClient(new Uri(DbUri), DbKey))
{
await documentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DbId, CollectionId), message);
}
}
}
catch (Exception e)
{
System.Diagnostics.Debug.Print(e.ToString());
}
}
}
然后在Global.asax.cs中注册:
Conversation.UpdateContainer(builder =>
{
builder.RegisterType<DocumentDbActivityLogger>().AsImplementedInterfaces().InstancePerDependency();
}