After all, the person is not God, who wrote the program will have a bug, the bug is not scary, terrible is wrong, you do not know where the error. So we need to keep a record of all the exceptions thrown in the application, or we'll make a mistake and find a problem that will kill you. The main discussion below is how to catch a global exception. Basically the idea of capturing global exceptions in WinForm or the web is the same, adding the exception-caught code to the global Application object and writing it to the log file.
I. Capturing global exceptions in a WinForm program
In Winfrom we need to understand the two events in the Application object
①application.threadexception Event- occurs when an exception in the UI thread is not captured.
②appdomain.unhandledexception Event- occurs when an exception in a non-UI thread is not captured.
We need to set the catch code for the exception in Program.cs, as shown in. The Loghelper class is a custom log helper class that has been involved in several previous articles.
The code that needs to be added in Program.cs is as follows
Using system;using system.collections.generic;using system.linq;using system.windows.forms;using System.Text;using Common;namespace testlog4n{Static Class Program {///<summary>//The main entry point of the application. </summary> [STAThread] static void Main () {Bindexceptionhandler ();//exception Handling in binding program Application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (FALSE); Application.Run (New Form1 ()); }///<summary>///The exception handling in the binder///</summary> private static void Bindexceptionhan Dler () {//Set application handling Exception mode: ThreadException processing Application.setunhandledexceptionmode (unhandledexce Ptionmode.catchexception); Handling UI Thread Exceptions Application.ThreadException + = new System.Threading.ThreadExceptionEventHandler (application_threadex Ception); Handling uncaught Exceptions AppDomain.CurrentDomain.UnhandledException + = New Unhandledexceptioneventhandler (currentdomain_unhandledexception); }///<summary>//Handling UI thread exception///</summary>//<param name= "Sender" ></pa ram>//<param name= "E" ></param> static void Application_threadexception (object sender, Syst Em. Threading.threadexceptioneventargs e) {loghelper.errorlog (null, e.exception as Exception); }///<summary>//handling uncaught exceptions///</summary>//<param name= "Sender" ></pa ram>//<param name= "E" ></param> static void Currentdomain_unhandledexception (object sender, UnhandledExceptionEventArgs e) {loghelper.errorlog (null, e.exceptionobject as Exception); } }}
Sample code Download
Second, catch global exceptions in the Web
We just need to add the code for the exception capture in the Global.asax file.
The full Global.asax code is shown below
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Security;using System.web.sessionstate;using common;namespace webapplication_testlog4net{public class Global: System.Web.HttpApplication {void Application_Start (object sender, EventArgs e) {//In application startup fortune Line code} void Application_End (object sender, EventArgs e) {//code to run when application shuts down} void Application_Error (object sender, EventArgs e) {//code that runs when an unhandled error occurs Exception Objexp = HttpContext.Current.Server.GetLastError (); Loghelper.errorlog ("<br/><strong> client ip</strong>:" + request.userhostaddress + "<br/>< strong> error address </strong>: "+ Request.url, objexp); } void Session_Start (object sender, EventArgs e) {//code run at new session startup} void Sessio N_end (object sender, EventArgs e) {//code to run at the end of the session。 Note: The Session_End event is raised only if the sessionstate mode in the Web. config file is set to//InProc. If the session mode is set to StateServer//or SQL Server, the event is not raised. } }}
Sample program Download
Iii. capturing global exceptions in WPF
We just need to add the code for the exception capture in the App.xaml file.
Capturing global exceptions in WPF is primarily related to the following two events
1.Appdomain.unhandledexception Event -occurs when an exception is not captured. The main point is the non-UI thread.
2,application.dispatcherunhandledexception event --If the exception was thrown by the application but not processed, occurs. The main point is the UI thread.
The complete App.xaml file is shown below
Using system;using system.collections.generic;using system.configuration;using system.data;using System.Linq;using System.windows;using common;namespace wpfapplication1{///<summary>//App.xaml interactive logic///</SUMMARY> ; Public partial class App:application {public App ()} {this. Dispatcherunhandledexception + = new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler (app_ Dispatcherunhandledexception); AppDomain.CurrentDomain.UnhandledException + = new Unhandledexceptioneventhandler (currentdomain_unhandledexception ); } void Currentdomain_unhandledexception (object sender, UnhandledExceptionEventArgs e) {i F (E.exceptionobject is System.Exception) {Loghelper.errorlog (null, (System.Exception) E.excepti Onobject); }} public static void HandleException (Exception ex) {loghelper.errorlog (NULL,EX); }void App_dispatcherunhandledexception (object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {e.handled = true; Loghelper.errorlog (null, e.exception); } }}
Sample code Download
Applying Log4net (iv) in C # code to catch global exceptions in WinForm and the Web