nlog 根据记录器优先级更改行为

  • 本文关键字:优先级 记录器 nlog nlog
  • 更新时间 :
  • 英文 :


我想记录某个布局:

Message: {the message}  
Properties: {key}: {value} | {key}: {value} | ...

我主要是:

static void Main(string[] args)
{
Loggers.TxtLogger.LogInfo();
Loggers.TxtLogger.LogInfo("My custom message");
Loggers.TxtLogger.LogInfo(new { id = 1, issue = "my custom issue" });

Console.ReadKey();
}

我的记录器类是:

public static class Loggers
{
public static ILogger TxtLogger { get; private set; }
static Loggers()
{
TxtLogger = LogManager.GetLogger("TxtLogger");
}

public static void LogInfo(this ILogger @this, object message = null)
{
@this.Info()
.Property("Property1Key", "Property1Value")
.Property("Property2Key", "Property2value")
.Message(message == null ? "" : "{@message}", message)
.Write();
}
public static void LogError(this ILogger @this, Exception e, object message = null)
{
@this.Error()
.Exception(e)
.Property("Property1Key", "Property1Value")
.Property("Property2Key", "Property2value")
.Message(message == null ? "" : "{@message}", message)
.Write();
}
}

我的 NLog.config 是:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
throwConfigExceptions="true"
internalLogLevel="Off" internalLogFile="c:tempnlog-internal.log">
<variable name="logFile" value="./logs/${shortdate}/${logger}.log" />
<variable name="colon" value="${literal:text=:}"/>
<variable name="quotationMark" value="${literal:text=&quot;}"/>
<variable name="messageIfNotEmpty" value="${when:when=length('${message}')>0:inner=Message${colon} ${message}${newline}}"/>
<variable name="properties" value="Properties${colon} ${all-event-properties:format=[key]: [value]:separator= | }${newline}"/>
<variable name="layout2" value="${messageIfNotEmpty}${properties}"/>
<targets>
<target name="ConsoleLog" xsi:type="Console" layout="${layout2}"/>
<target name="TxtLog" xsi:type="File" fileName="${logFile}" layout="${layout2}"/>
</targets>
<rules>
<logger name="*" writeTo="ConsoleLog" final="true"/>
<logger name="*" writeTo="TxtLog" />
</rules>
</nlog>

有了这个集合,我得到了想要的结果:

Properties: Property1Key: Property1Value | Property2Key: Property2value  
Message: "My custom message"  
Properties: Property1Key: Property1Value | Property2Key: Property2value 
Message: {"id":1, "issue":"my custom issue"}  
Properties: Property1Key: Property1Value | Property2Key: Property2value 

但是当我尝试将其记录到.txt文件中时(我从ConsoleLog记录器中删除了_final="true"_属性(,我得到以下结果:

Properties: Property1Key: Property1Value | Property2Key: Property2value
Message: "My custom message"  
Properties: message: My custom message | Property1Key: Property1Value | Property2Key: Property2value  
Message: {"id":1, "issue":"my custom issue"}  
Properties: message: { id = 1, issue = my custom issue } | Property1Key: Property1Value | Property2Key: Property2value  

现在有一个解决方法,那就是如果我将_LogInfo_方法中的_object message = null_参数更改为_string message_并使用_newtonsoft json serializer_来序列化对象,但我想使用 NLog 序列化程序。
有什么想法吗?

更新

在 NLog.Config 上,我更改了:<variable name="layout2" value="${messageIfNotEmpty}${properties}"/>
<variable name="layout2" value="${properties}${messageIfNotEmpty}"/>
(将消息变量放在属性变量之后(和:

<rules> <logger name="*" writeTo="ConsoleLog" final="true"/> <logger name="*" writeTo="TxtLog" /> </rules>


<rules> <logger name="*" writeTo="ConsoleLog"/> <logger name="*" writeTo="TxtLog" /> </rules>
(删除了final=true属性(

所以现在我的控制台结果是:

Properties: Property1Key: Property1Value | Property2Key: Property2value
Properties: Property1Key: Property1Value | Property2Key: Property2value
Message: "My custom message"
Properties: Property1Key: Property1Value | Property2Key: Property2value
Message: {"id":1, "issue":"my custom issue"}  

这是所需的结果(属性后带有消息(,
但在 txt 文件中我得到结果:

Properties: Property1Key: Property1Value | Property2Key: Property2value
Properties: message: My custom message | Property1Key: Property1Value | Property2Key: Property2value
Message: "My custom message"
Properties: message: { id = 1, issue = my custom issue } | Property1Key: Property1Value | Property2Key: Property2value
Message: {"id":1, "issue":"my custom issue"}  

这显然不是预期的结果。

现在我再次更改 NLog.Config:

<rules>
<logger name="*" writeTo="ConsoleLog"/>
<logger name="*" writeTo="TxtLog" />
</rules>  

自:

<rules>
<logger name="*" writeTo="TxtLog" />
<logger name="*" writeTo="ConsoleLog"/>
</rules>  

(我把TxtLog放在第一位(
现在结果是受人尊敬的,
控制台:不是想要的结果(消息打印两次(
TXT:期望的结果
最后,如果我改变:

<variable name="layout2" value="${properties}${messageIfNotEmpty}"/>  

回到:

<variable name="layout2" value="${messageIfNotEmpty}${properties}"/>  

(将消息变量放在属性之前(,
无论记录器顺序如何,我都没有得到想要的结果。

NLog 版本 4.5.10 中存在一个错误,但您可以通过将代码更改为以下内容来解决它:

public static class Loggers
{
public static ILogger TxtLogger { get; private set; }
static Loggers()
{
TxtLogger = LogManager.GetLogger("TxtLogger");
}

public static void LogInfo(this ILogger @this, object message = null)
{
@this.Info()
.Message("{0}", message ?? (object)string.Empty)
.Property("Property1Key", "Property1Value")
.Property("Property2Key", "Property2value")
.Write();
}
public static void LogError(this ILogger @this, Exception e, object message = null)
{
@this.Error()
.Message("{0}", message ?? (object)string.Empty)
.Exception(e)
.Property("Property1Key", "Property1Value")
.Property("Property2Key", "Property2value")
.Write();
}
}

另请参阅 https://github.com/NLog/NLog/pull/2963

最新更新