NLog: Force BufferingTargetWrapper to empty on AppDomain UnhandledException

╄→гoц情女王★ 提交于 2019-12-08 01:34:31

问题


I have NLog configured in my application to to use the BufferingTargetWrapper for sending emails with the MailTarget.

The problem I'm running into is I can not find a way to force NLog to empty the BufferingTargetWrapper before the application exits from Unhandled Exceptions.

I tried calling LogManager.Flush() and LogManager.DisableLogging() from the Current App Domain's UnhandledException Event but it does not seam to work.

What would I need to do to make it send the emails?


回答1:


you can call the BufferingTargetWrapper and force it to write the logs. Its strange that the LogManger.Flush doesn't work.

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

or

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


来源:https://stackoverflow.com/questions/2413299/nlog-force-bufferingtargetwrapper-to-empty-on-appdomain-unhandledexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!