Skip to content

dotnet core web api exception handling - unhandled exceptions #2950

Closed
@gitfortee

Description

@gitfortee

The Main method in Program.cs in my dotnet core web api project has the following code.

public static void Main(string[] args)
{
    var logger = LogManager.LoadConfiguration("nlog.config").GetCurrentClassLogger();
    try
    {
        BuildWebHost(args).Run();
    }
    catch (Exception ex)
    {
        //NLog: catch setup errors
        logger.Error(ex, "An exception occurred during application start.");
        throw;
    }
}  

I also setup an exception handling middleware like this:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseResponseCompression();
    app.UseMiddleware(typeof(HandleExceptionMiddleware));
    app.UseMvc();
}

Should i also add a AppDomain.CurrentDomain.UnhandledException ? to the Main method - since dotnet core web api is a console application?. Do I loose/gain any chances of trapping exceptions without this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions