CosmosDB是否有固有的方式来记录CRUD请求?

时间:2018-06-21 00:44:14

标签: azure-cosmosdb audit-trail

我正在尝试查找CosmosDB是否具有任何可用作审核日志的内置功能,即现有的Azure诊断/监视功能之一注销对数据库的特定更改,包括写入的JSON数据。

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

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

1 个答案:

答案 0 :(得分:1)

基于您提到的蔚蓝宇宙数据库中的官方docDiagnostic logging,可以登录DataPlaneRequestsMongoRequestsMetric Requests。但是,没有这些信息表示对数据库,集合,文档等的特定更改。

因此,我建议您在Azure Cosmos数据库中更深入地研究change feed。它内置在Azure Cosmos DB中,使我们能够捕获对集合所做的所有更改,而无需考虑什么系统已完成更改。您可以通过三种不同的方式阅读更改摘要:

1。使用Azure函数

请在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

希望它对您有帮助。