From f79904404e0eef492add9d810ef7a75fe99d713a Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Tue, 28 Mar 2017 10:48:46 -0700 Subject: [PATCH] Log status of port before starting server with SelfHostDeployer (#993) --- .../Common/PortHelper.cs | 77 +++++++++++++++++++ .../Deployers/SelfHostDeployer.cs | 8 +- 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/PortHelper.cs diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/PortHelper.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/PortHelper.cs new file mode 100644 index 0000000000..805a590180 --- /dev/null +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/PortHelper.cs @@ -0,0 +1,77 @@ +// 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.Diagnostics; +using System.Runtime.InteropServices; +using Microsoft.Extensions.Logging; + +namespace Microsoft.AspNetCore.Server.IntegrationTesting +{ + public class PortHelper + { + public static void LogPortStatus(ILogger logger, int port) + { + logger.LogInformation("Checking for processes currently using port {0}", port); + + var psi = new ProcessStartInfo + { + RedirectStandardOutput = true, + RedirectStandardError = true, + }; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + psi.FileName = "cmd"; + psi.Arguments = $"/C netstat -nq | find \"{port}\""; + } + else + { + psi.FileName = "lsof"; + psi.Arguments = $"-i :{port}"; + } + + var process = new Process + { + StartInfo = psi, + EnableRaisingEvents = true + }; + + var linesLogged = false; + + process.OutputDataReceived += (sender, data) => + { + if (!string.IsNullOrWhiteSpace(data.Data)) + { + linesLogged = true; + logger.LogInformation("portstatus: {0}", data.Data); + } + }; + process.ErrorDataReceived += (sender, data) => + { + if (!string.IsNullOrWhiteSpace(data.Data)) + { + logger.LogWarning("portstatus: {0}", data.Data); + } + }; + + try + { + process.Start(); + process.BeginErrorReadLine(); + process.BeginOutputReadLine(); + process.WaitForExit(); + + if (!linesLogged) + { + logger.LogInformation("portstatus: it appears the port {0} is not in use.", port); + } + } + catch (Exception ex) + { + logger.LogWarning("Failed to check port status. Executed: {0} {1}\nError: {2}", psi.FileName, psi.Arguments, ex.ToString()); + } + return; + } + } +} diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/SelfHostDeployer.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/SelfHostDeployer.cs index 31a7023cfb..83ec0806c0 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/SelfHostDeployer.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/SelfHostDeployer.cs @@ -83,6 +83,12 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting executableArgs = $"run --framework {targetFramework} {DotnetArgumentSeparator}"; } + if (uri.Port != 0) + { + // shows output from netstat/lsof. May be useful if the port is already in use + PortHelper.LogPortStatus(Logger, uri.Port); + } + executableArgs += $" --server.urls {uri} " + $" --server {(DeploymentParameters.ServerType == ServerType.WebListener ? "Microsoft.AspNetCore.Server.HttpSys" : "Microsoft.AspNetCore.Server.Kestrel")}"; @@ -148,4 +154,4 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting StopTimer(); } } -} \ No newline at end of file +}