gpt4 book ai didi

asp.net - NLog 彩色控制台

转载 作者:行者123 更新时间:2023-12-02 23:42:34 30 4
gpt4 key购买 nike

我正在使用 NLog 来记录错误。这是配置代码

<target name="console" xsi:type="AsyncWrapper" >
<target xsi:type="ColoredConsole" layout="${longdate:padding=-10}${callsite:className=false:includeSourcePath=false:methodName=false} | ${message}" >
<highlight-row condition="level >= LogLevel.Info" foregroundColor="Green" backgroundColor="NoChange"/>
</target>
</target>

我在日志事件上设置了一个自定义属性,例如

private LogEventInfo GetLogEvent(string loggerName, LogLevel level, string message, ConsoleColor color)
{
var logEvent = new LogEventInfo(level, loggerName, message);

logEvent.Properties["color"] = color;// color= any console color
}

这设置了“颜色”属性。(这里说“红色”)

我正在尝试在目标中使用这个“颜色”属性,例如

 <highlight-row condition="equals('${color}','Red')" foregroundColor="Red" backgroundColor="NoChange"/> 

这确实有效,我尝试过

<highlight-row condition="equals('${event-context:item=color}','Red')" foregroundColor="Red" backgroundColor="NoChange"/> 

但运气不好。

我错过了什么或者有更好的方法吗?在这种情况下我们可以使用布局渲染器吗?如果是,我们如何实现?

最佳答案

首先,由于您将值存储在 LogEventInfo.Properties 中,因此您应该使用第二个配置示例,该示例从 event-context 获取值。

我还没有使用过ColoredConsoleTarget,所以将此作为一个建议,而不是我知道的事实会起作用的东西。

我怀疑 NLog Condition 对象不知道 ConsoleOutputColor 枚举。因此,当您将 ConsoleOutputColor 枚举值存储在 LogEventInfo.Properties 中时,Condition 不知道 'Red' >(在条件中)指的是 ConsoleOutputColor.Red。我有两个建议:

第一个选项:将 ConsoleOutputColor 的字符串值存储在 LogEventInfo.Properties 中。使用 ToColor 可能就足够了。像这样的事情:

var logEvent = new LogEventInfo(level, loggerName, message);
logEvent.Properties["color"] = color.ToString();

然后,在您的 Condition 中,您应该能够与 ConsoleOutputColor 字符串值进行比较(如果您存储颜色,您的配置中的内容可能是正确的)按照我的建议命名字符串)。

如果这不起作用,您可以尝试...

第二个选项:将 ConsoleOutputColor 值存储在 LogEventInfo.Properties 中,就像您现在所做的那样,但更改配置文件中的条件以比较来自的“颜色” event-context 为 ConsoleOutputColor 值的数值。像这样的东西(我没有尝试过,所以我不确定它是否正确):

<highlight-row condition="equals('${event-context:item=color}','12')" foregroundColor="Red" backgroundColor="NoChange"/>

(在 ConsoleOutputColor 枚举中,Red12)。

关于asp.net - NLog 彩色控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13105168/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com