[英]Writing logs AzureCosmosDB via SeriLog: Partition Key
我正在通過 SeriLog 將日志寫入 AzureDocumentDB。 看起來 SeriLog 已經擁有自己的日志結構,並且所有自定義字段都放置在Properties字段下(請查看附件)。
在這種情況下,如何使用自定義字段作為我的數據庫分區鍵?
當我使用/Properties/UserId作為我的分區鍵時,所有日志都被 CosmosDB 拒絕,說必須為此操作提供分區鍵值。 如何將 UserId 指定為分區鍵?
任何幫助將不勝感激。
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"
}
}
]
}
}
}
}
}
}
必須為此操作提供分區鍵值
基於這個錯誤,你需要在將文檔插入分區集合時,在 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的源代碼,發現導入操作是通過執行批量導入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只支持單分區collections。 請看下面的線程,他們遇到了和你一樣的錯誤:
1. https://github.com/Azure/azure-documentdb-datamigrationtool/issues/47
2. https://github.com/Azure/azure-documentdb-datamigrationtool/issues/64
所以,恐怕你必須按照.net 執行器庫自己修改SeriLog AzureDocumentDB sink
的導入代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.