How to do a Xna Log file

Although the title says XNA log file this is actually a C# log file, I’ve just thrown this title cause lot’s of people search for xna log instead of C# log file.

Many have asked me why take time to do a log file when you can throw exceptions when something goes wrong. Well the answer is simple, to keep track of what’s happening, log steps, write to the file exactly what when wrong even if you are on Release mode, and more, if someone complains the game is crashing you simply ask for the log file and see what when wrong.

I’ve recently build one for my engine Basalt so I decided to share the result, start by creating log type:

public enum Log_Type
    {
        ERROR = 0,
        WARNING = 1,
        INFO = 2
    }

Next, the class itself:

 public static class Logger
   {
        static protected bool              _active;  // In case you want to deactivate the logger
 
        static public init()
        {
            _active = true;
#if WINDOWS
 
            StreamWriter textOut = new StreamWriter(new FileStream("log.html", FileMode.Create, FileAccess.Write));
            textOut.WriteLine("Log File");
            textOut.WriteLine("");
            textOut.WriteLine("<span style="font-family: &quot;Kootenay&quot;; color: #000000;">");
            textOut.WriteLine("Log started at " + System.DateTime.Now.ToLongTimeString()+"</span>
<hr />");
            textOut.Close();
#endif
        }
 
        static public bool Active
        {
            get { return _active; }
            set { _active = value; }
        }
 
        static public void log(Log_Type type, string text)
        {
#if WINDOWS
            if (!_active) return;
            string begin = "";
            switch (type)
            {
                case Log_Type.ERROR: begin = "<span style="color: #00f000;">"; break;
                case Log_Type.INFO: begin = "<span style="color: #0008f0;">"; break;
                case Log_Type.WARNING: begin = "<span style="color: #00ff00;">"; break;
            }
            text = begin+System.DateTime.Now.ToLongTimeString() + " : " + text + "</span>";
            Output(text);
#endif
        }
 
        static private void Output(string text)
        {
#if WINDOWS
            try
            {
                StreamWriter textOut = new StreamWriter(new FileStream("log.html", FileMode.Append, FileAccess.Write));
                textOut.WriteLine(text);
                textOut.Close();
            }
            catch (System.Exception e)
            {
                string error = e.Message;
            }
 
#endif
        }
    }
</span></span>

For using just do on your game constructor:

<span style="color: #00f000;"><span style="color: #0008f0;"> Logger.init();
</span></span>

After this since it’s a static class you can do wherever you want a log, something like this:

<span style="color: #00f000;"><span style="color: #0008f0;"> Logger.log(Log_Type.INFO,"Resolution Changed");
 Logger.log(Log_Type.ERROR,"Unable to find texture XPTO");
</span></span>

This will output a log.html file that you can open with your favorite browser.

basalt_log