我试图找出 CosmosDB 是否有任何可用作审计日志的内置功能,即现有的 Azure 诊断/监控功能之一记录了对数据库的特定更改,包括写入的 JSON 数据.
这是否存在,还是我必须自己编写?
到目前为止,我发现最接近的是诊断日志记录,其中包含很多信息,但不实际写入、更改或删除的内容。
最佳答案
基于官方doc ,您提到的 azure cosmos db 中的 Diagnostic logging
可以记录 DataPlaneRequests
、MongoRequests
、Metric Requests
。但是,有没有这样的信息表示对数据库、集合、文档等的具体更改。
所以,我建议你深入研究 change feed在 azure cosmos db 中。它内置在 Azure Cosmos DB 中,使我们能够捕获对我们的集合所做的所有更改,而无需考虑是哪个系统进行了更改。您可以通过三种不同的方式阅读更改提要:
1.使用 Azure Functions
请在 Azure Functions 应用程序中创建 Azure Cosmos DB 触发器,选择要连接的 Azure Cosmos DB 集合,只要对集合进行更改,就会触发该函数。
2.使用 Azure Cosmos DB SDK
foreach (PartitionKeyRange pkRange in partitionKeyRanges){
string continuation = null;
checkpoints.TryGetValue(pkRange.Id, out continuation);
IDocumentQuery<Document> query = client.CreateDocumentChangeFeedQuery(
collectionUri,
new ChangeFeedOptions
{
PartitionKeyRangeId = pkRange.Id,
StartFromBeginning = true,
RequestContinuation = continuation,
MaxItemCount = -1,
// Set reading time: only show change feed results modified since StartTime
StartTime = DateTime.Now - TimeSpan.FromSeconds(30)
});
while (query.HasMoreResults)
{
FeedResponse<dynamic> readChangesResponse = query.ExecuteNextAsync<dynamic>().Result;
foreach (dynamic changedDocument in readChangesResponse)
{
Console.WriteLine("document: {0}", changedDocument);
}
checkpoints[pkRange.Id] = readChangesResponse.ResponseContinuation;
}
}
3. Using the Azure Cosmos DB change feed processor library
希望对你有帮助。
关于azure-cosmosdb - CosmosDB 是否有一种固有的方式来记录 CRUD 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50958867/