【发布时间】:2014-10-08 14:30:42
【问题描述】:
我是 NLog 的新手,我一直在使用 LogEventInfo 对象,因为我认为我的应用程序中需要它们。我用纯文本字符串创建了一个 LogEventInfo 对象,然后我调用了 Logger.Debug(myEventInfoObject),使用 FileAppender 设置来写入 ${message}。在我希望看到我的文本字符串的地方,我看到了记录器名称、消息级别、消息和序列 ID。我发现那个扩展的字符串是我调用 myEventInfoObject.ToString() 时得到的。如何控制 ${message} 来自 LogEventInfo 对象时出现的内容?
这是我的配置文件:
<?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"
throwExceptions="true">
<targets>
<target name="file" xsi:type="File" layout="${longdate} ${logger} ${message}" fileName="${basedir}/nlog_sample_file.txt" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
这是我生成日志的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
namespace NLogSample
{
class Program
{
static void Main(string[] args)
{
try
{
Logger logger = LogManager.GetCurrentClassLogger();
LogEventInfo theEvent = new LogEventInfo(LogLevel.Debug, "", "This message from the LogInfoEvent will not be used.");
theEvent.Properties["Message"] = "This message from the LogInfoEvent Message property will be used.";
logger.Debug(theEvent);
logger.Debug("Does the sequence ID show up in this message?");
string formattedMessage = theEvent.FormattedMessage;
string eventString = theEvent.ToString();
}
catch (Exception ex)
{
int a = 1;
}
}
}
}
最后,这里是一个消息示例:
2014-10-08 10:14:13.5525 NLogSample.Program 日志事件:Logger=''Level=Debug Message='LogInfoEvent 中的这条消息将不会被使用。'序列ID=2
我在 Win7 Pro 机器上使用 Visual Studio 2012。
非常感谢!
罗伯R
【问题讨论】: