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

Always log startup exceptions #1153

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,26 @@ private RequestDelegate BuildApplication()

return builder.Build();
}
catch (Exception ex) when (_options.CaptureStartupErrors)
catch (Exception ex)
{
// EnsureApplicationServices may have failed due to a missing or throwing Startup class.
if (_applicationServices == null)
{
_applicationServices = _applicationServiceCollection.BuildServiceProvider();
}

EnsureServer();

// Write errors to standard out so they can be retrieved when not in development mode.
Console.Out.WriteLine("Application startup exception: " + ex.ToString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't still want the ConsoleWriteline do we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep it as people probably don't use the Console Logger in non-development environments and having the console output as an additional troubleshooting mechanism could be helpful.

E.g. in Service Fabric you can redirect console output to a file - by keeping this Console.WriteLine one would at least get the startup messages ("Listening on...") and any startup exceptions.

var logger = _applicationServices.GetRequiredService<ILogger<WebHost>>();
logger.ApplicationError(ex);

if (!_options.CaptureStartupErrors)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the braces

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the braces - sorry for that!

{
throw;
}

EnsureServer();

// Generate an HTML error page.
var hostingEnv = _applicationServices.GetRequiredService<IHostingEnvironment>();
var showDetailedErrors = hostingEnv.IsDevelopment() || _options.DetailedErrors;
Expand Down
39 changes: 39 additions & 0 deletions test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,45 @@ public async Task Build_DoesNotThrowIfUnloadableAssemblyNameInHostingStartupAsse
}
}

[Fact]
public void StartupErrorsAreLoggedIfCaptureStartupErrorsIsTrue()
{
var builder = CreateWebHostBuilder()
.CaptureStartupErrors(true)
.Configure(app =>
{
throw new InvalidOperationException("Startup exception");
})
.UseServer(new TestServer());

using (var host = (WebHost)builder.Build())
{
var sink = host.Services.GetRequiredService<ITestSink>();
Assert.True(sink.Writes.Any(w => w.Exception?.Message == "Startup exception"));
}
}

[Fact]
public void StartupErrorsAreLoggedIfCaptureStartupErrorsIsFalse()
{
ITestSink testSink = null;

var builder = CreateWebHostBuilder()
.CaptureStartupErrors(false)
.Configure(app =>
{
testSink = app.ApplicationServices.GetRequiredService<ITestSink>();

throw new InvalidOperationException("Startup exception");
})
.UseServer(new TestServer());

Assert.Throws<InvalidOperationException>(() => builder.Build());

Assert.NotNull(testSink);
Assert.True(testSink.Writes.Any(w => w.Exception?.Message == "Startup exception"));
}

[Fact]
public void HostingStartupTypeCtorThrowsIfNull()
{
Expand Down