Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to route System.Diagnostics.TraceSource logs through NLog in .NET Core

is there anyway to route System.Diagnostics.TraceSource logs through NLog in .NET Core? Using the NLog for .Net 4.5 we could setup NLog.NLogTraceListener in the app.config file as remonstrated in this post http://nlog-project.org/2010/09/02/routing-system-diagnostics-trace-and-system-diagnostics-tracesource-logs-through-nlog.html , but we dont have app.config in .net core anymore.

I've setup libraries that are outputting logs with TraceSource and I would like to route it to NLog targets.

Suggestions are welcome.

like image 506
glm Avatar asked Oct 14 '16 21:10

glm


1 Answers

Try this. It worked for me.

using System.Diagnostics;
using NLog;

// Add this at the start of your app, like in Program.Main()
Trace.Listeners.Add(new NLogTraceListener());

// Now you can do...
Trace.TraceInformation("This message should go through NLog");

Of course, you'll need to properly configure NLog (NLog.config) with the targets you need to see the messages.

Beware of target using xsi:type="Trace" as this will lead to StackOverFlow Exception.

like image 82
Filip Jansky Avatar answered Oct 08 '22 23:10

Filip Jansky