10

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

应用主机:

LogManager.LogFactory = new NLogFactory();

在应用逻辑中:

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>

4

1 回答 1

7

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

LogManager.LogFactory = new NLogFactory();
var appHost = new AppHost();
appHost.Init();
于 2013-04-04T17:39:11.490 回答