2016-11-23 314 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