【问题标题】:Hide ASP.NET Core WebHost messages隐藏 ASP.NET Core WebHost 消息
【发布时间】:2017-11-23 15:18:39
【问题描述】:

我有一个 ASP.NET Core 应用程序,我想隐藏启动应用程序时显示的控制台行(因为我有自己的欢迎消息)

Hosting environment: Development
Content root path: path\to\my\executable
Now listening on: http://localhost:portnumber
Application started. Press Ctrl+C to shut down.

我尝试了一个解决方案,将 Console.Out 设置为空流,然后在我想写东西时将其设置回来。它在我的开发 PC 上运行良好,但我在其他 PC 上看到了这个问题。

    Console.Write(WelcomeMessage);
    ConsOut = Console.Out;  //Save the reference to the old out value (The terminal)
        Console.SetOut(new StreamWriter(Stream.Null));  //Hack out the console
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseUrls(url)
            .UseStartup<Startup>()
            .UseApplicationInsights()
            .Build();
        host.Run();

我对这个解决方案不满意,因为它看起来像一个 hack,而且不能始终如一地工作。

How to turn off the logging done by the ASP.NET core framework,我发现您可以在 Startup.cs 类的 Configure() 方法中禁用自动日志记录系统,所以我相信这是我正在寻找的类似的东西,我只是不知道如何删除托管消息。

编辑:我注意到我的问题与Asp.Net Core API disable startup complete message 相同。 但是,在想要隐藏启动消息,但仍然想编写自己的控制台消息方面尚未得到满意的答案。

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    我发现this answer 也可以解决我的问题。 我的方法最终看起来像这样:

         Console.Write(WelcomeMessage);
            ConsOut = Console.Out;  //Save the reference to the old out value (The terminal)
            Console.SetOut(new StreamWriter(Stream.Null));  //Remove the console output
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseUrls(url)
                .UseStartup<Startup>()
                .UseApplicationInsights()
                .Build();
            host.Start();   //Start the host in a non-blocking way
            Console.SetOut(ConsOut);    //Put the console output back, after the messages has been written
            Console.CancelKeyPress += OnConsoleCancelKeyPress;
            var waitHandles = new WaitHandle[] {
                CancelTokenSource.Token.WaitHandle
            };
            WaitHandle.WaitAll(waitHandles);    //Wait until the cancel signal has been received
    

    OnConsoleCancelKeyPress 方法如下所示:

        /// <summary>
        /// This method is meant as an eventhandler to be called when the Console received a cancel signal.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void OnConsoleCancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            //Got Ctrl+C from console
            Console.WriteLine("Shutting down.");
            CancelTokenSource.Cancel();
        }
    

    由于 Start() 是一个非阻塞调用,当 WebHost 启动时返回,我可以确保此时启动 WebHost 时输出的消息已经写入,并且它们不会再打扰我的用户(我在我首先遇到错误的机器上进行了测试)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 2020-02-24
      • 2014-02-01
      • 2018-07-06
      相关资源
      最近更新 更多