Menu
Carl Camera

Poor Man's Logger

I saw a coworker use this technique the other day and thought it clever. I'm sure he's not the first to use it, but it can come in handy especially in static functions without a formal logger.

This C# code will generate null files in your "temp" directory. The filenames will be sequenced based on a timestamp at the front, then the information you're tracking behind.

The Regex.Replace is necessary to weed out the characters that Windows does not allow in filenames, and the length check keeps filenames under 255 characters.

void PoorMansLogger(string infotolog)
{
  string ts = System.DateTime.Now.Ticks.ToString();
  ts = ts + "_" + Regex.Replace(infotolog,@"[^\w\.\-]",@"_");
  if (ts.Length > 245) ts = ts.Substring(0,245);
  try
  {
    System.IO.File.Create(@"c:\temp\" + ts );
  }
  catch
  {
  }
  return;
}

This certainly isn't anything you'd want to do on a regular basis but if there's something weird going on and stepping through code doesn't seem practical, this might help.

Comments