Creating a log utility in XNA/C#
Note: This will work with standard C# or XNA on a PC, but I don't think that it will work on a XBOX 360 due to differences between the standard and the compact .NET framework. I have not been able to test this theory yet as I don't have a 360.
This is one of the simplest, and most useful utility classes that I've ever wrote.
I got the idea while nosing around the files for "Company of Heroes: Opposing Forces". COHOF like most modern games has a little "log" text file that records little tid-bits that can later be used for troubleshooting. It's a nifty little feature, and there's really no reason not to have one. (plus it took me only about a minute to code!).
just call the add method to add text, and call the compile method when your program shuts down.
I'm currently using it to have classes report when they're done loading their assets. (also useful for error reporting, but my code is so magnificent that i don't have this problem. heh )
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace yourNamespace
{
static class log
{
#region fields
private static List<string> data = new List<string>();
#endregion
//----------------------------------------------------------------------------------
//add
//----------------------------------------------------------------------------------
public static void add(string text)
{
text = System.DateTime.Now.ToLongTimeString()+" : " + text;
data.Add(text);
}
//-----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
//compile. creates log document composed of all of the collected text
//----------------------------------------------------------------------------------
public static void compile()
{
StreamWriter textOut = new StreamWriter(new FileStream("log.txt", FileMode.Create, FileAccess.Write));
foreach (string t in data)
{
textOut.WriteLine(t);
}
textOut.Close();
}
//----------------------------------------------------------------------------------
}
}