0

I am using StraemWriter to log text messages to a log file. The log file should be created if it doesn't exist, appended to if the file creation date is less than a given time or recreated if created before that time. I am using the class/code below

public static class LogIt
{
    private const string LOG_FNAME = @"Logfile.log";

    public static void WriteMsg(string msg)
    {
        bool append = true;
        if (File.Exists(LOG_FNAME))
        {
            //DateTime delDate = DateTime.Now.AddDays(-1);
            DateTime delDate = DateTime.Now.AddMinutes(-30);
            DateTime fileCreatedDate = File.GetCreationTime(LOG_FNAME);
            if (DateTime.Compare(fileCreatedDate, delDate) < 0)
            {
                Console.WriteLine("DELETE FILE");
                File.Delete(LOG_FNAME);
            }
        }

        using (StreamWriter sw = new StreamWriter(LOG_FNAME, append))
        {
            sw.WriteLine(msg);
        }

        Console.WriteLine(msg);
    }
}

This class is used by a simple console app run by the Task Scheduler which runs every x minutes.

The message are written as follows:

LogIt.WriteMsg("Log this message");

The messages are logged file when the file is initially created however when the file creation date is past the delete date, the file is recreated but no subsequent messages are ever written to the file.

Any ideas on why?

4

3 回答 3

1

由于某种原因,该文件具有初始创建日期(第一次创建具有该路径的文件)作为创建日期,即使它是在删除后重新创建的。您可以检查文件属性并查看日志文件创建日期始终相同。一种解决方法是在重新创建文件时在代码中更新文件创建日期。您可以为此使用 FileInfo 类。

于 2013-05-16T21:53:42.707 回答
0

我猜您是在 Windows 2003(或者可能是 XP)上运行此代码。如果是这样:当您在 T1 时间在某个目录中创建文件并删除它然后重新创建它时;惊喜惊喜它有 T1 作为创建日期!

我知道这只是因为我在 Windows 2003 上遇到了同样的问题!

顺便说一句,我现在使用 NLog 并且(恕我直言)它非常完美并且拥有我需要的一切。

于 2013-05-16T22:00:47.947 回答
0

@MPD没问题。这是我建议的解决方法的实现。试一试,让我知道这是否有效。

private const string LOG_FNAME = @"Logfile.log";

        public static void WriteMsg(string msg)
        {
            bool deleted = false;
            bool append = true;
            if (File.Exists(LOG_FNAME))
            {
                //DateTime delDate = DateTime.Now.AddDays(-1);
                DateTime delDate = DateTime.Now.AddMinutes(-30);
                DateTime fileCreatedDate = File.GetCreationTime(LOG_FNAME);
                if (DateTime.Compare(fileCreatedDate, delDate) < 0)
                {
                    Console.WriteLine("DELETE FILE");
                    File.Delete(LOG_FNAME);

                    //record that file was deleted and a new one will be created
                    deleted = true;
                }
            }

            using (StreamWriter sw = new StreamWriter(LOG_FNAME, append))
            {

                sw.WriteLine(msg);

            }

            if (deleted)
            {
                //a new file is created. Make sure the creation time is set
                FileInfo fi = new FileInfo(LOG_FNAME);
                fi.CreationTime = DateTime.Now;
            }

            Console.WriteLine(msg);
        }
于 2013-05-16T22:15:08.233 回答