Windows service on local computer started and then stopped


I am getting below error while running windows service:

The "my service name" service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other service or programs"


It is difficult to track down this type of generic errors. You can do the following options to figure out the issue and fix it.

Option 1: With Event viewer you can see the errors and on which line number we are getting it. Do the following steps to open the Event Viewer:

1) Type Event Viewer on start and click it.

2) In popup look for "Windows Logs" under "Event Viewer (Local)" on left panel and click it.

3) Now you will get list of windows logs, select "Application" from it. Here you can search for error, see the screen shot below:


Option 2: You can have try catch blog with logging errors to table. I have created a small function to log error as below:

public static void getLog(string msg)
 string query = "insert into LOGTABLE (DATE,LEVEL,MESSAGE,EXCEPTION) values(current_timestamp,'INFO','"+msg+"','-')";
 SqlCommand command;
 SqlDataReader dataReader;
 var DB = dbConnect();
 command = new SqlCommand(query, DB);
 dataReader = command.ExecuteReader();

Add more try/catch blocks and logging to the app to get a better picture of what's going on. Hope it helps someone!!!

Your feedbacks are most welcome..