是否可以将JsonAttribute转换为Integer

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

Json布局中的Nlog是否可以将Json属性作为整数花费。属性始终以字符串形式输出。我试过以下代码示例:

到目前为止,版本是正确的,就像字符串而不是整数。

可以帮我一个人了解什么是错的?

LayoutRenderer.Register("level", info => info.Level.Ordinal * 100);
var fieldsLayout = new JsonLayout
{
    RenderEmptyObject = false,
    SuppressSpaces = true
};

fieldsLayout.Attributes.Add(new JsonAttribute("channel", "${channel}"));
fieldsLayout.Attributes.Add(new JsonAttribute("level", "${level}"));
c# nlog
1个回答
2
投票

认为覆盖默认的${level}渲染器是个坏主意。

水平的简单解决方案就是这样做:

fieldsLayout.Attributes.Add(new JsonAttribute("level", "${level:format=ordinal}") { Encode = false });

但是如果你需要将它乘以100那么你需要注册一个自定义布局渲染器:

LayoutRenderer.Register("level100", info => info.Level.Ordinal * 100);
fieldsLayout.Attributes.Add(new JsonAttribute("level", "${level100}") { Encode = false });
© www.soinside.com 2019 - 2024. All rights reserved.