in existing project we are just showing exception message to the users.
Now i want to log all exception messages in a log file( i can able to write log files)
but in my project we have many number of web pages and every page having many events, so for logging each and every catch block in all events is to much time consuming and i need to write the log code in all catch blocks in all pages.
so is there any option to globally write log message for all try catch events in all pages.
Member
375 Points
472 Posts
Global Exception Handling
Apr 29, 2018 01:30 PM|murali krishna14|LINK
Hi All,
I have an existing project, it is in asp.net web forms
in that project every event (page_load,button_click,dropdown_selectionIndexChanged etc.....) is calling in between try catch block like below
in existing project we are just showing exception message to the users.
Now i want to log all exception messages in a log file( i can able to write log files)
but in my project we have many number of web pages and every page having many events, so for logging each and every catch block in all events is to much time consuming and i need to write the log code in all catch blocks in all pages.
so is there any option to globally write log message for all try catch events in all pages.
it would be more helpful to me
Thanks,
Author in www.studyclues.com
Contributor
2891 Points
2598 Posts
Re: Global Exception Handling
Apr 29, 2018 02:35 PM|DA924|LINK
You can create a base class that all aspx.cs codebehind classes inherit from the base class.
http://www.4guysfromrolla.com/articles/041305-1.aspx
You can catch any exception globally.
https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.getlasterror(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
You can use Log4Net that can be installed using Nuget to log the exception.
https://www.c-sharpcorner.com/UploadFile/db2972/error-logging-using-log4net-in-web-application/
Other information...
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/aspnet-error-handling
All-Star
18265 Points
3594 Posts
Microsoft
Re: Global Exception Handling
May 01, 2018 06:38 AM|Nan Yu|LINK
Hi Murali,
Since you are able to write log file , the global place to handle uncatched Exceptions could be in Global.asax by handling Application_Error :
https://msdn.microsoft.com/en-us/library/24395wz3.aspx
For .NET framework, the most popular logging libraries are probably log4net and NLog. You can consider using that libraries.
Best Regards,
Nan Yu
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue.
Participant
1596 Points
766 Posts
Re: Global Exception Handling
May 02, 2018 12:50 PM|Khuram.Shahzad|LINK
Here you will find different solutions, I hope catching event at global level will help you, you need to throw error up from pages
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/aspnet-error-handling