Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
paulssn opened this issue Nov 6, 2018 · 4 comments
Assignees
Labels

Comments

@paulssn
Copy link
Contributor

paulssn commented Nov 6, 2018

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.

@paulssn
Copy link
Contributor Author

paulssn commented Nov 6, 2018

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

@StefH StefH self-assigned this Nov 6, 2018
@StefH StefH added the bug label Nov 6, 2018
@StefH StefH assigned paulssn and unassigned StefH Nov 6, 2018
@StefH
Copy link
Collaborator

StefH commented 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 ... ?)

@paulssn
Copy link
Contributor Author

paulssn commented 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?

@StefH StefH closed this as completed in #224 Nov 6, 2018
@StefH
Copy link
Collaborator

StefH commented 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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants