的StreamWriter没有创造新的文件 [英] StreamWriter not creating new file

查看:153
本文介绍了的StreamWriter没有创造新的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每一个小时,一台服务器上运行以下code创建新的日志文件。正在创建当天的第一个日志文件,并写入到细,但那天获得创建没​​有进一步的日志文件。任何想法可能是想错了吗?没有异常抛出无论是。

 私人无效的LogMessage(信息MSG)
{
    字符串名称= _logDirectory + DateTime.Today.ToString(YYYYMMDDHH)+名.txt;

    使用(StreamWriter的SW = File.AppendText(名称))
    {
        sw.WriteLine(msg.ToString());
    }
}
 

解决方案

使用 DateTime.Today 的归零的时间的一部分。您应该使用 DateTime.Now DateTime.UtcNow ,以便返回的DateTime 包含一个小时不为零。

I'm trying to create a new log file every hour with the following code running on a server. The first log file of the day is being created and written to fine, but no further log files that day get created. Any ideas what might be going wrong? No exceptions are thrown either.

private void LogMessage(Message msg)
{
    string name = _logDirectory + DateTime.Today.ToString("yyyyMMddHH") + ".txt";

    using (StreamWriter sw = File.AppendText(name))
    {
        sw.WriteLine(msg.ToString());
    }
}

解决方案

The use of DateTime.Today zeroes the time part. You should use DateTime.Now or DateTime.UtcNow so that the returned DateTime contains an hour different than zero.

这篇关于的StreamWriter没有创造新的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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