Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASP.NET Core 1.0 Global exception handler in console application (DNX) #5059

Closed
dotnetshadow opened this issue Feb 3, 2016 · 5 comments
Closed
Assignees
Milestone

Comments

@dotnetshadow
Copy link

@dotnetshadow dotnetshadow commented Feb 3, 2016

How do you catch global exceptions in an asp.net core console application? In previous versions we had AppDomain.CurrentDomain.UnhandledException but this doesn't seem to work in rc1

public class Program
{
    [STAThread]
    public static void Main(string[] args)
    {           
        #if !DNXCORE50
        // Register unhandled exception handler
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);

        // Throw exception
        throw new Exception("1");

        #endif            
        Console.ReadLine();
    }



    #if !DNXCORE50


    /// <summary>
    ///  Catch all unhandled exceptions in all threads.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
    {
        try
        {
            // Get the exception that was thrown
            Exception exceptionThrown = (Exception)args.ExceptionObject;

            Console.WriteLine("Information coming from exception handler");                                
        }
        catch (Exception ex)
        {                
            Console.WriteLine(ex.ToString());                
        }
    }
    #endif
}

The above only works in 4.5.1 but not dnxcore50, As soon as the exception is thrown the code breaks. Is there any way in a dnx app to catch unhandled exceptions?

@NickCraver
Copy link
Member

@NickCraver NickCraver commented Feb 4, 2016

I'm also curious what the mechanism is as well. We have global exception handlers that attach this way and log to a central location - some equally global capture method is really needed in .Net Core.

@janvorli
Copy link
Member

@janvorli janvorli commented Feb 4, 2016

@NickCraver, @dotnetshadow, there is currently no way to get event on unhandled exceptions in the .Net Core, mostly due to the fact that there is no AppDomain class, so the event has disappeared with it.
But I agree that it is a useful feature that we should put back. I am currently working on figuring out where to put such event.

@NickCraver
Copy link
Member

@NickCraver NickCraver commented Feb 4, 2016

@janvorli Totally understand on the app domain side; I think we all only care for the existence of the event not particularly where it lives. I don't really have a good solution for placement in mind but happy to help if we can.

@terrajobst
Copy link
Member

@terrajobst terrajobst commented Feb 25, 2016

@janvorli
Copy link
Member

@janvorli janvorli commented Sep 26, 2016

Closing this as the AppDomain.CurrentDomain.UnhandledException was added back in dotnet/corefx#11275

@janvorli janvorli closed this Sep 26, 2016
@msftgits msftgits transferred this issue from dotnet/coreclr Jan 30, 2020
@msftgits msftgits added this to the 1.1.x milestone Jan 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.