nlog 根据记录器优先级更改行为
nlog changes behaviour depending on logger priority
我想记录某个布局:
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();
}
我的记录器 class 是:
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:\temp\nlog-internal.log">
<variable name="logFile" value="./logs/${shortdate}/${logger}.log" />
<variable name="colon" value="${literal:text=\:}"/>
<variable name="quotationMark" value="${literal:text="}"/>
<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 序列化程序。
有什么想法吗?
UPDATE
在 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 ver. 中存在错误。 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();
}
}
我想记录某个布局:
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();
}
我的记录器 class 是:
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:\temp\nlog-internal.log">
<variable name="logFile" value="./logs/${shortdate}/${logger}.log" />
<variable name="colon" value="${literal:text=\:}"/>
<variable name="quotationMark" value="${literal:text="}"/>
<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 序列化程序。
有什么想法吗?
UPDATE
在 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 ver. 中存在错误。 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();
}
}