StreamWriter does not update its path on a new day

I have a program that writes a log file called "appname_yyyyMMdd.log", where appname is the name of my application and yyyyMMdd is the current date; The exemplary log file name may be "loglistener_20110615.log". In any case, my application perfectly creates a log file and updates it as planned. However, after changing the date, the application does not register anything and does not create a new file. In other words, since today it is 6/15, I need to create a file called "loglistener_20110616.log" after midnight today, and I need it to continue to be included in this new file.

Here are the code snippets:

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;
}

, ( ).

. , , . . , . , , W/CreateText/AppendText... , log log . , , /.

+1
1

, .

File.AppendText

: System.IO.StreamWriter A StreamWriter, UTF-8 .

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));
        }
    }

}

.

File.AppendText

, .

+1

All Articles