The Problem
If you’ve ever seen this message when hitting your ASP.NET Core app:
“An error occurred while starting the application. .NET Framework <version number> | Microsoft.AspNetCore.Hosting version <version number> | Microsoft Windows <version number>”
It looks a little something like this:
What Happened?
It basically means something really bad happened with your app. Some things that might have gone wrong:
- You might not have the correct .NET Core version installed on the server.
- You might be missing DLL’s
- Something went wrong in your Program.cs or Startup.cs before any exception handling kicked in
Event Viewer (probably) won’t show you anything
If you’re running on Windows and behind IIS, you might immediately go to the Event Viewer to see what happened based on your previous ASP.NET knowledge. You’ll notice that the error is not there. This is because Event Logging must be wired up explicitly and you’ll need to use the Microsoft.Extensions.Logging.EventLog package, and depending on the error, you might not have a chance to even catch it to log to the Event Viewer.
How to figure what happened (if running on IIS)
Instead of the Event Viewer, if you’re running behind IIS, we can log the request out to a file. To do that:
- Open your web.config
- Change stdoutLogEnabled=true
- Create a logs folder
- Unfortunately, the AspNetCoreModule doesn’t create the folder for you by default
- If you forget to create the logs folder, an error will be logged to the Event Viewer that says: Warning: Could not create stdoutLogFile \\?\YourPath\logs\stdout_timestamp.log, ErrorCode = -2147024893.
- The “stdout” part of the value “.\logs\stdout” actually references the filename not the folder. Which is a bit confusing.
- Unfortunately, the AspNetCoreModule doesn’t create the folder for you by default
- Run your request again, then open the \logs\stdout_*.log file
Note – you will want to turn this off after you’re done troubleshooting, as it is a performance hit.
So your web.config’s aspNetCore element should look something like this
<aspNetCore processPath=”.\YourProjectName.exe” stdoutLogEnabled=”true” stdoutLogFile=”.\logs\stdout” />
Doing this will log all the requests out to this file and when the exception occurs, it will give you the full stack trace of what happened in the \logs\stdout_*.log file
Hope this helps. This happened to me a friend of mine. A friend. Yep. Definitely not me. My apps would never bomb.
I think there is a simpler solution, just add .CaptureStartupErrors(true) to WebHostBuilder : https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosting#configuring-a-host . It helped me for asp.net core in azure
LikeLiked by 3 people
Thanks for the suggestion Gregory! That’s a good find.
LikeLike
I’ve got the same ugly error. Just add logs file and it’s running as well ! Thanks for you article !
LikeLiked by 1 person
Thanks for the comment!
LikeLike
Thnx Scott Sauber. It save my lots of time to figure out real issue.
LikeLiked by 1 person
Awesome! Thanks for letting me know this helped.
LikeLike
Hey thanks a lot Scott! You just saved my day. Keep up the good work
LikeLike
Thanks for letting me know, Andrew! Glad this helped you out.
LikeLike
[…] How to troubleshoot: “An error occurred while starting the application” in ASP.NET Core … […]
LikeLike
com/en-us/aspnet/core/fundamentals/hosting#configuring-a-host . Thanks for you article !
LikeLiked by 1 person
Many thanks for your post! Even mid-way through 2018 and this post helps.
LikeLiked by 1 person
Thanks for the comment. Glad it helps.
LikeLike
If you’re hosting this on a server that you can access, another way to find startup errors is by running `dotnet YourAspNetCoreProject.dll` from a command prompt.
LikeLiked by 1 person
I cannot being to express how much of a life saver your article was. Something as simple as outputting the log saved me as our production system was down and there’s nothing in any log that is descriptive of what is happening, but the kestrel output was perfect. Thanks again!
LikeLiked by 1 person
Thanks for letting me know, always happy to hear it was useful.
LikeLike
[…] Scott Sauber: How to troubleshoot: “An error occurred while starting the application” in ASP.NET… […]
LikeLike
SUPERRR.. thanks, that was really helpful.
LikeLiked by 1 person
Glad it helped! Thanks for letting me know.
LikeLike
you saved me!!! thats great article
LikeLiked by 1 person
Thanks for letting me know it helped, Viki, I appreciate it.
LikeLike
Great article, thanks!
LikeLiked by 1 person
Hey Radek – thanks for letting me know it helped! Glad it was useful.
LikeLike
how do you go about this if you are hosting it on AppHarbor
LikeLiked by 1 person
Sorry for the late response Tayo… I’m not really sure to be honest as I’m not familiar with AppHarbor. If they let you throttle up the logging some how that seems like the best bet. If you found a solution it’d be awesome to share it so the next person may benefit!
LikeLike
Scott you saved my day too. Also see this article too: https://odetocode.com/blogs/scott/archive/2018/07/16/7-tips-for-troubleshooting-asp-net-core-startup-errors.aspx
LikeLiked by 1 person
Awesome – thanks for letting me know and thanks for the additional link – Scott Allen is a wizard.
LikeLike
[…] And if configuring swagger wasn’t the solution, you can either check one of the links below, or just troubleshoot your app. […]
LikeLike
Thanks for this, I had to change permissions on the logs folder as well, I just gave “everyone” full control.
stdout stacktrace was helpful to pinpoint exact line number causing the problem.
LikeLiked by 1 person
Thanks a lot…
LikeLiked by 1 person
Hey Scott,
It still didn’t help to solve any issue. I have created the logs folder, however it doesn’t create any file inside the folder. On other hand the same problem still is there.
LikeLike
Hi Farid – Just checking, you changed the web.config file too right? The other thing to check would be permissions that the user the app pool is running under has permissions to create files.
LikeLike
Thanks,
This made me figure out that there was something wrong with my navsettings.json file.
LikeLiked by 1 person
Glad to hear it helped, Peter! Thanks for letting me know.
LikeLike
[…] Open web.config under the website < aspNetCore processPath=”dotnet” arguments=”.xxx.dll” stdoutLogEnabled=”false” stdoutLogFile=”.logsstdout” /> Instead of < aspNetCore processPath=”dotnet” arguments=”.xxx.dll” stdoutLogEnabled=”true” stdoutLogFile=”.logsstdout” /> The logs directory is then used to see the cause and then to resolve it. If there are no logs in the root directory of the site, to create them manually, see the link in the original article. When the record finds the cause and resolves, change the above true back to false. Example 1: Dependency file missing Application startup exception: System.ArgumentException: The directory name D:wwwRootnode_modules is invalid. Example 2. Database connection error Application startup exception: System. The Data. SqlClient. SqlException x80131904 (0) : user ‘sa’ login failed. ats System. The Data. SqlClient. SqlInternalConnectionTds.. ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling) The original link How to troubleshoot: “An error occurred while starting the application” in ASP.NET Core … […]
LikeLike
So I checked that stdoutLogEnabled= true, and it is.
But in the log file does not get created, and in Event Logs, I get this warning:
Warning: Could not create stdoutLogFile C:\inetpub\wwwroot\nopCommerce\logs\stdout_7712_2021111195224.log, ErrorCode = -2147024891.
Do you have an idea as to why it is not creating the file?
LikeLike
Did you create a logs folder too in C:\inetpub\wwwroot\nopCommerce after flipping that flag? If so, then I would check to make sure the App Pool User has write file permissions to that folder.
LikeLike
Thank you.
LikeLike
No problem Peterson. Glad it helped!
LikeLike