Amorphous
Amorphous

Reputation: 697

Log file gets created but remains empty?

I implemented a Utility class in my Windows Form app with a log method. It seems to be creating the log.txt file fine, but not writing anything to it. No other programs are using this particular text file.

using System;
using System.IO;
using System.Text;


namespace program1{
    static class Utils {

        static Utils() { }


        private static readonly string FilePath = TestEnvironment.PATH + @"\log.txt";

        private static void CheckFile()
        {
            if (File.Exists(FilePath)) return;

            using (FileStream fs = File.Create(FilePath)) {
                Byte[] info = new UTF8Encoding(true).GetBytes("");
                fs.Write(info, 0, info.Length);

                fs.Close();
            }

        }

        public static string Log(string code, string message) {

            StreamWriter _w = File.AppendText(FilePath);

            CheckFile();

            string log = ("\r\n" + code + ": \n");
            log += String.Format("{0} {1}\n", DateTime.Now.ToLongTimeString(),
                DateTime.Now.ToLongDateString());
            log += String.Format("  :{0}\n", message);
            log += String.Format("-------------------------------");
            _w.WriteLine(log);
            _w.Close();
            return log;
        }

        public static string LogDump() {

            StreamReader _r = File.OpenText(FilePath);
            string output = "";

            string line;
            while ((line = _r.ReadLine()) != null) {
                output += line;
            }
            _r.Close();

            return output;
        }



    }
}

Is it perhaps not liking the String.Formats?

Upvotes: 0

Views: 515

Answers (2)

David Crowell
David Crowell

Reputation: 3813

You don't need the CheckFile() method. AppendText() will create the file if necessary.

The real problem is how you're writing the file. Change your method to this:

public static string Log(string code, string message)
{
    string log;
    using (var writer = File.AppendText(FilePath))
    {
        log = ("\r\n" + code + ": \n");
        log += String.Format("{0} {1}\n", DateTime.Now.ToLongTimeString(),
            DateTime.Now.ToLongDateString());
        log += String.Format("  :{0}\n", message);
        log += String.Format("-------------------------------");
        writer.WriteLine(log);
    }

    return log;
}

To clarify, the using block calls Dispose() on the StreamWriter. This flushes content to the file.

Upvotes: 2

Grant Winney
Grant Winney

Reputation: 66449

According to MSDN:

A stream’s encoder is not flushed unless you explicitly call Flush or dispose of the object.

Either dispose the StreamWriter instance you're creating (ideally by enclosing it in a using block, or alternatively by explicitly calling Dispose()):

using (StreamWriter _w = File.AppendText(FilePath))
{
    ...
}

Or explicitly call Flush():

_w.Flush();

Upvotes: 3

Related Questions