一、.net core 發布后的站點啟動報錯如下
An error occurred while starting the application.
.NET Core 4.6.26328.01 X64 v4.0.0.0 | Microsoft.AspNetCore.Hosting version 2.0.3-rtm-10026 | Microsoft Windows 10.0.16299 | Need help?
二、解決辦法,先打開iis日志。web.config 修改如下(重點背景色黃色)
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" /> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="dotnet" arguments=".\GP.Core.WebAPI.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" /> </system.webServer> </configuration>
一般情況下我們發布后,都會存在這段代碼,但是要把 stdoutLogEnabled="false"
換成 stdoutLogEnabled="true" 才能在根目錄記錄文本日志,查看異常原因。log日志文件夾需要手動創建!
三、刷新站點頁面,查看發布根目錄發現多了一個logs文件夾,打開里面的stdout_xxx.log文件,打開就可以看到我們站點啟動失敗的原因(eg:某些日志文件nlog.config或者文檔xml等沒有復制輸出到發布目錄)。

Application startup exception: System.IO.FileNotFoundException: Could not find file 'G:\FTP\DialysisWebApi\Dialysis.WebApi.xml'. File name: 'G:\FTP\DialysisWebApi\Dialysis.WebApi.xml' at System.IO.Win32FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent) at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.Xml.XmlSystemPathResolver.GetEntity(Uri uri, String role, Type typeOfObjectToReturn) at System.Xml.XmlTextReaderImpl.FinishInitUriString() at System.Xml.XmlTextReaderImpl..ctor(String uriStr, XmlReaderSettings settings, XmlParserContext context, XmlResolver uriResolver) at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext) at System.Xml.XmlReader.Create(String inputUri) at System.Xml.XPath.XPathDocument..ctor(String uri, XmlSpace space) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions.<>c__DisplayClass24_0.<IncludeXmlComments>b__0() at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions.CreateSwaggerProvider(IServiceProvider serviceProvider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass3_0.<UseMiddleware>b__0(RequestDelegate next) at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build() at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() Hosting environment: Production Content root path: G:\FTP\DialysisWebApi Now listening on: http://localhost:22365 Application started. Press Ctrl+C to shut down.
四、因為項目在startup的時候沒有讀到nlog.config這個文件導致的錯誤。解決辦法:
在開發環境中,把這個文件的屬性配置成 “生成操作:內容”和 “復制到輸出目錄:始終復制” 就可以了。