Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core Global exception handler in console application

I'm porting an console application to .NET Core, and I'm trying to replace this line:

AppDomain.CurrentDomain.UnhandledException += UnhandledException;

After reading this, it seems there is no built-in way to do this.

So my question: is the only way to replace this line surrounding my entire code with a try/catch?

By reading this, it seems like there is another way, namely by keep on using System.AppDomain, but I can't seem to find this class/method. My only guess was this library, but it clearly states that it should not be used if possible, so I would like not to.

like image 569
Romain Vergnory Avatar asked Apr 26 '17 16:04

Romain Vergnory


People also ask

How do you handle exceptions globally in .NET Core?

Use the UseExceptionHandler middleware in ASP.NET Core So, to implement the global exception handler, we can use the benefits of the ASP.NET Core build-in Middleware. A middleware is indicated as a software component inserted into the request processing pipeline which handles the requests and responses.


1 Answers

You're right, the AppDomain.UnhandledException or it's analog will be available only in .Net Core 2.0, so for now you should either wait or add additional try/catch blocks. However, if you're using the tasks, you can use TaskScheduler.UnobservedTaskException, which is available from first version of .Net Core.

like image 103
VMAtm Avatar answered Sep 18 '22 09:09

VMAtm