Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit b496410

Browse files
authored
Always log startup exceptions (#1217)
1 parent 8ef3e48 commit b496410

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -191,21 +191,26 @@ private RequestDelegate BuildApplication()
191191

192192
return builder.Build();
193193
}
194-
catch (Exception ex) when (_options.CaptureStartupErrors)
194+
catch (Exception ex)
195195
{
196196
// EnsureApplicationServices may have failed due to a missing or throwing Startup class.
197197
if (_applicationServices == null)
198198
{
199199
_applicationServices = _applicationServiceCollection.BuildServiceProvider();
200200
}
201201

202-
EnsureServer();
203-
204202
// Write errors to standard out so they can be retrieved when not in development mode.
205203
Console.Out.WriteLine("Application startup exception: " + ex.ToString());
206204
var logger = _applicationServices.GetRequiredService<ILogger<WebHost>>();
207205
logger.ApplicationError(ex);
208206

207+
if (!_options.CaptureStartupErrors)
208+
{
209+
throw;
210+
}
211+
212+
EnsureServer();
213+
209214
// Generate an HTML error page.
210215
var hostingEnv = _applicationServices.GetRequiredService<IHostingEnvironment>();
211216
var showDetailedErrors = hostingEnv.IsDevelopment() || _options.DetailedErrors;

test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs

+39
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,45 @@ public async Task Build_DoesNotThrowIfUnloadableAssemblyNameInHostingStartupAsse
851851
}
852852
}
853853

854+
[Fact]
855+
public void StartupErrorsAreLoggedIfCaptureStartupErrorsIsTrue()
856+
{
857+
var builder = CreateWebHostBuilder()
858+
.CaptureStartupErrors(true)
859+
.Configure(app =>
860+
{
861+
throw new InvalidOperationException("Startup exception");
862+
})
863+
.UseServer(new TestServer());
864+
865+
using (var host = (WebHost)builder.Build())
866+
{
867+
var sink = host.Services.GetRequiredService<ITestSink>();
868+
Assert.True(sink.Writes.Any(w => w.Exception?.Message == "Startup exception"));
869+
}
870+
}
871+
872+
[Fact]
873+
public void StartupErrorsAreLoggedIfCaptureStartupErrorsIsFalse()
874+
{
875+
ITestSink testSink = null;
876+
877+
var builder = CreateWebHostBuilder()
878+
.CaptureStartupErrors(false)
879+
.Configure(app =>
880+
{
881+
testSink = app.ApplicationServices.GetRequiredService<ITestSink>();
882+
883+
throw new InvalidOperationException("Startup exception");
884+
})
885+
.UseServer(new TestServer());
886+
887+
Assert.Throws<InvalidOperationException>(() => builder.Build());
888+
889+
Assert.NotNull(testSink);
890+
Assert.True(testSink.Writes.Any(w => w.Exception?.Message == "Startup exception"));
891+
}
892+
854893
[Fact]
855894
public void HostingStartupTypeCtorThrowsIfNull()
856895
{

0 commit comments

Comments
 (0)