2016-11-23 316 views
2

有没有使用NLog给特定单词上色的方法?我的目标是ColoredConsoleNLog:ColoredConsole中的颜色特定单词

比方说,我要的颜色的longdate红色和调用点蓝色。什么是最好的方式来做到这一点?这似乎并不工作:

<target xsi:type="ColoredConsole" 
     name="debugConsole"    
     layout="${longdate} ${callsite} ${message}"> 
    <highlight-word text="${longdate}" foregroundColor="Red" /> 
    <highlight-word text="${callsite}" foregroundColor="Blue" /> 
</target> 
+1

见(https://github.com/nlog/NLog/wiki/ColoredConsole-target) – Amy

+0

嗨艾米[为'ColoredConsoleTarget'文件],感谢您的帮助。我已经用我目前的尝试更新了我的问题。 – Aetherix

+0

'text'属性不是'Layout'。这将是太昂贵,因此这不会工作。 – Julian

回答

1

如果您缠绕longdatecallsite一些字符,你可以使用正则表达式。

例如

<target xsi:type="ColoredConsole" 
     name="debugConsole"    
     layout="(${longdate}) [${callsite}] ${message}"> 
    <highlight-word regex="\([^)]+\)" foregroundColor="Red" /> 
    <highlight-word regex="\[[^]]+\]" foregroundColor="Blue" /> 
</target> 
+0

好的,这是一个潜在的解决方法。仍然好奇,为什么上述不工作。 – Aetherix

+0

我认为$ {longdate}和$ {callsite}是按字面解释的。 – Aetherix

+0

做了一点测试。试图登录“你好$ {callsite}”,它没有变成彩色,所以也许它不是字面解释。 – Aetherix