简体   繁体   中英

Hide ASP.NET Core WebHost messages

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.

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.

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.

EDIT: 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

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM