2010-03-09 36 views
2

我在我的应用程序中配置了NLog以使用BufferingTargetWrapper发送带有MailTarget的电子邮件。NLog:强制BuffDoTargetWrapper在AppDomain上为空UnhandledException

我遇到的问题是我找不到一种方法来强制NLog在应用程序退出未处理的异常之前清空BufferingTargetWrapper

我尝试从当前应用程序域的UnhandledException事件中调用LogManager.Flush()LogManager.DisableLogging(),但它没有缝合工作。

我需要做什么才能发送电子邮件?

+0

您使用的是PostFilteringWrapper?我真的很想看到你的.nlog或程序化配置。 – 2010-03-14 11:01:24

+0

我认为有一个额外的线程运行缓冲区,它没有得到任何时间片或待定时器,这将需要(一些)时间。你尝试了一些Threading.Thread.Speep(x)? – 2011-04-05 18:39:46

回答

0

您可以调用BufferingTargetWrapper并强制它写入日志。奇怪的是LogManger.Flush不起作用。

var buffWapper = 
      LogManager.Configuration.FindTargetByName("BufferingTargetWrapper") as BufferingTargetWrapper; 
     if (buffWapper != null) 
      buffWapper.Flush(); 

var buffWapper = 
      LogManager.Configuration.FindTargetByName("BufferingTargetWrapper") as BufferingTargetWrapper; 
     if (buffWapper != null) 
     { 
      buffWapper.BufferSize = 1; 
      buffWapper.Flush(); 
     }