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) #2999

Closed
dotnetshadow opened this Issue Feb 3, 2016 · 5 comments

Comments

Projects
None yet
5 participants
@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

This comment has been minimized.

Show comment
Hide comment
@NickCraver

NickCraver Feb 4, 2016

Member

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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@janvorli

janvorli Feb 4, 2016

Member

@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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@NickCraver

NickCraver Feb 4, 2016

Member

@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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@terrajobst
Member

terrajobst commented Feb 25, 2016

Following our API review process I've filed dotnet/corefx#6398.

@sergiy-k sergiy-k modified the milestones: 1.0.0-rtm, 1.0.0-rc2 Mar 23, 2016

@sergiy-k sergiy-k added the area-VM label Apr 29, 2016

@sergiy-k sergiy-k modified the milestones: 1.1.0, 1.0.0-rtm May 4, 2016

@janvorli

This comment has been minimized.

Show comment
Hide comment
@janvorli

janvorli Sep 26, 2016

Member

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

Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment