Add debug logging in address binding when IPv6Any fails

This commit is contained in:
Nate McMaster 2017-04-24 15:36:18 -07:00 committed by Nate McMaster
parent 089ff49643
commit 39047638cc
6 changed files with 52 additions and 1 deletions

View File

@ -25,6 +25,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "shared", "shared", "{0EF2AC
test\shared\KestrelTestLoggerFactory.cs = test\shared\KestrelTestLoggerFactory.cs
test\shared\LifetimeNotImplemented.cs = test\shared\LifetimeNotImplemented.cs
test\shared\MockFrameControl.cs = test\shared\MockFrameControl.cs
test\shared\MockLogger.cs = test\shared\MockLogger.cs
test\shared\MockSocketOutput.cs = test\shared\MockSocketOutput.cs
test\shared\MockSystemClock.cs = test\shared\MockSystemClock.cs
test\shared\StringExtensions.cs = test\shared\StringExtensions.cs

View File

@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="FallbackToIPv4Any" xml:space="preserve">
<value>Failed to bind to http://[::]:{port} (IPv6Any). Attempting to bind to http://0.0.0.0:{port} instead.</value>
</data>
<data name="ResponseStreamWasUpgraded" xml:space="preserve">
<value>Cannot write to response body after connection has been upgraded.</value>
</data>

View File

@ -206,6 +206,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
}
catch (Exception ex) when (!(ex is IOException))
{
context.Logger.LogDebug(CoreStrings.FormatFallbackToIPv4Any(parsedAddress.Port));
// for machines that do not support IPv6
options = new ListenOptions(new IPEndPoint(IPAddress.Any, parsedAddress.Port));
await BindEndpointAsync(options, context).ConfigureAwait(false);

View File

@ -10,6 +10,20 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core
private static readonly ResourceManager _resourceManager
= new ResourceManager("Microsoft.AspNetCore.Server.Kestrel.Core.CoreStrings", typeof(CoreStrings).GetTypeInfo().Assembly);
/// <summary>
/// Failed to bind to http://[::]:{port} (IPv6Any). Attempting to bind to http://0.0.0.0:{port} instead.
/// </summary>
internal static string FallbackToIPv4Any
{
get => GetString("FallbackToIPv4Any");
}
/// <summary>
/// Failed to bind to http://[::]:{port} (IPv6Any). Attempting to bind to http://0.0.0.0:{port} instead.
/// </summary>
internal static string FormatFallbackToIPv4Any(object port)
=> string.Format(CultureInfo.CurrentCulture, GetString("FallbackToIPv4Any", "port"), port);
/// <summary>
/// Cannot write to response body after connection has been upgraded.
/// </summary>

View File

@ -8,6 +8,7 @@ using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
@ -88,6 +89,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
[InlineData("http://contoso.com:80")]
public async Task FallbackToIPv4WhenIPv6AnyBindFails(string address)
{
var logger = new MockLogger();
var addresses = new ServerAddressesFeature();
addresses.Addresses.Add(address);
var options = new List<ListenOptions>();
@ -97,7 +99,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
await AddressBinder.BindAsync(addresses,
options,
NullLogger.Instance,
logger,
endpoint =>
{
if (endpoint.IPEndPoint.Address == IPAddress.IPv6Any)
@ -116,6 +118,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
Assert.True(ipV4Attempt, "Should have attempted to bind to IPAddress.Any");
Assert.True(ipV6Attempt, "Should have attempted to bind to IPAddress.IPv6Any");
Assert.Contains(logger.Messages, f => f.Equals(CoreStrings.FormatFallbackToIPv4Any(80)));
}
}
}

28
test/shared/MockLogger.cs Normal file
View File

@ -0,0 +1,28 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions.Internal;
namespace Microsoft.AspNetCore.Testing
{
public class MockLogger : ILogger
{
private List<string> _messages = new List<string>();
public IDisposable BeginScope<TState>(TState state)
=> NullScope.Instance;
public bool IsEnabled(LogLevel logLevel)
=> true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
_messages.Add(formatter(state, exception));
}
public IReadOnlyList<string> Messages => _messages;
}
}