Monday, August 28, 2006

A simple logging function

I love to use the built in tracing functionality. However, sometimes I want logging for a sublevel .dll that I do not want to attach to the standard tracing files. Whenever I encounter this kind of a situation I fall back on a simple WriteToLogFile function that I wrote a long time ago. I usually have the .dll know about it's own log file and call this function from a helper .dll This has proven a very useful function when I need to log in .dlls without relying on a Tracing being turned on.

static public void WriteToLogFile(string filename, string output)
{

System.IO.FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.Read);
Byte[] OutputArray = new Byte[output.Length];
for ( int i=0; i < output.Length; i++)
{
OutputArray[i] = Convert.ToByte(output[i]);
}
fs.Write(OutputArray, 0, OutputArray.Length);
fs.Close();
}

No comments: