1

I have a program that's writing to a log file called "appname_yyyyMMdd.log", where appname is the name of my app, and yyyyMMdd is the current date; a sample log file name might be "loglistener_20110615.log" . Anyway, my app creates the log file fine, and it updates it as planned. However, once the date changes, the app doesn't log anything, and it doesn't create a new file. In other words, since today is 6/15, I need it to create a file called "loglistener_20110616.log" after midnight tonight, and I need it to continue logging to that new file.

Here are code excerpts:

public static void LogInfo(string format, params object[] args)
{
    lock (_logLock)
    {
        using (StreamWriter sw = File.AppendText(GetLogPath()))
        {
            sw.WriteLine(GetTimeStamp() + String.Format(format, args));
        }
    }
}

private static string GetLogPath()
{
    string appName = "loglistener";
    string today = DateTime.Today.ToString("yyyyMMdd");
    string fileName = appName + "_" + today + ".log";
    string fullLogPath = AppDomain.CurrentDomain.BaseDirectory + fileName;

    return fullLogPath;
}

I checked this similar question, but that question describes a different scenario (with a non-applicable fix).

UPDATE - Just in case googlers land on this page, I later discovered a different cause altogether w/ this. My log is logging info from an email-listening service. The service itself had a problem where it was timing out after a half-hour. So, my problem wasn't w/ CreateText / AppendText... My problem was there was nothing to log. Very annoying, but I hope other people won't be misled by this question/answer.

Community
  • 1
  • 1
WEFX
  • 8,298
  • 8
  • 66
  • 102
  • 1
    I'm assuming you don't get any exceptions (e.g. permissions). After the date changes, do the messages go to "old" (previous day's file) or are they just "lost"? – Randy Levy Jun 15 '11 at 18:50
  • I've fixed it using @msarchet's answer, but to answer your question, there were no exceptions, and the messares are just "lost". They weren't appearing on the previous day's log. Again, it's fixed now. Thanks for helping. – WEFX Jun 15 '11 at 19:28
  • You might want to use File.AppendAllText(...) if you're just doing a single write operation. It will create the file if it doesn't exist, write, and close the file. http://msdn.microsoft.com/en-us/library/ms143356%28v=VS.100%29.aspx – The Moof Jun 15 '11 at 20:11
  • Just a note that I was not able to reproduce your behavior given the code above and the description of the problem. So it seems like there is something else at work here (environment, configuration, some other code). – Randy Levy Jun 16 '11 at 15:56

1 Answers1

1

You should check to make sure that the file exists first.

From the File.AppendText Documentation

Type: System.IO.StreamWriter A StreamWriter that appends UTF-8 encoded text to an existing file.

public static void LogInfo(string format, params object[] args)
{
    lock (_logLock)
    {
        using (StreamWriter sw = File.Exists(GetLogPath) ? File.AppendText(GetLogPath()) : File.CreateText(GetLogPath())
        {
            sw.WriteLine(GetTimeStamp() + String.Format(format, args));
        }
    }

}

Try that instead

After looking at the comments and re-reading the documentation.

File.AppendText

Should always work, regardless of file existence.

msarchet
  • 15,104
  • 2
  • 43
  • 66
  • I had done something similar earlier, but Create leaves the stream open, and it was creating errors when I immediately tried writing to it. I can give this a shot though... – WEFX Jun 15 '11 at 18:33
  • @WEFX the using block should close and dispose of the streamwriter for you. – msarchet Jun 15 '11 at 18:34
  • I don't think it's file existence. From the [documentation](http://msdn.microsoft.com/en-us/library/system.io.file.appendtext.aspx): "If the file specified by path does not exist, it is created." – Randy Levy Jun 15 '11 at 18:35
  • I think @Tuzo is correct - see [this answer](http://stackoverflow.com/questions/2781357/file-being-used-by-another-process-after-using-file-create) – WEFX Jun 15 '11 at 18:39
  • However, the Microsoft example on the aforementioned [documentation page](http://msdn.microsoft.com/en-us/library/system.io.file.appendtext.aspx) uses an if-else statement and calls CreateText if the file does not exist, as opposed to AppendText, so @msarchet could be onto something... – WEFX Jun 15 '11 at 18:44
  • @WEFX: Yes, documentation can be wrong but in this case it isn't. :) Run a test; you've probably already tested it -- I assume the first time your app runs it created a file. – Randy Levy Jun 15 '11 at 18:48
  • The first time my app runs, it creates a file. The problem is, if I leave the app running, a new file does NOT get created at midnight. Using @msarchet's answer worked. Thanks for the help, both of you. – WEFX Jun 15 '11 at 19:27
  • @WEFX: That behavior seems very strange to me. Weird. Also, did you use the code exactly as above with 3 calls to `GetLogPath`? If so, then you could hit an issue because `GetLogPath` calls `DateTime.Today` so it's possible for `File.Exists` to return true but then before the call to `File.AppendText` the date changes so the next call to `GetLogPath` returns a new file name that doesn't actually exist and you end up in the same situation as you are having now. Better to call `GetLogPath` once. – Randy Levy Jun 15 '11 at 19:54