Error handling and logging

Server-side logging

While developing we strongly recommend to use logs for errors detection.

//attaches begin and end events to enable and disable logging in them
connector.Begin += new EventHandler(connector_Begin);
connector.End += new EventHandler(connector_End);
 
//we will store log content here
private StringBuilder _LogContent = new StringBuilder();
 
void connector_End(object sender, EventArgs e)
{
   Log.Enabled = false;//stop logging
}
 
void connector_Begin(object sender, EventArgs e)
{
   //enable logging and add listener to it
   Log.Enabled = true;
   Log.Listeners.Add(new TextWriterTraceListener(new StringWriter(this._LogContent)));
}

Log object is a wrapper over the System.Diagnostics.Trace class, thus you can use standard .NET Framework techniques for tracing. To start logging you need to set the Log's Enabled property to true and add one or more listeners to its Listeners collection. You can read more about TraceListeners here.

The example above writes log messages into the local variable (_LogContent). Alternatively, you can use console or file system as messages' destination. What's more, you can see the log messages in Microsoft Visual Studio's Output window ("Show output from: Debug") when you run application in the debug mode.

Adding custom records to the log

During the development process, you may need to write some custom data to the log (can be useful for custom server-side events), in such case you can use the default log as:

Log.WriteLine(this, "Message");

The first parameter is the reference to the object which writes message to the log. The second one is the message itself.

Back to top