swan icon indicating copy to clipboard operation
swan copied to clipboard

Logger creation fails when there is no terminal

Open katjoek opened this issue 2 years ago • 1 comments

Describe the bug I've created a webserver using EmbedIO. That webserver is to be started by another application without the user seeing that it is running. So no console window should be present. I'm starting the application using this process definition:

        return new Process
        {
            EnableRaisingEvents = true,
            StartInfo = new ProcessStartInfo
            {
                WorkingDirectory = workingDirectory,
                FileName = fileName,
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
            },
        };

When the webserver application is started, the application fails. In the EventViewer we see this:

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException
   at System.IO.__Error.WinIOError(Int32, System.String)
   at System.Console.set_CursorVisible(Boolean)
   at Swan.Terminal..cctor()

Exception Info: System.TypeInitializationException
   at Swan.Terminal.get_IsConsolePresent()
   at Swan.Logging.Logger..cctor()

Exception Info: System.TypeInitializationException
   at Swan.Logging.Logger.Info(System.String, System.String, System.Object, System.String, System.String, Int32)
   at EmbedIO.WebServer.CreateHttpListener()
   at EmbedIO.WebServer..ctor(System.Action`1<EmbedIO.WebServerOptions>)
   at Luxexcel.LensRecognition.Server.Program.CreateWebServer(System.String)
   at Luxexcel.LensRecognition.Server.Program.RunWebserver(Luxexcel.LensDiscovery.Shared.CommandLineOptions)
   at CommandLine.ParserResultExtensions.WithParsed[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](CommandLine.ParserResult`1<System.__Canon>, System.Action`1<System.__Canon>)
   at Luxexcel.LensRecognition.Server.Program.Main(System.String[])

It fails in Logger's class constructor on IsConsolePresent.

I'm using Swan 3.1.0

Expected behavior It should not throw an exception and not try to add the terminal as a sink when it isn't there.

Desktop (please complete the following information):

  • OS: Windows 10
  • .NET Framework 4.8

katjoek avatar Oct 28 '22 15:10 katjoek

Hello @katjoek, thanks for using EmbedIO! Oh, and Swan, of course.

Unfortunately the bug is in Terminal's class initializer, where it checks for the existence of the console. I'm sure the irony doesn't escape you.

The only workaround I can think of is to replace this line:

                CreateNoWindow = true,

with this:

                WindowStyle = ProcessWindowStyle.Hidden,

so the console is created but doesn't clutter your screen. I'm not 100% sure it will work but I'd give it a shot.

rdeago avatar Oct 28 '22 22:10 rdeago