From c759f5b8f82fc0ed1a5e918c6b611ffb861af84d Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Thu, 19 Dec 2019 17:19:49 -0800 Subject: [PATCH] HttpSys logs and samples cleanup (#17971) --- src/Servers/HttpSys/HttpSysServer.sln | 15 +++++++++ .../HttpSys/samples/SelfHostServer/Program.cs | 32 +++++++++++++++++++ .../SelfHostServer/SelfHostServer.csproj | 2 ++ .../HttpSys/samples/SelfHostServer/Startup.cs | 21 ++---------- src/Servers/HttpSys/src/HttpSysListener.cs | 6 ++-- src/Servers/HttpSys/src/LogHelper.cs | 12 +++++++ .../HttpSys/src/NativeInterop/UrlGroup.cs | 4 +-- 7 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 src/Servers/HttpSys/samples/SelfHostServer/Program.cs diff --git a/src/Servers/HttpSys/HttpSysServer.sln b/src/Servers/HttpSys/HttpSysServer.sln index dddd3217b2..75f0aad286 100644 --- a/src/Servers/HttpSys/HttpSysServer.sln +++ b/src/Servers/HttpSys/HttpSysServer.sln @@ -74,6 +74,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QueueSharing", "samples\Que EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets", "..\Kestrel\Transport.Sockets\src\Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.csproj", "{33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore", "..\..\DefaultBuilder\src\Microsoft.AspNetCore.csproj", "{E8880B06-7172-4995-893D-13E87AF00E7B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -332,6 +334,18 @@ Global {33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Release|Mixed Platforms.Build.0 = Release|Any CPU {33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Release|x86.ActiveCfg = Release|Any CPU {33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB}.Release|x86.Build.0 = Release|Any CPU + {E8880B06-7172-4995-893D-13E87AF00E7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E8880B06-7172-4995-893D-13E87AF00E7B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E8880B06-7172-4995-893D-13E87AF00E7B}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {E8880B06-7172-4995-893D-13E87AF00E7B}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {E8880B06-7172-4995-893D-13E87AF00E7B}.Debug|x86.ActiveCfg = Debug|Any CPU + {E8880B06-7172-4995-893D-13E87AF00E7B}.Debug|x86.Build.0 = Debug|Any CPU + {E8880B06-7172-4995-893D-13E87AF00E7B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E8880B06-7172-4995-893D-13E87AF00E7B}.Release|Any CPU.Build.0 = Release|Any CPU + {E8880B06-7172-4995-893D-13E87AF00E7B}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {E8880B06-7172-4995-893D-13E87AF00E7B}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {E8880B06-7172-4995-893D-13E87AF00E7B}.Release|x86.ActiveCfg = Release|Any CPU + {E8880B06-7172-4995-893D-13E87AF00E7B}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -359,6 +373,7 @@ Global {00A88B8D-D539-45DD-B071-1E955AF89A4A} = {4DA3C456-5050-4AC0-A554-795F6DEC8660} {9B58DF76-DC6D-4728-86B7-40087BDDC897} = {3A1E31E3-2794-4CA3-B8E2-253E96BDE514} {33CF53ED-A4BC-4EAA-9EA7-EF5E748A03BB} = {4DA3C456-5050-4AC0-A554-795F6DEC8660} + {E8880B06-7172-4995-893D-13E87AF00E7B} = {4DA3C456-5050-4AC0-A554-795F6DEC8660} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {34B42B42-FA09-41AB-9216-14073990C504} diff --git a/src/Servers/HttpSys/samples/SelfHostServer/Program.cs b/src/Servers/HttpSys/samples/SelfHostServer/Program.cs new file mode 100644 index 0000000000..df39ba8da8 --- /dev/null +++ b/src/Servers/HttpSys/samples/SelfHostServer/Program.cs @@ -0,0 +1,32 @@ +// 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 Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Server.HttpSys; +using Microsoft.Extensions.Hosting; + +namespace SelfHostServer +{ + public static class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup() + .UseHttpSys(options => + { + options.UrlPrefixes.Add("http://localhost:5000"); + // This is a pre-configured IIS express port. See the PackageTags in the csproj. + options.UrlPrefixes.Add("https://localhost:44319"); + options.Authentication.Schemes = AuthenticationSchemes.None; + options.Authentication.AllowAnonymous = true; + }); + }); + } +} diff --git a/src/Servers/HttpSys/samples/SelfHostServer/SelfHostServer.csproj b/src/Servers/HttpSys/samples/SelfHostServer/SelfHostServer.csproj index 35450964c9..a824a8daee 100644 --- a/src/Servers/HttpSys/samples/SelfHostServer/SelfHostServer.csproj +++ b/src/Servers/HttpSys/samples/SelfHostServer/SelfHostServer.csproj @@ -12,7 +12,9 @@ + + diff --git a/src/Servers/HttpSys/samples/SelfHostServer/Startup.cs b/src/Servers/HttpSys/samples/SelfHostServer/Startup.cs index 42f6d11f81..28665f54dc 100644 --- a/src/Servers/HttpSys/samples/SelfHostServer/Startup.cs +++ b/src/Servers/HttpSys/samples/SelfHostServer/Startup.cs @@ -1,3 +1,6 @@ +// 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 Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -28,23 +31,5 @@ namespace SelfHostServer await context.Response.WriteAsync("Hello world from " + context.Request.Host + " at " + DateTime.Now); }); } - - public static void Main(string[] args) - { - var host = new WebHostBuilder() - .ConfigureLogging(factory => factory.AddConsole()) - .UseStartup() - .UseHttpSys(options => - { - options.UrlPrefixes.Add("http://localhost:5000"); - // This is a pre-configured IIS express port. See the PackageTags in the csproj. - options.UrlPrefixes.Add("https://localhost:44319"); - options.Authentication.Schemes = AuthenticationSchemes.None; - options.Authentication.AllowAnonymous = true; - }) - .Build(); - - host.Run(); - } } } diff --git a/src/Servers/HttpSys/src/HttpSysListener.cs b/src/Servers/HttpSys/src/HttpSysListener.cs index bbb4c9d088..9aad393968 100644 --- a/src/Servers/HttpSys/src/HttpSysListener.cs +++ b/src/Servers/HttpSys/src/HttpSysListener.cs @@ -135,7 +135,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys { CheckDisposed(); - LogHelper.LogInfo(Logger, "Start"); + LogHelper.LogTrace(Logger, "Starting the listener."); // Make sure there are no race conditions between Start/Stop/Abort/Close/Dispose. // Start needs to setup all resources. Abort/Stop must not interfere while Start is @@ -195,6 +195,8 @@ namespace Microsoft.AspNetCore.Server.HttpSys return; } + LogHelper.LogTrace(Logger, "Stopping the listener."); + // If this instance created the queue then remove the URL prefixes before shutting down. if (_requestQueue.Created) { @@ -236,7 +238,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys { return; } - LogHelper.LogInfo(Logger, "Dispose"); + LogHelper.LogTrace(Logger, "Disposing the listener."); Stop(); DisposeInternal(); diff --git a/src/Servers/HttpSys/src/LogHelper.cs b/src/Servers/HttpSys/src/LogHelper.cs index 2a9345d624..f26bd78662 100644 --- a/src/Servers/HttpSys/src/LogHelper.cs +++ b/src/Servers/HttpSys/src/LogHelper.cs @@ -79,6 +79,18 @@ namespace Microsoft.AspNetCore.Server.HttpSys } } + internal static void LogTrace(ILogger logger, string data) + { + if (logger == null) + { + Debug.WriteLine(data); + } + else + { + logger.LogTrace(data); + } + } + internal static void LogException(ILogger logger, string location, Exception exception) { if (logger == null) diff --git a/src/Servers/HttpSys/src/NativeInterop/UrlGroup.cs b/src/Servers/HttpSys/src/NativeInterop/UrlGroup.cs index 87a5012641..be81b8768b 100644 --- a/src/Servers/HttpSys/src/NativeInterop/UrlGroup.cs +++ b/src/Servers/HttpSys/src/NativeInterop/UrlGroup.cs @@ -71,7 +71,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys internal void RegisterPrefix(string uriPrefix, int contextId) { - LogHelper.LogInfo(_logger, "Listening on prefix: " + uriPrefix); + LogHelper.LogDebug(_logger, "Listening on prefix: " + uriPrefix); CheckDisposed(); var statusCode = HttpApi.HttpAddUrlToUrlGroup(Id, uriPrefix, (ulong)contextId, 0); @@ -90,7 +90,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys internal bool UnregisterPrefix(string uriPrefix) { - LogHelper.LogInfo(_logger, "Stop listening on prefix: " + uriPrefix); + LogHelper.LogDebug(_logger, "Stop listening on prefix: " + uriPrefix); CheckDisposed(); var statusCode = HttpApi.HttpRemoveUrlFromUrlGroup(Id, uriPrefix, 0);