ASP.NET启动设置:IIS Express与Project差异及日志重复问题咨询
Hey there! Let me break down the key differences between these two launch options, and explain exactly why your logging is acting up depending on which one you use.
Core Differences
1. Hosting Environment & Process Count
- IIS Express: This is a lightweight, local version of IIS that acts as an out-of-process host. When you launch your app with this option, you’re actually running two main processes:
iisexpress.exe: The IIS Express proxy/host process that handles web requests and routes them to your app.- Your app’s process: For .NET Framework, this is
w3wp.exe(the IIS application pool process); for .NET Core/.NET 5+, it’sdotnet.exerunning your app directly.
Both processes can end up loading your logging component if it’s registered in a shared context.
- Project (Self-Hosted): This launches your app directly as a standalone process (no intermediate IIS proxy). There’s only one process running your application, so your logging component is initialized exactly once.
2. Startup & Configuration Control
- IIS Express: Uses IIS’s configuration system (like
applicationhost.configin your.vsfolder). Your app’s behavior is governed by IIS rules—think application pool settings, pipeline mode, and request routing handled by the IIS proxy. You have less direct control over the startup process compared to self-hosting. - Project Launch: Pulls configuration directly from your app’s
appsettings.json(orweb.configfor .NET Framework) and uses the startup logic you defined inProgram.cs/Startup.cs. You set the environment variables, launch arguments, and runtime behavior directly through Visual Studio’s launch settings.
3. Debugging Experience
- IIS Express: Since there are multiple processes, debugging might require attaching to both the IIS proxy and your app process if you need to trace request flow through the proxy.
- Project Launch: Debugging is simpler—you only need to attach to your single app process, making it easier to trace code execution end-to-end.
Why Your Logs Are Duplicating with IIS Express
The root cause here is multiple processes initializing your logging component. When using IIS Express, both the iisexpress.exe proxy and your app’s main process load your logging library (especially if the library is referenced in a shared assembly or registered globally). Each process writes the same log entries independently, leading to duplicates.
With Project launch, only your app’s main process runs, so the logging component is initialized once, and each log entry is written exactly once.
Fixes to Try
- Restrict Logging Initialization: Make sure your logging component is only initialized in your app’s startup code (e.g.,
Program.csfor .NET Core) and not in shared libraries that might be loaded by the IIS proxy process. - Filter Logs by Process: Update your logging configuration to filter out logs from the
iisexpress.exeprocess. Most logging libraries (like Serilog, NLog, or Microsoft.Extensions.Logging) let you add filters based on process name. - Check IIS Express App Pool Settings: Open your
.vs\config\applicationhost.configfile and verify your app pool isn’t configured for multiple worker processes or rapid recycling, which could spawn additional app processes.
内容的提问来源于stack exchange,提问作者nubi

