How to tell why an IIS application pool is recycled

后端 未结 4 1240
走了就别回头了
走了就别回头了 2020-12-25 14:31

Background:

I\'ve deployed an ASP.NET MVC 3 app that works on my machine to a shared hosting provider and am discovering some problems that appear to be related to

4条回答
  •  [愿得一人]
    2020-12-25 15:13

    This is a really late answer, but I hope it can provide extra insight for those having similar problems (IIS 7.x or above).

    1. Finding when application pool is starting to shutdown - the following code can be used to find out when the application pool start its shutdown. Actual shutdown occurs in a maximum of Shutdown limit (seconds, default 90) after this event.

    public class ApplicationPoolService : IApplicationPoolService
    {
        public bool IsShuttingDown()
        {
            return System.Web.Hosting.HostingEnvironment.ShutdownReason != ApplicationShutdownReason.None;
        }
    
        public ApplicationShutdownReason GetShutdownReason()
        {
            return System.Web.Hosting.HostingEnvironment.ShutdownReason;
        }
    }
    
    public class HostingEnvironmentRegisteredObject : IRegisteredObject
    {
        public void Stop(bool immediate)
        {
            // second call is done when the Stop is imminent 
            if (immediate)
                return;
    
            var reason = appPoolService.GetShutdownReason().ToString();
            logger.Log(LogLevel.Info, $"HostingEnvironmentRegisteredObject.stop called with shutdown reason {reason}");
        }
    }
    
    // this code should be placed in global.asax.cs
    protected void Application_Start()
    {
        HostingEnvironment.RegisterObject(new HostingEnvironmentRegisteredObject());
    }
    

    This helps to find the general reason and exactly when it was triggered. In your case, I think HostingEnvironment is the value. Unfortunately, the underlying cause is not unique. It can be periodic recycle, recycle due to memory limit (most probable reason in OP's question), recycle due to fixed hour etc.

    2. Finding the exact cause - one way to find out the exact cause is to search for it in the EventLog. If this is not accessible, it can be requested from the hosting provider by providing the following details to narrow their search.

    • Exact time of shutdown initiation
    • Event log filter:
      • Event sources = WAS
      • Event level = Information
      • Logged = custom range including exact time of shutdown +/- 1 minute or so

    Event log should return more relevant information like the ones below:

    A worker process with process id of 'xxx' serving application pool 'xxx' has requested a recycle because it reached its scheduled recycle time.


    A worker process with process id of 'xxx' serving application pool 'xxx' has requested a recycle because it reached its virtual memory limit.

提交回复
热议问题