azure-cosmosdb - CosmosDB 是否有一种固有的方式来记录 CRUD 请求?

标签 azure-cosmosdb audit-trail

我试图找出 CosmosDB 是否有任何可用作审计日志的内置功能,即现有的 Azure 诊断/监控功能之一记录了对数据库的特定更改,包括写入的 JSON 数据.

这是否存在,还是我必须自己编写?

到目前为止,我发现最接近的是诊断日志记录,其中包含很多信息,但实际写入、更改或删除的内容。

最佳答案

基于官方doc ,您提到的 azure cosmos db 中的 Diagnostic logging 可以记录 DataPlaneRequestsMongoRequestsMetric Requests。但是,有没有这样的信息表示对数据库、集合、文档等的具体更改。

所以,我建议你深入研究 change feed在 azure cosmos db 中。它内置在 Azure Cosmos DB 中,使我们能够捕获对我们的集合所做的所有更改,而无需考虑是哪个系统进行了更改。您可以通过三种不同的方式阅读更改提要:

1.使用 Azure Functions

请在 Azure Functions 应用程序中创建 Azure Cosmos DB 触发器,选择要连接的 Azure Cosmos DB 集合,只要对集合进行更改,就会触发该函数。

enter image description here

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/

相关文章:

c# - 如何(我应该)为 DocumentDb 单元测试模拟 DocumentClient?

mongodb - Azure 门户中 Cosmos DB 的帐户 key 在哪里

asp.net - Informix - 在删除触发器中捕获用户 ID

Mysql - 选择到 XML

entity-framework - 使用 Attach 方法时 EF 6 OriginalValues 丢失

node.js - 尝试从 DocumentDb 删除文档时出现错误请求错误

c# - 如何使用 ITranscriptLogger 和 TranscriptLoggerMiddleware 在 cosmos DB 中存储聊天记录

linq - Azure DocumentDb 中数组的复杂 WHERE 子句

mysql - 结合使用 mongoDb 和 MySQL

sql - 只保留审计表中每个对象的最后 5 行