您的位置:首页 > 编程语言 > C#

C# 写日志文件

2012-09-21 10:54 148 查看
public static void WriteLog(string txt)

{

try

{

string path = Application.StartupPath + @"\log\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";

if (!Directory.Exists(path))

{

Directory.CreateDirectory(path);

}

path += DateTime.Now.ToString("yyyyMMdd") + "-" + DateTime.Now.ToString("HH") + ".txt";

if (!File.Exists(path))

{

File.Create(path);

}

FileStream fs;

StreamWriter sw;

fs = new FileStream(path, FileMode.Append);

sw = new StreamWriter(fs, Encoding.Default);

sw.Write(DateTime.Now.ToString("HH:mm:ss") + " " + txt + "\r\n");

sw.Close();

fs.Close();

}

catch (Exception ex)

{

WriteLog("程序发生异常(WriteLog)。详情:" + ex.Message);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐