DocumentDB 中查询的 RU 费用

RU Charge for Queries in DocumentDB

对于 .NET 库,我如何确定查询的 RU 费用。它 return 是 IQueryable,我不确定如何记录它。如何记录所有请求的 RU 的奖励积分。

简单代码,但 return RU 没有:

 var docs = DocumentDBRepository<CampaignMessage>.client.
         CreateDocumentQuery<CampaignMessage>(UriFactoryExtensions.CreateCollectionUri(), new FeedOptions() { MaxItemCount = -1, MaxDegreeOfParallelism = 5 }).Where(x => x.BlastKey == "abc-796").

With the .NET library how do I determine the RU charge for a query. It returns IQueryable and I'm not sure how to log that.

由于Mikhail提供的link,您需要调用docs.AsDocumentQuery().ExecuteNextAsync从DocumentDB服务中检索结果,您可以从FeedResponse<T>.RequestCharge中获取查询的RU费用。

对于记录所有请求的RU,我在使用.NET客户端SDK记录操作日志时检查了客户端日志,但只有错误操作有响应头的日志。我假设您需要编写代码来记录每个请求的 RU 费用,这是代码片段,您可以参考它:

public static class DocumentDBExtension
{
    public static async Task<IEnumerable<TSource>> QueryWithRuLog<TSource>(this IQueryable<TSource> source)
    {
        List<TSource> items = new List<TSource>();
        double totalRuCost = 0;
        var query = source.AsDocumentQuery();
        while (query.HasMoreResults)
        {
            var result =await query.ExecuteNextAsync<TSource>();
            items.AddRange(result);
            //log RU
            totalRuCost += result.RequestCharge;
        }
        //log totalRuCost
        Console.WriteLine($"RUs cost:{totalRuCost}");
        return items;
    }
}

//Usage
var result=docs.QueryWithRuLog<CampaignMessage>();