StreamWriter在新的一天不更新其路径 [英] StreamWriter not updating its path on new day

查看:77
本文介绍了StreamWriter在新的一天不更新其路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序正在写入名为"appname_yyyyMMdd.log"的日志文件,其中appname是我的应用程序的名称,而yyyyMMdd是当前日期;样本日志文件名称可能是"loglistener_20110615.log".无论如何,我的应用程序会很好地创建日志文件,并按计划对其进行更新.但是,日期更改后,该应用程序将不会记录任何内容,也不会创建新文件.换句话说,由于今天是6/15,所以我需要它在今晚午夜之后创建一个名为"loglistener_20110616.log"的文件,并且需要它继续记录到该新文件.

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.

以下是代码摘录:

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).

更新-以防万一谷歌用户登陆此页面,后来我发现了一个与此完全不同的原因.我的日志正在记录来自电子邮件监听服务的信息.该服务本身存在一个问题,即半小时后超时.因此,我的问题不是w/CreateText/AppendText ...我的问题是没有什么可记录的.很烦人,但我希望其他人不会被这个问题/答案所误导.

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.

推荐答案

您应检查以确保该文件首先存在.

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

File.AppendText文档

类型:System.IO.StreamWriter A 附加UTF-8的StreamWriter 编码的文本到现有文件.

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

}

尝试尝试

查看评论并重新阅读文档后.

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

File.AppendText

无论文件是否存在,都应始终工作.

Should always work, regardless of file existence.

这篇关于StreamWriter在新的一天不更新其路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆