Unable to log inside TopShelf BeforeStartingService Callback - topshelf

I've written a TopShelf service and I'd like to do some validation when the service starts. If the service is horribly misconfigured, I want to give a nice error message and gracefully stop.
To do that, I've put my validation code into BeforeStartingService. When I run my service from the console, this works great. However, when I run as a service, my logging code doesn't write anything.
Is this an appropriate use case for BeforeStartingService? Is there some trick that I need to write to the log from it? I'm using Log4Net. I've tried disabling log4net entirely and just using the default trace logger. I've tried getting the logger through TopShelf with HostLogger.Get, as well as directly from Log4Net with LogManager.GetLogger.
Thanks in advance.

Related

Is there a way to monitor a Windows service and alert people when it hangs/stops?

We have a service running on a Windows Server 2003 machine. This service watches a particular folder on an FTP server, and when files appear there, it invokes one of a few different executables to process them.
I've been asked to find a way for staff to be alerted in some way when this service hangs or stops.
Can anyone suggest anything with just this much information? If not, what else would you need to know?
Seems we could write ANOTHER service to watch THIS service, but then there's a chance THAT one would stop ... so we haven't resolved anything.
About the only thing that I know if is writing another application or service that monitors if that service is running; something like that shouldn't have any unexpected behavior and stop, hopefully.
Another thing to do is go to the service in Windows, go to its properties, and then go to recovery options. From here, you can set the behavior of a service if it is to fail. The options in Windows 7 are to restart the service or computer, or run a program. This program could send some sort of notification. However, I don't know if any or all of these options exist in Server 2003. This would also not likely work if the service were to just hang, but a service watching it probably wouldn't either.
Also, if you have the source code, you can override some of the service-related methods such as OnStop() (for C#) to send a notification, but I don't believe this works with a failure.
My personal choice would be to set the recovery options just to restart the service on failure, unless it repeatedly fails, which there is also an option for. But just do what you think will work best for you; there isn't really a fail-safe method to do it.
UPDATE:
I did check, and Server 2003 does indeed have the same recovery options in the service manager. As the guys said above, you can deal with that, but it is only in C++ from what I have seen; there is also a command prompt way to do it:
sc failure [servicename] reset= 0 actions= restart/60000
I found that command here and you can look at it more in its MSDN documentation. You could call this command from C# or other languages if you are not using C++, or use it directly from the command prompt if you do not have the source code.
Use ChangeServiceConfig2() to define Failure Actions for your service. You could use that to invoke an external command to issue the alert (or do pretty much anything else you want) if the service terminates unexpectedly.
The SCM (the component which handles services) has built-in auto-restart logic that you can take advantage of to restart your service, as necessary. Additionally and/or alternatively, you can configure 'custom actions' to be associated with a failure of the service - the custom action can include launching a program of your own, which could then log the failure, and perhaps manually restart your service.
You can read more about such custom actions on MSDN by looking at the documentation of the structure used to configure such actions: SERVICE_FAILURE_ACTIONS. Once you fill that structure, you notify the SCM by calling the ChangeServiceConfig2 function.
Please don't ask "well, what happens if my failure handler program crashes" :)

What is the simplest way to add logging to a background worker in AppHarbor

I have a console app that uses Quartz and sends out emails on a schedule. I want some basic logging so I can see if the app has started and configured (NHibernate) correctly, and also any stack traces from uncaught exceptions.
I could set up Log4net or similar to write log entries to a database table, or email them to me. But is there a simple way built into AppHarbor?
Use the LogEntries Add on. Then install the le_log4net nuget package and enter your api key in the app.config. There is a Free subscription that should tell you what you need.

How to make sure that my service is always up and running

I have created a service in Windows and set enteries in Registry so that the service automatically starts on log on.
Now the problem is that in Task manager->Services field, my service's status is Running for only 2-3 minutes after log-on.
After this time my service status turns to Stopped, and it never again switches to running.
It also doesnot do its designated work.
I want to know that what changes in Registry or the Properties of the service can be made to make sure that service is always running.
Chances are that you are getting an unhandled exception which is shutting down the process.
You need to add logging to your windows service - something that will write all exceptions to the event log is a fairly common thing to do.
This will allow you to see why the service is stopping. At this point you will hopefully have enough information to fix the coding error.
I want to know that what changes in Registry or the Properties of the service can be made to make sure that service is always running.
Well, assuming the issue is with your service, there is no configuration that will help.

In Windows, what default event sources are available in the Application Event Log?

Short Version:
Are the event sources "Application" and "Application Error" always included in the Application Event Log? Are they available on new installations of Windows XP, Vista and Windows 7? Would it be really bad to use them instead of creating my own source (an impossibility for me)?
Long Version:
I have a ClickOnce application that is used by users without administrative privileges on their machines.
When I try to write to the Appliction Event Log, I get a security exception. (The Windows event logging infrastructure is trying to create me a new event source, and gets a security violation.)
So I would like to try reusing an existing event source. I have found a only two"generic-sounding" sources in the Application Event Log. Are these always part of a Windows installation, and would make a reasonable choice?
Application
Application Error
I am sure this is frowned upon, as I should distinguish my application using its own event source. But this is for infrequent fatal errors, which should be getting logged elsewhere by my code. I just want a really easy place to find them on a client machine in case it all goes wrong...
When I try to write to the Appliction Event Log, I get a security exception. (The Windows event logging infrastructure is trying to create me a new event source, and gets a security violation.)
I have just answered this here: Using EventLog in ClickOnce application
So I would like to try reusing an existing event source. I have found a only two"generic-sounding" sources in the Application Event Log. Are these always part of a Windows installation, and would make a reasonable choice?
It's really not wise to do this. Existing event sources will be used by either Windows applications, or by third party applications. If any of those are removed, or changed by something like a service pack or patch, your program will crash unless you have implemented exception handling to handle the exception gracefully, but then you wont have any event logging.
Also consider the work you may have to do to port your app to the next version of Windows. I suggest you will be making a rod for your own back.
In the answer I linked to, I suggested the best way to handle the problem, is to install your application using admin privs with the installer creating the source, or by creating a simple app that effectively does the same using the admin role.
The only thing else I can suggest is to always run your application in admin mode.

ServiceBase.OnShutdown and event logs in Windows .Net 3.5

I've written a custom service that overrides ServiceBase.OnShutdown().
Unfortunately, when I log to the event log, nothing is written.
My guess is that the Windows event log was shut down before my service.
Is there a way to order service shutdown so that my servce shuts down
before the event logger? I don't want to have to write out to a file.
Pl. advise. Thanks.
You could try to setup a dependency where your service depends on the Event logger, this is mostly done to make them load in the correct order but I assume that might make sure that your service always was stopped first as well.
As can be seen in this Technet article, you'd need to change the DependOnService value either using the Sc.exe tool or the ChangeServiceConfig API.
There is a way, but it is more or less a Reflection-Hack.
I added my solution to an other post: Here
Hope I could help.

Resources