Bug: Example for WireMock as Windows Service throws Exception because of WireMockConsoleLogger #151

Closed
opened 2025-12-29 14:23:47 +01:00 by adam · 4 comments
Owner

Originally created by @paulssn on GitHub (Nov 6, 2018).

Originally assigned to: @paulssn on GitHub.

Problem:
If I run the example for WireMock as a Windows Service, it throws the following exception on service startup:

Service cannot be started. System.IO.IOException: The handle is invalid.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError()
at System.Console.set_OutputEncoding(Encoding value)
at WireMock.Logging.WireMockConsoleLogger..ctor() in /Logging/WireMockConsoleLogger.cs:line 18
at Wiremock.Net.Service.Program.Start() in C:\Development\WireMockStandalone\WireMock.Net.Service\Program.cs:line 60
at Wiremock.Net.Service.Program.Service.OnStart(String[] args) in C:\Development\WireMockStandalone\WireMock.Net.Service\Program.cs:line 24
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

Reason:
This is caused by the WireMockConsoleLogger which is also used if the application is actually running as a service. In this case the exception is thrown when the logger tries to access the console (which is actually not accessible in the service context).

Possible solution:
Use the WireMockConsoleLogger only in case the application is running as console app and use the WireMockNullLogger if it's running as service.

Of course, this can be easily fixed and does therefore not present a major problem, but it would be nice if the example would just work out of the box without the need to fix this.

Originally created by @paulssn on GitHub (Nov 6, 2018). Originally assigned to: @paulssn on GitHub. **Problem:** If I run the [example](https://github.com/WireMock-Net/WireMock.Net/tree/master/examples/WireMock.Net.Service) for WireMock as a Windows Service, it throws the following exception on service startup: > Service cannot be started. System.IO.IOException: The handle is invalid. > > at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) > at System.IO.__Error.WinIOError() > at System.Console.set_OutputEncoding(Encoding value) > at WireMock.Logging.WireMockConsoleLogger..ctor() in /Logging/WireMockConsoleLogger.cs:line 18 > at Wiremock.Net.Service.Program.Start() in C:\Development\WireMockStandalone\WireMock.Net.Service\Program.cs:line 60 > at Wiremock.Net.Service.Program.Service.OnStart(String[] args) in C:\Development\WireMockStandalone\WireMock.Net.Service\Program.cs:line 24 > at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state) **Reason:** This is caused by the `WireMockConsoleLogger` which is also used if the application is actually running as a service. In this case the exception is thrown when the logger tries to access the console (which is actually not accessible in the service context). **Possible solution:** Use the `WireMockConsoleLogger` only in case the application is running as console app and use the `WireMockNullLogger` if it's running as service. Of course, this can be easily fixed and does therefore not present a major problem, but it would be nice if the example would just work out of the box without the need to fix this.
adam added the bug label 2025-12-29 14:23:47 +01:00
adam closed this issue 2025-12-29 14:23:48 +01:00
Author
Owner

@paulssn commented on GitHub (Nov 6, 2018):

I'd be happy to submit a PR, if that's ok for you.

@paulssn commented on GitHub (Nov 6, 2018): I'd be happy to submit a PR, if that's ok for you.
Author
Owner

@StefH commented on GitHub (Nov 6, 2018):

Thanks for finding this bug.

You can submit a PR where you just use the WireMockNullLogger in the Program.cs.

Or maybe better (however more work), create WireMockEventLogger which logs to the Windows Event log ? (Maybe using log4net / nlog or ... ?)

@StefH commented on GitHub (Nov 6, 2018): Thanks for finding this bug. You can submit a PR where you just use the `WireMockNullLogger` in the **Program.cs**. Or maybe better (however more work), create `WireMockEventLogger` which logs to the Windows Event log ? (Maybe using log4net / nlog or ... ?)
Author
Owner

@paulssn commented on GitHub (Nov 6, 2018):

You're welcome. Thank you for maintaining this useful project!

For now I fixed it by using the WireMockNullLogger in the corresponding case.

I agree with you that using an actual logger would be better and make sense. I'll consider to implement it and submit a separate PR for it as well.
Where would you put the corresponding logger? Just in the WireMock.Net.Service-project like for example the WireMockLog4NetLogger which lives in the WireMock.Net.StandAlone.NETCoreApp-example?

I'd tend to use a log4net-logger and configure something like an EventLogAppender and maybe an additional file appender. Would this basically match your thoughts?

@paulssn commented on GitHub (Nov 6, 2018): You're welcome. Thank you for maintaining this useful project! For now I fixed it by using the `WireMockNullLogger` in the corresponding case. I agree with you that using an actual logger would be better and make sense. I'll consider to implement it and submit a separate PR for it as well. Where would you put the corresponding logger? Just in the `WireMock.Net.Service`-project like for example the `WireMockLog4NetLogger` which lives in the `WireMock.Net.StandAlone.NETCoreApp`-example? I'd tend to use a log4net-logger and configure something like an [EventLogAppender ](http://logging.apache.org/log4net/log4net-2.0.7/release/sdk/html/T_log4net_Appender_EventLogAppender.htm) and maybe an additional file appender. Would this basically match your thoughts?
Author
Owner

@StefH commented on GitHub (Nov 6, 2018):

Correct. Just add a new WireMockEventLogger in the WireMock.Net.Service-project which can be based on log4net or just custom code to write to event log.

@StefH commented on GitHub (Nov 6, 2018): Correct. Just add a new WireMockEventLogger in the WireMock.Net.Service-project which can be based on log4net or just custom code to write to event log.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#151