-
C# & Find All Application ErrorsIT Story/C# & WPF 2019. 12. 12. 21:52반응형
Exception handling is a critical component of every software application. The last thing you want is your users to see weird errors, or even worse, your application keeps crashing. In this article, we are going to discuss how to find and catch all exceptions in C# applications. .NET provides several different ways to catch exceptions and view unhandled exceptions.
Topics in this article:
- Catching “First Chance Exceptions”
- ASP.NET Exception Handling
- Including MVC, Web API, WCF, & ASP.NET Core
- .NET Framework Exception Events
- Find all exceptions with Retrace and no code changes
- Windows Event Viewer
Catching “First Chance Exceptions”
If you want to find and catch every single exception in your application, you need to be aware of “first chance exceptions.” The .NET Framework provides an easy mechanism to subscribe to every single exception that is ever thrown in your code. This includes exceptions that are caught deep inside your code that never gets surfaced anywhere. Although you can’t technically “catch” them, you can subscribe to .NET Framework events so you can log them. Finding these exceptions is a great way to improve performance and eliminate weird application behaviors.
It is also important to note that first chance exceptions can include a lot of noise. Some exceptions happen because they are expected to, or may only happen when an application starts up as various warnings. Don’t be alarmed if you see some of this noise all of a sudden if you start logging these errors.
By subscribing to the event, you can potentially log every exception to help identify weird and hidden problems in your application. I don’t recommend doing this long term on a production application due to the sheer volume of noise and data it can cause. It is best used during development or to help find pesky production problems.
Setup this event handler at the start of your application. In Program.cs, Startup.cs or your Global.asax file.
AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) => { Debug.WriteLine(eventArgs.Exception.ToString()); };
If your application has a Global.asax, which is essentially a HttpApplication, you should setup events around unhandled exceptions. It is important to know that depending on if you are using MVC, Web API, Nancy, SerivceStack, WCF, etc, that not all exceptions may bubble up to your Global.asax error handler. Those specific web frameworks may also have their own error handling mechanisms. We will cover those below as well!
//Global.asax public class MvcApplication : System.Web.HttpApplication { protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); if (exception != null) { //log the error } } protected void Application_Start() { //may have some MVC registration stuff here or other code } }
반응형'IT Story > C# & WPF' 카테고리의 다른 글
C#으로 만든 Log Class (0) 2019.12.12 C# Tree, XML로 Save & Load (0) 2019.12.12 How to create REST service (0) 2019.12.12 How to select certain Elements (0) 2019.12.05 A Short History of Sorting in C# (0) 2019.12.05