如何使用NLog Config和ServiceStack日志记录?

时间:2012-11-08 17:12:38

标签: servicestack nlog

基本的连线似乎很简单但是,我很难理解如何正常配置NLog。鉴于以下设置,如何设置配置以将文本文件转储到文件夹?

APPHOST:

LogManager.LogFactory = new NLogFactory();

在App Logic中:

ILog log = LogManager.GetLogger(GetType());

log.InfoFormat("Something happened");

配置文件,如:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<targets>
<target name="console" xsi:type="ColoredConsole"
 layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}" />
<target name="file" xsi:type="File" fileName="${specialfolder:folder=ApplicationData}/logs/App.log"
 layout="${date}: ${message}" />
<target name="eventlog" xsi:type="EventLog" source="My App" log="Application"
layout="${date}: ${message} ${stacktrace}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="file" />
<logger name="*" minlevel="Fatal" writeTo="eventlog" />
</rules>

1 个答案:

答案 0 :(得分:7)

理想情况下,应在AppHost初始化之前指定日志记录,因此ServiceStack中所有类的所有静态初始化程序都使用配置的记录程序,例如:

LogManager.LogFactory = new NLogFactory();
var appHost = new AppHost();
appHost.Init();