This is not really a question, but an answer which would hopefully help other people.
Those who've written a windows service before, know what a mission it could be to find a bug in it, particularly if it only happens on the live environment. In my case, I had a service that ran smoothly for a couple of hours, and then fell over from a stack overflow error. No stack trace. Good luck finding the needle in the haystack.
The service did produce a log file, and the code was littered with log entries, but as detailed as is was, it produced log files of 500 MB! You could barely open the file, never mind analyse it. But how do you get around this problem? You could try to produce log files with less information, or one that automatically delete older logs entries as newer ones are being written, but then you loose important context of the error.
The solution is a log file that will keep track of loops in your code, and automatically delete the log entries for each successful iteration of that loop. This way, you can maintain a highly detained log file which remains relatively small at the same time. When your service breaks, your log file will tell you exactly where it happened, plus all the necessary context to explain how and why it happened.
You can download this logfile generator from http://sourceforge.net/projects/smartl/files/?source=navbar. It's a stand alone class and all its methods is static. An example class is provided to show you how to use the logging methods correctly:
public void ExampleMethod()
{
SmartLog.EnterMethod("ExampleMethod()");
try
{
SmartLog.Write("Some code happening before the loop");
Guid exampleLoopID = SmartLog.RegisterLoop("exampleLoopID");
for (int i = 0; i < 10; i++)
{
SmartLog.IncrementLoop(exampleLoopID);
SmartLog.Write("Some code happening inside the loop.");
}
SmartLog.CompleteLoop(exampleLoopID);
SmartLog.Write("Some code happening after the loop.");
SmartLog.LeaveMethod("ExampleMethod()");
}
catch (Exception ex)
{
SmartLog.WriteException(ex);
SmartLog.LeaveMethod("ExampleMethod()");
throw;
}
}
Make sure your application has read and write access on its root folder.
If you execute the code, and you break it inside the loop, the logfile will look something like this:
. ENTER METHOD: FirstMethod()
some code happening here.
Calling a different method:
. . ENTER METHOD: ExampleMethod()
some code happening before the loop.
LOOP: doWorkLoopID [4135a8ed-05b7-45de-b887-b2ab3c638faa] - CURRENT ITERATION: 20
some code happening inside the loop.
Once the loop has been completed, its content is removed, and your log file will look like this:
. ENTER METHOD: FirstMethod()
some code happening here.
Calling a different method:
. . ENTER METHOD: ExampleMethod()
some code happening before the loop.
LOOP: doWorkLoopID [4135a8ed-05b7-45de-b887-b2ab3c638faa] - TOTAL ITERATIONS: 22
some code happening after the loop.
. . LEAVING METHOD: ExampleMethod()
some code happening here.
some code happening here.
. LEAVING METHOD: FirstMethod()
I hope this can help someone solve that problem which otherwise could've taken weeks. It sure did the trick for me.