Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NLog Config with ServiceStack logging?

The basic wire up seems straight forward but, I'm having difficulty understanding how to configure NLog as I might normally. Given the following setup, how would I set the configuration in order to get a text file dumped to a folder?

AppHost:

LogManager.LogFactory = new NLogFactory();

In App Logic:

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

log.InfoFormat("Something happened");

A Config file like:

<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>

like image 373
Shawn Avatar asked Nov 08 '12 17:11

Shawn


1 Answers

Logging should ideally be specified before the AppHost is initialized, so all static initalizers for all classes in ServiceStack use the configured logger, e.g:

LogManager.LogFactory = new NLogFactory();
var appHost = new AppHost();
appHost.Init();
like image 161
mythz Avatar answered Sep 26 '22 15:09

mythz