Re: Windows Service in C#



Yep, I usually do something similar to what Jeroen said with all of the services I have to maintain. I typically add a command line argument /debug and pause the thread for 10 seconds in OnStart. Be aware that if the service doesn't complete OnStart after approximately 30 seconds the service can be terminated at any time. Also, I tend to use the Event Log on the machine to log any unhandled exceptions that occur by the services. Depending on what level of logging you need log4net or the Enterprise Library logging block may be of use.

I'd start by checking the event log in the application section on the machine where the errors are occuring at just to make sure something hasn't already been logged.

"Jeroen Mostert" <jmostert@xxxxxxxxx> wrote in message news:4873646a$0$14352$e4fe514c@xxxxxxxxxxxxxxxxx
Alberto Poblacion wrote:
<emanuelanechei@xxxxxxxxx> wrote in message news:d66f3e2d-0435-4c44-b5d8-04fd06fca9c1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
These days i'm creating a windows service in c# which reads some data
from a file and saves it in a database. The data in the file changes
every minute.
I am using FileSystemWatcher object which monitors that file for
LastWrite event.
The problem is that after the reading of file and writing the data in
the database the service quits unexpectedly.
What happends? I googled but found nothing about it.
Thank you in advance

Try running the service attached to the debugger, so that you can trace execution step by step and see where it is failing. To do this, start the service, and then from Visual Studio select Debug -> Attach to Process, and select the service that you started. In this way you can use the debugger against the service in the same way as if you were debugging a standard windows executable.

This works, but a drawback of this method is that it doesn't allow you to debug the process from the start, so you can miss events you need to see. It's also quite inconvenient to have to do this every time.

A more convenient way of debugging a service is to run it as a console application instead. To do this, change the boilerplate code generated for the Main() method to something like this:

Service1 service = new Service1();
if (Environment.UserInteractive) {
service.OnStart();
Console.ReadLine();
service.OnStop();
} else {
ServiceBase.Run(service);
}

You will now be able to debug the service from within Visual Studio. Be aware that there are differences between running the application like this and running it as a service, the most important being that services typically run under the local system account rather than your user account. This method will not help you diagnose failures that are a result of the service running in a special environment, but it will help you find "regular" bugs.

Also, to diagnose unexpected exits, it always pays to hook up an event handler to AppDomain.UnhandledException and write verbose logging somewhere. Apropros logging: this, too, is invaluable for services, since the event log should be used sparingly and is not suitable for dumping debugging information to. I recommend a library like log4net for this.

--
J.

.



Relevant Pages

  • Re: Setting a variable (NDEBUG) during compilation of a JDK 1.1 applet
    ... do debug stuff ... are you referring to a method of debugging that involves the ide in some way that you can do with other languages / ides etc that you cant with java. ... Don't use println's for logging. ... But logging should be used for recording what's going on. ...
    (comp.lang.java.programmer)
  • Re: Logging!!?
    ... > I am used to use logging into a file with Java but I see many in .NET use ... > Can you tell me if anybody cares about logging and what is the strategy ... you might want to use a database. ... The Event log allows you to filter/sort the records on ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Constant "Configuration Errors"
    ... It seems that you are running into a permission issue. ... access denied to the user who is debugging. ... and shut down the logging tool after several seconds. ... Immediately select Options/Capture Events to STOP logging. ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: GP NOt Working on One XP Workstation
    ... You probably need to enable logging and try to find any ... possibly related messages in the event log of that machine. ... XP that walks you through enabling logging by policy ...
    (microsoft.public.windows.group_policy)
  • Re: Can you Audit logouts??
    ... > I have auditing for logins/logouts turned on. ... > when people are logging in and logging out? ... > see when user John Doe logged out last week one night if ... The event log is painful to go ...
    (microsoft.public.win2000.security)