How to troubleshoot: “An error occurred while starting the application” in ASP.NET Core on IIS

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:

2017-04-07_15-20-55

 

What Happened?

It basically means something really bad happened with your app.  Some things that might have gone wrong:

  1. You might not have the correct .NET Core version installed on the server.
  2. You might be missing DLL’s
  3. 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:

  1. Open your web.config
  2. Change stdoutLogEnabled=true
  3. Create a logs folder
    1. Unfortunately, the AspNetCoreModule doesn’t create the folder for you by default
      1. 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.
    2. The “stdout” part of  the value “.\logs\stdout” actually references the filename not the folder.  Which is a bit confusing.
  4. 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

2017-04-07_15-35-45.png

 

2017-04-07_15-42-40.png

 

 

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

  1. I’ve got the same ugly error. Just add logs file and it’s running as well ! Thanks for you article !

  2. 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.

  3. 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!

    • 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!

  4. 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.

  5. 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.

    • 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.

  6. […] 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 … […]

  7. 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.

  8. 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.

  9. […] 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… […]

  10. 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.AspNetCore.Hosting.Views.ErrorPage.ExecuteAsync()
    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

  11. 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.

Leave a Reply