我是NLog的新手,我一直在玩LogEventInfo對象,因爲我想我需要在我的應用程序中使用它們。我使用純文本字符串創建了一個LogEventInfo對象,然後使用設置爲寫入$ {message}的FileAppender調用Logger.Debug(myEventInfoObject)。在我希望看到我的文本字符串的地方,我看到了記錄器名稱,消息級別,消息和序列ID。我發現擴展字符串是我在調用myEventInfoObject.ToString()時得到的。如何控制$ {message}來自LogEventInfo對象時出現的內容?NLog:如何控制來自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月8日10 :14:13.5525 NLogSample.Program Log Event:Logger =''Level = Debug Message ='來自LogInfoEvent的此消息將不被使用。' SequenceID = 2
我在Win7 Pro計算機上使用Visual Studio 2012。
非常感謝!
ROBR