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 lose/gain any chances of trapping exceptions without this?
By adding try/catch in Program.cs you will end up catching app's start-up exceptions which i don't believe you should handle those exceptions because in any case if your app is not able to start so what will be the point . Regarding to the App domain exception handling the answer is no , you don't need to handle them due to dotnet core built-in exception handling as mentioned in the document any exception that your app is not handling is being handled by the dotnet itself . In short , let the app have a clean start-up and let it throw it's exceptions and then you can always check the dotnet run 's console for any exceptions .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.