Use NLog in ASP.NET Core application

For ASP.NET Core, you need NLog.Web.AspNetCore – which has a dependency on NLog.Extensions.Logging. Note: Microsoft.Framework.Logging.NLog has been replaced by NLog.Extensions.Logging on NuGet, which is maintained by the NLog team. How to use: Setup NLog needs to be enabled so it will integrate into the DI and log API of ASP.NET Core. This will result that … Read more

When should I use Tracing vs Logger.NET, Enterprise Library, log4net or Ukadc.Diagnostics?

There are many similar questions here on SO: Logging best practices log4net versus TraceSource Silverlight Logging framework and/or best practices log4net vs. Nlog What’s the point of a logging facade? C# Logging. What should I use? You missed several commonly used logging frameworks. Here is a list of commonly used frameworks, some of which you … Read more

NLog: How to exclude specific loggers from a specific rule?

I think something like this is what you want: <logger name=”SpammyLogger” minlevel=”Off” maxlevel=”Trace” final=”true” /> <logger name=”SpammyLogger” minlevel=”Debug” maxlevel=”Fatal” writeTo=”SpammyFileTarget” final=”true” /> <logger name=”*” levels=”Trace” writeTo=”RegularFileTarget/” /> Adding final=”true” means that no more rules will be executed for the events produced by “SpammyLogger”, but it applies only to the specified levels.(see https://github.com/nlog/nlog/wiki/Configuration-file#rules) See this link … Read more

Global exception handling in ASP.NET Web API 2.1 with NLog?

It’s actually quite simple, you either implement IExceptionLogger by hand or inherit from the base class, ExceptionLogger. public class NLogExceptionLogger : ExceptionLogger { private static readonly Logger Nlog = LogManager.GetCurrentClassLogger(); public override void Log(ExceptionLoggerContext context) { Nlog.LogException(LogLevel.Error, RequestToString(context.Request), context.Exception); } private static string RequestToString(HttpRequestMessage request) { var message = new StringBuilder(); if (request.Method != null) … Read more

Delete log files after x days

You could simply use the built-in archiving functionality. This setting will keep 7 old log files in addition to your current log. The cleanup is done by NLog automatically. <?xml version=”1.0″ ?> <nlog xmlns=”http://www.nlog-project.org/schemas/NLog.xsd” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”> <targets> <target name=”file” xsi:type=”File” layout=”${longdate} ${logger} ${message}” fileName=”${basedir}/logs/logfile.txt” archiveFileName=”${basedir}/logs/log.{#}.txt” archiveEvery=”Day” archiveNumbering=”Rolling” maxArchiveFiles=”7″ concurrentWrites=”true” /> </targets> <rules> <logger name=”*” minlevel=”Debug” writeTo=”file” … Read more