When you get no log output from NLog, then you should check these normal pitfalls:
Build Action = Content
and Copy to Output Directory = Copy if newer
in Visual Studio.NLog.LogManager.Shutdown()
before application exits, so targets are not flushed correctly.NLog.config
(Or Application.exe.nlog
) that takes priority.When the checkpoints listed above doesn't resolve the issue, then one can try these troubleshooting steps:
nlog.config
file is configured with Build Action = None
or Copy to Output Directory = Do not copy
in Visual Studio. Then fix it and set Build Action = Content
and Copy to Output Directory = Copy if newer
.nlog.config
in all lowercase, because of case-sensitive filenames.Web.config
/App.config
/etc. which can overwrite the custom NLog-configuration.nlog.config
on application-deployment.NLog.config
, then include the xml-attribute throwConfigExceptions="true"
for the NLog.config
. This will ensure NLog will "explode" when something is wrong with loading the NLog.config (Also invalid XML):<nlog throwConfigExceptions="true"> <targets> ... </targets> <rules> ... </rules> </nlog>
NLog.config
:
<nlog internalLogLevel="Debug" internalLogFile="c:\temp\nlog-internal.txt" internalLogToConsole="true" throwConfigExceptions="true"> <targets> ... </targets> <rules> ... </rules> </nlog>
class Program { // Make sure to comment out static Logger-objects, or else InternalLogger will not work // readonly static Logger = LogManager.GetCurrentClassLogger(); static void Main() { NLog.Common.InternalLogger.LogLevel = NLog.LogLevel.Debug; NLog.Common.InternalLogger.LogToConsole = true; NLog.Common.InternalLogger.LogFile = @"c:\temp\nlog-internal.txt"; // On Linux one can use "/home/nlog-internal.txt" Logger logger = LogManager.GetLogger("foo"); logger.Info("Program started"); LogManager.Shutdown(); // Remember to flush } }
<rules>
section.
NLog.LogManager.Shutdown()
.<nlog throwExceptions="true"> <targets> <target name="file" type="File" fileName="${basedir}/log.txt" /> </targets> <rules> <logger name="*" minLevel="Trace" writeTo="{all target names separated by comma}" /> </rules> </nlog>
LogManager.ThrowExceptions
. It is only intended for unit-testing, and it can have unwanted side-effects like unwanted application crashes. So make sure to turn it off again when done with troubleshooting:
⚠️ If having
throwExceptions="true"
enabled already, then it can be the cause of NLog is not working. ThethrowExceptions="true"
is intended for unit-testing of NLog itself, and will prevent NLog from doing self-recovery after failure, and should never be used in production environment.
class Program { // Make sure to comment out static Logger-objects, or else InternalLogger will not work // readonly static Logger = LogManager.GetCurrentClassLogger(); static void Main() { NLog.LogManager.ThrowExceptions = true; // TODO Remove this when done trouble-shooting NLog.Common.InternalLogger.LogLevel = LogLevel.Debug; NLog.Common.InternalLogger.LogToConsole = true; NLog.Common.InternalLogger.LogFile = "c:\temp\nlog-internal.txt"; // On Linux one can use "/home/nlog-internal.txt" // Perform test output, ensure first NLog Logger is created after InternalLogger is enabled. Logger logger = LogManager.GetLogger("foo"); logger.Info("Program started"); LogManager.Shutdown(); // Remember to flush } }
${basedir}\log.txt
your logs may be written to the working directory, which is not necessarily the same directory as where the application resides.NLog has its own internal logging system which can be used to troubleshoot problems with log routing and configuration. The simplest way to enable is to add the following attributes to your configuration file:
internalLogFile="c:\path\to\nlog-internal.txt"
- specifies the location of the internal log fileinternalLogLevel="Trace"
specifies the level of detail of information written to the internal log fileinternalLogToConsole="true"
writes internal log to the console outputMore ways to enable internal logging are explained here.
Other troubleshooting tools and techniquesYou may use a tracing application such as Process Monitor from SysInternals to monitor file activity in your system. This should give you an idea of what files are being read and written and their exact locations.
Process Explorer is another indispensable utility which can help with investigation of various system-level issues, such as permissions, threading, deadlocks, performance, etc.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4