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:
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.
43 thoughts on “How to troubleshoot: “An error occurred while starting the application” in ASP.NET Core on IIS”
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
Thanks for the suggestion Gregory! That’s a good find.
I’ve got the same ugly error. Just add logs file and it’s running as well ! Thanks for you article !
Thanks for the comment!
Thnx Scott Sauber. It save my lots of time to figure out real issue.
Awesome! Thanks for letting me know this helped.
Hey thanks a lot Scott! You just saved my day. Keep up the good work
Thanks for letting me know, Andrew! Glad this helped you out.
[…] How to troubleshoot: “An error occurred while starting the application” in ASP.NET Core … […]
com/en-us/aspnet/core/fundamentals/hosting#configuring-a-host . Thanks for you article !
Many thanks for your post! Even mid-way through 2018 and this post helps.
Thanks for the comment. Glad it helps.
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.
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!
Thanks for letting me know, always happy to hear it was useful.
[…] Scott Sauber: How to troubleshoot: “An error occurred while starting the application” in ASP.NET… […]
SUPERRR.. thanks, that was really helpful.
Glad it helped! Thanks for letting me know.
you saved me!!! thats great article
Thanks for letting me know it helped, Viki, I appreciate it.
Great article, thanks!
Hey Radek – thanks for letting me know it helped! Glad it was useful.
how do you go about this if you are hosting it on AppHarbor
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!
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
Awesome – thanks for letting me know and thanks for the additional link – Scott Allen is a wizard.
[…] And if configuring swagger wasn’t the solution, you can either check one of the links below, or just troubleshoot your app. […]
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.
Thanks a lot…
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.
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.
This made me figure out that there was something wrong with my navsettings.json file.
Glad to hear it helped, Peter! Thanks for letting me know.
[…] 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 … […]
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?
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.
No problem Peterson. Glad it helped!
Finally got the logs to create (on non-published site after all the permissions changes – doesn’t work on Published DotNetCore 2.2 sites), however, all the log files are completely empty. IIS should have it’s OWN logging system, but MS likes to see us pull our hair out for hours.
[…] take a look at this post: https://scottsauber.com/2017/04/10/how-to-troubleshoot-an-error-occurred-while-starting-the-applicat… which will give you a good starter kit to get basic log informations when this kind of error […]
[…] Example 1: Missing dependency file 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): Login for user ‘sa’ failed. system ats. The data. SqlClient. SqlInternalConnectionTds.. ctor(identity DbConnectionPoolIdentity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling) The original bond Troubleshooting: “An error occurred while starting the application” in ASP.NET Core on I… […]
I get this error, and in the logs this is the thrown exception.
An unhandled exception was thrown by the application.
System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst, Int32 dstOffset, Int32 count)
at System.IO.MemoryStream.Write(Byte buffer, Int32 offset, Int32 count)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Write(String value)
at Microsoft.Extensions.RazorViews.BaseView.ExecuteAsync(HttpContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
I can’t find the stack trace, not sure how to fix this. Please help
Just a heads up, if you are using a build agent like TeamCIty, make sure the builds are clean, even if they are clean try building another instance of the project and run it again. At least that’s what happened to me, and the errors that ASP.Net threw were very vague and without a stack trace, I have put the error in case there is a similar one.