如何在 C# 中解构字符串数组并将其作为 Serilog 的参数传递

问题描述 投票:0回答:1

我正在与 Serilog 合作。

我有这个代码

var message = "Age: {Age}, Weight: {Weight}";

// the list is ordered with the keys in the message
var elements = new List<string>{"15yo","60kgs"};

// call log method
logger.Log(LogLevel.Information, message, elements.ToArray());
// Expected log: 
"Age: 15yo, Weight: 60kgs"

// Actual log: 
"Age: ["15yo", "60kgs"], Weight: {Weight}"

实际上,它不是将数组解构为多个参数,而是将整个数组作为单个参数并替换第一个键

需要任何帮助。

谢谢

c# serilog
1个回答
0
投票

尝试将其投射到

object[]

elements.Cast<object>().ToArray()

© www.soinside.com 2019 - 2024. All rights reserved.