When Troubleshooting NLog logging issues, then it is a good idea to enable internal logging, which often helps to identify where the problem is. Internal log output can be sent to a file, console window or both.
Enabling internal logging using configuration fileWhen you configure NLog using Configuration File, you can enable internal logging by setting the following attribute on the <nlog>
element:
internalLogLevel="Off|Trace|Debug|Info|Warn|Error|Fatal"
– determines internal log level. The higher the level, the less verbose the internal log output.
internalLogFile="file.txt"
- adding internalLogFile cause NLog to write its internal debugging messages to the specified file. This includes any exceptions that may be thrown during logging.
Notice only basic layouts are supported, as the internal log needs to be as simple/stable as possible:
%appdata%
or %HOME%
(Introduced with NLog 4.6+).${currentdir}
(Introduced with NLog 4.6+ but without options).${basedir}
(Introduced with NLog 4.6+ but without options).${tempdir}
(Introduced with NLog 4.6+ but without options).${processdir}
(Introduced with NLog 4.7.1+ but without options).${commonApplicationDataDir}
, ${userApplicationDataDir}
, ${userLocalApplicationDataDir}
(Introduced with NLog v5 but without options)internalLogToConsole="false|true"
– sends internal logging messages to the console.
internalLogToConsoleError="false|true"
– sends internal logging messages to the console error output (stderr).
internalLogIncludeTimestamp="false|true"
- indicates whether timestamps should be included in the internal log output (NLog 4.3+)
Here is an example of a configuration file which enables internal logging to a file:
<nlog internalLogFile="c:\nlog-internal.txt" internalLogLevel="Debug"> <targets> <!-- target configuration here --> </targets> <rules> <!-- log routing rules --> </rules> </nlog>
If not getting internal logger output, then make sure the NLog.config
file has Build Action
set. See also Logging-troubleshooting. Alternative try to activate Internal Logging programmatically.
Internal logging can be configured through code by setting the following properties on InternalLogger class:
TextWriter
object to use for logging.Introduced with NLog 4.3
Introduced with NLog 4.7
For best diagnostic output then make sure to enable NLog InternalLogger, before creating the first NLog Logger-object.
Here is an example of activating internal logging from code:
using NLog; class Program { static void Main() { // enable internal logging to the console NLog.Common.InternalLogger.LogToConsole = true; // enable internal logging to a file NLog.Common.InternalLogger.LogFile = "c:\\nlog-internal.txt"; // On Linux one can use "/home/nlog-internal.txt" // enable internal logging to a custom TextWriter NLog.Common.InternalLogger.LogWriter = new StringWriter(); //e.g. TextWriter writer = File.CreateText("C:\\perl.txt") // set internal log level NLog.Common.InternalLogger.LogLevel = NLog.LogLevel.Debug; // Perform test output, ensure first NLog Logger is created after InternalLogger is enabled. NLog.LogManager.GetLogger("Test").Info("Hello World"); } }Enabling internal logging using environment variables
Before NLog 5.0 then environment variables and app.config settings would be automatically be applied on startup. But because of the startup overhead, then this has been disabled. Instead one now have to explicitly activate this behavior:
NLog.LogFactory.Setup().SetupInternalLogger(b => b.SetupFromEnvironmentVariables())
These are environment variables which control internal logging:
LogLevel.Fatal
- Just before application is going to crash.LogLevel.Error
- Logging pipeline has failed, and logevents has been discarded.LogLevel.Warn
- Detected invalid configuration but can recover. Detected connection lost but can recover.LogLevel.Info
- Application lifetime events like logging configuration loaded, and shutting down logging engine.LogLevel.Debug
- Reporting interesting events for detailed diagnostics. Having reflection issues when serializing logevents.LogLevel.Trace
- Dumping verbose configuration details.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