通过SeriLog写入日志AzureCosmosDB:分区键

我正在通过SeriLog将日志写入AzureDocumentDB。看起来SeriLog已经拥有自己的日志结构,并且所有自定义字段都位于 Properties 字段下(请检查附件)。

在那种情况下,如何使用自定义字段作为数据库分区键?

当我使用 / Properties / UserId 作为分区键时,所有日志都会被CosmosDB拒绝,并说必须为此操作提供分区键值。如何将UserId指定为分区键?

任何帮助将不胜感激。

通过SeriLog写入日志AzureCosmosDB:分区键

更新: 我使用ILogger实例编写日志。示例:

logger.LogInformation(“会话已完成:{@SessionCompleteEvent}”,sessionCompleteEvent);

Serilog应用程序设置:

{
  "Serilog": {
    "WriteTo": {
      "CosmosDB": {
        "Name": "Logger","Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly","Args": {
                  "expression": "StartsWith(@Properties['SourceContext'],'MyApp')"
                }
              }
            ],"WriteTo": [
              {
                "Name": "AzureDocumentDB","Args": {
                  "endpointUrl": "https://analytics-test.documents.azure.com:345/","authorizationKey": "app_key","databaseName": "db0","collectionName": "my_app","restrictedToMinimumLevel": "Information"
                }
              }
            ]
          }
        }
      }
    }
  }
}
Star595198770 回答:通过SeriLog写入日志AzureCosmosDB:分区键

  

此操作必须提供分区键值

基于此错误,在将文档插入分区集合时需要在requestOptions中指定分区键设置。请参考我的示例代码:

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        private static readonly string endpointUrl = "https://***.documents.azure.com:443/";
        private static readonly string authorizationKey = "***";
        private static readonly string databaseId = "db";
        private static readonly string collectionId = "coll";

        private static DocumentClient client;

        static void Main(string[] args)
        {
            QueryTest();
            Console.ReadLine();
        }


        public static async void QueryTest()

        {
            client = new DocumentClient(new Uri(endpointUrl),authorizationKey);
            var uri = UriFactory.CreateDocumentCollectionUri(databaseId,collectionId);


            Pojo pojo = new Pojo();
            pojo.name = "Name";
            pojo.Properties = new Properties();
            pojo.Properties.UserId = "123";

            RequestOptions request = new RequestOptions();
            request.PartitionKey = new PartitionKey("123");

            await client.CreateDocumentAsync(uri,pojo,request,false);
        }
    }
    class Pojo : Document
    {
        public Properties Properties { get; set; }
        public string name { get; set; }
    }

    class Properties
    {

        public string UserId { get; set; }
    }
}

更新答案:

深入研究您的注释中提到的SeriLog AzureDocumentDB sink的源代码,发现导入操作是通过执行bulk import js存储过程来实现的。

try {
                SelfLog.WriteLine($"Sending batch of {logEventsBatch.Count} messages to DocumentDB");
                var storedProcedureResponse = await _client
                                                   .ExecuteStoredProcedureAsync<int>(_bulkStoredProcedureLink,args)
                                                   .ConfigureAwait(false);
                SelfLog.WriteLine(storedProcedureResponse.StatusCode.ToString());

                return storedProcedureResponse.StatusCode == HttpStatusCode.OK;
            }

但是,根据我的研究和测试,批量导入js仅支持单个分区集合。请查看下面的线程,它们遇到了与您相同的错误:

1。https://github.com/Azure/azure-documentdb-datamigrationtool/issues/47

2。https://github.com/Azure/azure-documentdb-datamigrationtool/issues/64

因此,恐怕您必须遵循.net executor lib来自行修改SeriLog AzureDocumentDB sink的导入代码。

本文链接:https://www.f2er.com/3162944.html

大家都在问