Jump to content
  • Advertisement
Sign in to follow this  
Daniel Miller

extreme C# frustration x_x

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Since VS 2005 express seemingly can't enable unmanaged debugging, I decided to write a LogFile class:
namespace MapEditor
{
    sealed class LogFile
    {
        private LogFile()
        {
            log = new StreamWriter(File.Open(@"C:\Log.txt", FileMode.Append));
            log.WriteLine("Log start");
        }

        ~LogFile()
        {
            log.Close();
        }

        public static void AppendInfo(string text)
        {
            log.WriteLine(text);
        }

        public static LogFile Instance
        {
            get
            {
                return instance;
            }
        }

        private static StreamWriter log;
        private static readonly LogFile instance = new LogFile();
    }
}












I created a blank file at that location (so the log could append to it). I noticed that none of my log statements were outputting (size of the text file was 0), so I decided to append to the file while still inside the constructor. Nothing. If I add Close() to the end of the constructor, it works. I realize that Finalize doesn't call right away, but how am I supposed to do this? What the heck is going on? Why doesn't it call Close()? [Edited by - Daniel Miller on July 20, 2005 3:46:25 PM]

Share this post


Link to post
Share on other sites
Advertisement
I always manually call Flush after every write when I'm outputting to a debugging log. If your program completely eats hell and dies, the file should still end up containing everything that you requested to Write.

Also, instead of using that type of singleton, try a static class instead:


namespace MapEditor
{
static class LogFile
{
static LogFile()
{
log = new StreamWriter(File.Open(@"C:\Log.txt", FileMode.Append));
log.WriteLine("Log start");
log.Flush();
}

public static void AppendInfo(string text)
{
log.WriteLine(text);
log.Flush();
}

private static StreamWriter log;
}
}


Share this post


Link to post
Share on other sites
No, you would call LogThingy.Dispose() at the end of Main().
Even better is to forget about the IDisposable pattern in this case and just move file.Close() to LogThingy.Close() which you will call at the end of Main(). No difference in usage but avoids misusing IDisposable.

And Flush is good too :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Use the using clause. it will call destructor automatically !

Share this post


Link to post
Share on other sites
Quote:
Original post by Nypyren
I always manually call Flush after every write when I'm outputting to a debugging log. If your program completely eats hell and dies, the file should still end up containing everything that you requested to Write.

Also, instead of using that type of singleton, try a static class instead:

*** Source Snippet Removed ***



Wow, I've never heard of static classes... I'll look into that right now (it's sometimes good that I have these problems [wink]).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!