简体   繁体   中英

ASP.NET Core 2.2 App Service hosted on Azure returning 500 without an exception being thrown

I have developed an App Service backend hosted on Azure using ASP.NET Core 2.2 . From time to time I'm getting a 500 status but I'm not able to find out where is that 500 being generated.

I'm using application Insights and I see on Failed Requests that sometimes there are some calls to my App Service which are returning 500.

If I reproduce that same call with same values with Postman I'm not getting any 500. There are also some End Client Apps making requests to this backend. I believe it's happening to them but why I can't reproduce the same result if I'm performing the same request with same values.

I know that 500 implies "Internal Server Error" and that it is related to the Application code but I've even configured my App Service to use a Global Error handling but I never get that 500 when I'm executing the same request that I see on Application Insights.

I have set on my Configure on StartUpp class the global error handling (just to show you that I have also enabled that)

app.UseExceptionHandler(config => ExceptionHandlingSettings.ConfigureGlobalExcpetionHandling(config));

I would like to know where or how to get more information in order to find out the code generating the error/exception because I guess there is some bug in my code which throws an unhandled exception.

Here are couple of steps which you can do to understand the root cause:

1- Change stdoutLogEnabled="false" to true and then check the logs at stdoutLogFile=".\\logs\\stdout". The error(s) there might tell you something.

The ASP.NET Core Module stdout log often records useful error messages not found in the Application Event Log. To enable and view stdout logs:

  • Navigate to the Diagnose and solve problems blade in the Azure portal.
  • Under SELECT PROBLEM CATEGORY, select the Web App Down button.
  • Under Suggested Solutions > Enable Stdout Log Redirection, select the button to Open Kudu Console to edit Web.Config.
  • In the Kudu Diagnostic Console, open the folders to the path site > wwwroot. Scroll down to reveal the web.config file at the bottom of the list.
  • Click the pencil icon next to the web.config file.
  • Set stdoutLogEnabled to true and change the stdoutLogFile path to: \\?\\%home%\\LogFiles\\stdout.
  • Select Save to save the updated web.config file.
  • Make a request to the app.
  • Return to the Azure portal. Select the Advanced Tools blade in the DEVELOPMENT TOOLS area. Select the Go→ button. The Kudu console opens in a new browser tab or window.
  • Using the navigation bar at the top of the page, open Debug console and select CMD.
  • Select the LogFiles folder.
  • Inspect the Modified column and select the pencil icon to edit the stdout log with the latest modification date.
  • When the log file opens, the error is displayed.

Please make sure to remove the logging once the inspection is completed.

2 - Use Middleware for handling erroneous request like app.UseDeveloperExceptionPage();

3- You can add application Insight in your app and try to put Info logging to understand what part of your code is causing the failure.

For additional reference, please check below doc:

https://docs.microsoft.com/en-us/aspnet/core/test/troubleshoot-azure-iis?view=aspnetcore-3.0

https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-exceptions

Hope it helps.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM