You could hook into the tracing framework that forms part of the CLR. Using a simple class like: http://www.chaosink.co.uk/files/tracing.zip you can selectively log diagnostic information. To use it add the class to your application. Create an inistance of the tracer in your class like:
private Tracing trace = new Tracing("My.Namespace.Class");
and call it using:
MyClass()
{
trace.Verbose("Entered MyClass");
int x = 12;
trace.Information("X is: {0}", x);
trace.Verbose("Leaving MyClass");
}
There are 4 levels of information in the inbuilt tracing framework:
Verbose - To log program flow
Information - To log specific information of interest to monitors
Warning - To log an invalid state or recoverable exception
Error - To log an unrecoverable exception or state
To access the information from your application then add into the app.config (or web.config) the following:
<system.diagnostics>
<trace autoflush="false" indentsize="4">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\mylogfile.log" />
</listeners>
</trace>
<switches>
<add name="My.Namespace.Class" value="4"/>
</switches>
</system.diagnostics>
You can also attach listeners for publishing to the eventlog or anywhere else that interests you. More information on the tracing framework can be found at:
http://msdn.microsoft.com/en-us/library/ms733025.aspx