NLog Internal Logging
Internal Logging
If you have trouble getting NLog to work properly you may want to enable internal logging, which can help identify where the problem is. Internal log output can be sent to a file, console window or both.
Enabling internal logging using configuration file
When 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.- Note: only a few layouts are supported, as the internal log needs to be as stable as possible.
- NLog 4.6+: Environment Variables are also supported: e.g.
%appdata%
- NLog 4.6+: Supports renderers (without options):
${currentdir}
,${basedir}
,${tempdir}
- NLog 4.7.1+: Supports renderer (without options):
${processdir}
internalLogToConsole="false|true"
– sends internal logging messages to the console.internalLogToConsoleError="false|true"
– sends internal logging messages to the console error output (stderr).internalLogToTrace="false|true"
– sends internal logging messages toSystem.Diagnostics.Trace
(introduced in NLog 4.3)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:\log.txt" internalLogLevel="Trace">
<targets>
<!-- target configuration here -->
</targets>
<rules>
<!-- log routing rules -->
</rules>
</nlog>
Enabling internal logging using environment variables
There are environment variables which control internal logging. You can set those variables before running your program to enable internal logging:
- NLOG_INTERNAL_LOG_LEVEL - sets the internal logging level. The available values are Trace, Debug, Info, Warn, Error or Fatal - the default is Info which should be appropriate for most cases, to get more detailed logging - set it to Debug or Trace.
- NLOG_INTERNAL_LOG_FILE - if this variable is found in the environment NLog will save internal log to the specified file. The file must be writable by the current user or it will not be created.
- NLOG_INTERNAL_LOG_TO_CONSOLE - if this variable is found in the environment, will outputs internal diagnostic information to the console
- NLOG_INTERNAL_LOG_TO_CONSOLE_ERROR - sets internalLogToConsoleError
- NLOG_INTERNAL_LOG_TO_TRACE - write internal log to
System.Diagnostics.Trace
(introduced in NLog 4.3) - NLOG_INTERNAL_INCLUDE_TIMESTAMP - sets internalLogIncludeTimestamp (introduced in NLog 4.3)
Enabling internal logging programmatically
Internal logging can be configured through code by setting the following properties on InternalLogger class:
- InternalLogger.LogLevel - specifies internal logging level
- InternalLogger.LogFile - specifies name of the log file (null will disable logging to a file)
- InternalLogger.LogToConsole - enables or disables logging to the console
- InternalLogger.LogToConsoleError - enables or disables logging to the console error stream
- InternalLogger.LogToTrace - enables or disables logging to
System.Diagnostics.Trace
(introduced in NLog 4.3) - InternalLogger.LogWriter - specifies a
TextWriter
object to use for logging - InternalLogger.IncludeTimestamp - enables or disables whether timestamps should be included in the internal log output (NLog 4.3+)
using NLog;
using NLog.Common;
class Program
{
static void Main()
{
// enable internal logging to the console
InternalLogger.LogToConsole = true;
// enable internal logging to a file
InternalLogger.LogFile = "c:\\log.txt";
// enable internal logging to a custom TextWriter
InternalLogger.LogWriter = new StringWriter(); //e.g. TextWriter writer = File.CreateText("C:\\perl.txt")
// set internal log level
InternalLogger.LogLevel = LogLevel.Trace;
}
}
No internal log at all?
Maybe NLog can't find your nlog.config, see Logging-troubleshooting
2020-07-06 11:44:20.7883 Warn Failed to create file appender: log\rule\Rule_Info_20200706.log Exception: System.UnauthorizedAccessException: Access to the path 'log\rule\Rule_Info_20200706.log' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileSystemRights rights, FileShare share, Int32 bufferSize, FileOptions options)
at NLog.Internal.FileAppenders.WindowsMultiProcessFileAppender.CreateAppendOnlyFile(String fileName)
at NLog.Internal.FileAppenders.WindowsMultiProcessFileAppender..ctor(String fileName, ICreateFileParameters parameters)
at NLog.Internal.FileAppenders.WindowsMultiProcessFileAppender.Factory.NLog.Internal.FileAppenders.IFileAppenderFactory.Open(String fileName, ICreateFileParameters parameters)
at NLog.Internal.FileAppenders.FileAppenderCache.CreateAppender(String fileName, Int32 freeSpot)
https://github.com/NLog/NLog/issues/2710#issuecomment-654046302
InternalLogger
is never activated if called NLog Logger.Debug-method.
InternalLogger
is a diagnostic tool for NLog Targets (Like the FileTarget) to report events. InternalLogger
doesn't inspect the exceptions logged by the application itself using NLog Logger-objects.
If a Logger.Debug
call reaches a NLog Target that triggers an error (Ex. UnauthorizedAccessException
), then the event is usually reported to the InternalLogger
using another severity-level (Ex Error
-LogLevel).
If you did mean InternalLogger.Debug(ex, "My Exception)
then it will only be reported to output when using InternalLogger.LogLevel = NLog.LogLevel.Debug
(Works like minLevel)
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-07-06 What does jQuery.fn mean?
2018-07-06 Bootstrap4 网格系统
2017-07-06 out
2017-07-06 TortoiseSvn安装的时候,将svn的命令行工具单独隔离出来
2015-07-06 Binary to Text (ASCII) Conversion
2015-07-06 Pizza pieces