Thomas
Thomas

Reputation: 5998

Console.WriteLine() inside a Windows Service?

I am currently using TopShelf with a Console Application to create a Windows Service. When I run the code as a console application I use a few Console.WriteLine() to output results. Once the code does what it is supposed to do I install the console application as a Windows Service.

Are there any downsides with leaving the Console.WriteLine() code even though a Windows Service can not write to the console? Are there any risks of having unstable code if I leave the Console.WriteLine() in there?

Upvotes: 40

Views: 37670

Answers (5)

TomB
TomB

Reputation: 765

Also want to display a help depending on command line.
My solution is to open a new console.

internal static class NativeMethods
{
    [DllImport("user32.dll")]
    internal static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll")]
    internal static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

    [DllImport("kernel32.dll")]
    internal static extern bool AllocConsole();

    [DllImport("kernel32.dll")]
    internal static extern IntPtr GetConsoleWindow();
}

public static void ShowWindow(IntPtr hWnd, ShowWindowCommand cmd = ShowWindowCommand.Restore)
{
    NativeMethods.SetForegroundWindow(hWnd);
    NativeMethods.ShowWindowAsync(hWnd, (int)cmd);
}

public static void ShowConsoleWindow()
{
    var handle = NativeMethods.GetConsoleWindow();

    if (handle == IntPtr.Zero)
        NativeMethods.AllocConsole();
    else
        ShowWindow(handle, ShowWindowCommand.Show);
}

ShowWindowCommand is just an enum built from here
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow

Edit:
That solution worked for years but currently fails. Unclear if it is related to a Windows- or VisualStudio update. Definitly does not work on W10 20H2 and VS2019 16.9.x

Upvotes: 1

dtech
dtech

Reputation: 14060

The output will simply be discarded.

In a Windows Service there is no Console so Console.Write* output is discarded. There are a number of alternatives:

  1. The System.Diagnostics.Trace class has a similar interface to the Console class so you could migrate your code quite easily to this.
  2. It can then be configured to output to a file. You can use the System.Diagnostics.EventLog class to write to the Event Log which you can then monitor using Event Viewer.
  3. You can use the third-party open-source log4net library which is very flexible.

Upvotes: 42

Pierre
Pierre

Reputation: 114

The output was always discarded until Windows Server 2008R2. Leave a console.writeline() in a service installed on that OS, and you'll get an error 1067 when starting/running the service, depending on the position of the writeline().

Upvotes: 3

Bill Crim
Bill Crim

Reputation: 622

If you use the System.Diagnostics.Trace functionality, you can redirect the output using the listeners and switches. If you compile with the TRACE symbol, then code will be included. If you don't add the TRACE, then it won't be compiled into the project.

If you run your services as console for debugging, the Trace will output to the console by default. I've taken to using Trace instead of Debug or Console writes since I can, from the config file, output the trace information to any combination of files, screen, database, etc.

Upvotes: 4

doogle
doogle

Reputation: 3406

No, the console class will safely write to the STDOUT, but you will just not see the output.

Upvotes: 9

Related Questions