You can use nlog
as <sharedListeners>
in the App.config
<system.diagnostics>
<sources>
<source name="System" switchValue="All">
<listeners>
<add name="nlog" />
</listeners>
</source>
<source name="MySystem" switchValue="All">
<listeners>
<add name="nlog" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="nlog" type="NLog.NLogTraceListener, NLog" />
</sharedListeners>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="nlog" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
Edit: using TraceSource
As Nlog
comes with a TraceListener
which can be used as shared listener, you can also add a custom soure MySystem
as above you can get rid of LogManager.GetCurrentClassLogger();
and can use the build-in TraceSource
itself as.
public class Person
{
//...
var ts = new TraceSource("MySystem");
//or ts = TraceSource(this.GetType(),Name);
ts.TraceInformation("This will go through nLog listener");
This will avoid you to reference nlog.dll
in each/every project in solution and will minimize the dependency to a single main project where App.Config
resides.
Here with TraceSource(this.GetType(),Name)
you will achieve what LogManager.GetCurrentClassLogger()
will do for you. And for class Person
you need to add the following in the App.Config
:
<source name="Person" switchValue="All">
<listeners>
<add name="nlog" />
</listeners>
</source>
In turn now you can switch on/off logging from a module without rebuilding the whole solution.