2016-12-19 36 views
0

我希望能夠通過配置log4net記錄未處理的異常。 只需對配置文件進行更改就可以實現嗎?如果是的話,該怎麼做? 否則,如果這是不可能的,我應該在代碼中實現什麼來實現這一點?如何配置Log4Net以記錄未處理的異常

+0

不同類型的應用程序有不同的方法來設置一個未處理的異常處理程序,然後可以使用它來將異常記錄到log4net – stuartd

回答

0

你不能只用配置來做到這一點,你需要記錄實際的異常。所以你需要捕捉'未被捕捉的'異常並且記錄它們。

public static void Main(string[] args) 
{ 
    // Add the event handler for handling UI thread exceptions to the event. 
    Application.ThreadException += new  
    ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException); 

    // Set the unhandled exception mode to force all Windows Forms errors 
    // to go through our handler. 
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 

    // Add the event handler for handling non-UI thread exceptions to the event. 
    AppDomain.CurrentDomain.UnhandledException += new  
    UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 
} 

在處理程序中,您可以登錄到log4net。之後,您可以使用配置打開和關閉它們。

但是有些情況下,該異常沒有捕獲。例如,當有一個故障快速呼叫。