ASP .NET Core IIS Support开发时常挂起,如何解决及启用日志排查?
Hey there, I’ve dealt with similar frustrating hangs using ASP.NET Core + IIS Express during development, so I totally get how annoying it is to have to kill the dotnet process every time. Let’s walk through both immediate fixes for the hangs and how to enable proper logging to track down the root cause for good.
Fixing the Hang Issues (Immediate Workarounds & Root Cause Checks)
Here are the most common fixes I’ve used to resolve hangs quickly:
- Track down stuck requests/background tasks:More often than not, hangs come from a long-running request (like an unoptimized DB query with no timeout) or an async task deadlock. Fire up Visual Studio’s
Debug > Windows > Threadswindow to check thread states—look for threads stuck inWaitSleepJoinand inspect their call stacks to see where they’re hanging. Also, double-check that you’re usingawaitproperly everywhere (avoid.Resultor.Wait()on async methods, as these are frequent deadlock culprits). - Disable IIS Express idle timeout:By default, IIS Express will shut down idle processes, but sometimes this logic can glitch and cause unexpected hangs. Open your project’s
applicationhost.config(usually in.vs\YourProjectName\config), find your site’s application pool, and setidleTimeoutto00:00:00to disable idle shutdown:<applicationPools> <add name="YourAppPoolName" idleTimeout="00:00:00" /> </applicationPools> - Update your tools:Old versions of ASP.NET Core SDK or IIS Express often have known hang bugs. Grab the latest stable patch for your .NET version (e.g., .NET 6/7/8) and make sure IIS Express is up to date—this alone fixes a ton of weird, unexplained issues.
- Reset IIS Express and clear cached config:Sometimes IIS Express gets into a wonky state. Right-click the IIS Express icon in your taskbar, select "Stop Site", then restart your project. If that doesn’t work, delete the
.vsfolder (back up any custom configs first!) to force a fresh config generation. - Check for file lock conflicts:File locks from static assets, config files, or even third-party tools (like antivirus scanners or cloud sync apps) can hang the dotnet process. Use a tool like Process Explorer to search for locked files in your project directory and see if they’re being held by dotnet or another app.
Enabling Logging to Diagnose the Root Cause
Once you’ve got immediate fixes in place, set up logging to figure out why the hangs are happening in the first place.
ASP.NET Core Application Logging
- Tweak logging levels in
appsettings.Development.json:Crank up the verbosity for IIS-related logs to capture detailed interactions:{ "Logging": { "LogLevel": { "Default": "Debug", "Microsoft.AspNetCore": "Debug", "Microsoft.AspNetCore.Server.IIS": "Trace" }, "Console": { "IncludeScopes": true }, "Debug": { "LogLevel": { "Default": "Trace" } } } } - Ensure logging is enabled in
Program.cs:Make sure console and debug logging are set up so you can see logs in Visual Studio’s Output window (select "ASP.NET Core Web Server" from the dropdown):var builder = WebApplication.CreateBuilder(args); // Add console and debug logging builder.Logging.AddConsole(); builder.Logging.AddDebug(); // Filter to get extra IIS integration details builder.Services.AddLogging(logging => { logging.AddFilter("Microsoft.AspNetCore.Server.IIS", LogLevel.Trace); }); // Rest of your config... - Use structured logging (optional but powerful):For easier debugging, I recommend adding Serilog to write logs to files or a centralized tool. Install the
Serilog.AspNetCoreNuGet package, then configure it inProgram.cs:using Serilog; Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogLevel.Information) .MinimumLevel.Override("Microsoft.AspNetCore.Server.IIS", LogLevel.Trace) .WriteTo.Console() .WriteTo.File("logs/app.log", rollingInterval: RollingInterval.Day) .CreateLogger(); var builder = WebApplication.CreateBuilder(args); builder.Host.UseSerilog(); // Replace default logging with Serilog // Rest of your config...
IIS Express Logs
- Enable request logs:Track every request’s duration and status to spot slow or stuck requests. In
applicationhost.config, add alogFileentry to your site’s configuration:
Logs will be saved to<site name="YourSiteName" id="1"> <application path="/"> <virtualDirectory path="/" physicalPath="YourProjectPath" /> </application> <bindings> <!-- Your bindings --> </bindings> <logFile logFormat="W3C" directory=".\logs" enabled="true" /> </site>.vs\YourProjectName\logsin W3C format. - Enable Failed Request Tracing:This is critical for debugging hangs—it captures a step-by-step breakdown of requests that take too long. In
applicationhost.config, add a trace rule under<system.webServer><tracing>:
This will generate a detailed trace log for any request that takes over 1 minute. You can find the logs in<tracing> <traceFailedRequests> <add path="*"> <traceAreas> <add provider="ASP.NET" areas="Infrastructure,Module,Page,AppServices" verbosity="Verbose" /> <add provider="WWW Server" areas="Authentication,Security,Filter,StaticFile,CGI,Compression,Cache,RequestNotifications,Module" verbosity="Verbose" /> </traceAreas> <failureDefinitions timeTaken="00:01:00" statusCodes="200-599" /> </add> </traceFailedRequests> </tracing>.vs\YourProjectName\logs\FailedReqLogFiles—open the XML file in a browser to see exactly where the request got stuck.
内容的提问来源于stack exchange,提问作者Alexandr Sulimov
相关产品推荐
相关产品推荐

