隐藏ASP.NET Core WebHost消息 [英] Hide ASP.NET Core WebHost messages

查看:129
本文介绍了隐藏ASP.NET Core WebHost消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an ASP.NET Core application where I would like to hide the console lines that show up when starting the application (as I have my own welcome message)

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上看到了这个问题.

I have attempted a solution where I set Console.Out to a null stream, and then set it back when I want to write something. It works fine for my development PC, but I've seen the issue show up on other PCs.

    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();

我对这种解决方案感到不满意,因为它看起来像是骇客,并且无法始终如一地工作.

I am unhappy with this solution, as it seems like a hack, and doesn't work consistently.

来自如何关闭由ASP.NET核心框架完成的日志记录,我发现您可以在Startup.cs类的Configure()方法中禁用自动日志记录系统,因此我认为这是相似的我正在寻找,我只是不知道该怎么做才能删除托管消息.

From How to turn off the logging done by the ASP.NET core framework, I've figured out that you can disable the automated logging system in the Configure() method of the Startup.cs class, so I believe it is something similar I am looking for, I just do not know what to do to remove the hosting messages.

我注意到我的问题与

I've noticed that my question is the same as Asp.Net Core API disable startup complete message. However, it has not been given a satisfactory answer in terms of wanting to hide the startup messages, but still wanting to write my own console messages.

推荐答案

我发现

I found that this answer worked as a solution to my problem as well. My method ended up looking like this:

     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方法如下所示:

The OnConsoleCancelKeyPress method looks like this:

    /// <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时输出的消息到那时已经被写入,并且它们不会再打扰我的用户了(我首先在遇到错误的机器上进行了测试.

As Start() is a non-blocking call, returning when the WebHost has been started, I can ensure that the messages output while starting the WebHost have already been written by then, and they will not disturb my users anymore (I tested on the machines on which I'd encountered the error in the first place).

这篇关于隐藏ASP.NET Core WebHost消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆