我当前正在修改NLog配置,并尝试将目标设置为异步并使用BufferingTarget进行邮件处理时,我发现Nlog似乎并没有在应用程序关闭时刷新邮件。
调用LogManager.Flush();也不起作用,它给出了NLog.NLogRuntimeException:
NLog.dll中发生了类型为'NLog.NLogRuntimeException'的第一次机会异常。其他信息:发生了异步异常。
我发现async="true"从targets配置中删除即可。以下是我用来进行测试的配置和控制台源代码。
我看到有很多与日志序列和/或FallbackTarget相关的帖子,但是它们对解决我目前遇到的问题没有太大帮助。
控制台应用
class Program
{
static void Main(string[] args)
{
Logger logger = LogManager.GetCurrentClassLogger();
logger.Debug("Debug Message");
logger.Debug("Debug Message");
LogManager.Flush();
LogManager.Shutdown();
}
}
Run Code Online (Sandbox Code Playgroud)
Nlog配置
class Program
{
static void Main(string[] args)
{
Logger logger = LogManager.GetCurrentClassLogger();
logger.Debug("Debug Message");
logger.Debug("Debug Message");
LogManager.Flush();
LogManager.Shutdown();
}
}
Run Code Online (Sandbox Code Playgroud)
异常堆栈跟踪
at NLog.Common.AsyncHelpers.RunSynchronously(AsynchronousAction action)
at NLog.LogFactory.Flush(TimeSpan timeout)
at NLog.LogFactory.Flush()
at NLog.LogManager.Flush()
at MyProject.NlogTests.Program.Main(String[] args) in p:...\Program.cs:line 15
Run Code Online (Sandbox Code Playgroud)
Nlog日志文件(简体)
2016-12-09 17:29:55.0471 Trace Opening \bin\Dev\\Logs\Info.log with allowFileSharedWriting=False
2016-12-09 17:29:55.5354 Trace LogFactory.Flush(00:00:15)
2016-12-09 17:29:55.5354 Trace Flushing all targets...
2016-12-09 17:29:55.5354 Trace ForEachItemInParallel() 3 items
2016-12-09 17:29:55.5819 Trace Flushing 1 events.
2016-12-09 17:29:55.6119 Trace Opening \bin\Dev\\Logs\Info.log with allowFileSharedWriting=False
2016-12-09 17:29:55.6209 Trace Continuation invoked:
2016-12-09 17:29:55.6209 Trace Parallel task completed. 2 items remaining
2016-12-09 17:29:55.6825 Trace Flushing 0 events.
2016-12-09 17:29:55.6825 Trace Continuation invoked:
2016-12-09 17:29:55.6825 Trace Parallel task completed. 1 items remaining
2016-12-09 17:30:16.7442 Trace LogFactory.Flush(00:00:15)
2016-12-09 17:30:16.7442 Trace Flushing all targets...
2016-12-09 17:30:16.7702 Trace ForEachItemInParallel() 3 items
2016-12-09 17:30:16.8032 Trace Flushing 0 events.
2016-12-09 17:30:16.8032 Trace Flushing 0 events.
2016-12-09 17:30:16.8032 Trace Continuation invoked:
2016-12-09 17:30:16.8032 Trace Parallel task completed. 2 items remaining
2016-12-09 17:30:16.8032 Trace Continuation invoked:
2016-12-09 17:30:16.8032 Trace Continuation invoked:
2016-12-09 17:30:16.8167 Trace Parallel task completed. 1 items remaining
2016-12-09 17:30:16.8167 Trace Parallel task completed. 0 items remaining
2016-12-09 17:30:16.8167 Info Shutting down logging...
2016-12-09 17:30:16.8167 Info Closing old configuration.
2016-12-09 17:30:16.8167 Trace LogFactory.Flush(00:00:15)
2016-12-09 17:30:16.8167 Trace Flushing all targets...
2016-12-09 17:30:16.8167 Trace ForEachItemInParallel() 3 items
2016-12-09 17:30:16.8457 Trace Using basic authentication: Username='########@########.com' Password='********'
2016-12-09 17:30:16.8472 Debug Sending mail to ########@########.com using smtp.gmail.com:587 (ssl=True)
2016-12-09 17:30:16.8472 Trace Subject: 'Service: ####### | ######### | Info'
2016-12-09 17:30:16.8472 Trace From: '########@########.com'
Run Code Online (Sandbox Code Playgroud)