这个日志文件存放在程序的bin文件夹下得Debug中
static private string logpathlog = AppDomain.CurrentDomain.BaseDirectory + "log.txt";
static public void writelog(string classname)
{
string path = logpathlog;
if (!File.Exists(path))
{
// Create a file to write to.
using (File.Create(path)) { }
}
FileInfo fileinfo = new FileInfo(path);
if (fileinfo.Length > 1024 * 1024 * 2)
{
File.Move(path, AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyyyMMddHHmmss") + "log.txt");
if (!File.Exists(path))
{
using (File.Create(path)) { }
}
}
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + "\t\n");
sw.WriteLine(classname + "\t\n");
sw.WriteLine("------------------------------------------------------------------------" + "\t\n");
sw.Close();
}
}
本文详细介绍了如何在程序中实现日志文件的管理与操作,包括日志路径设置、文件创建、日志大小限制及日志内容的写入等关键步骤。
1016

被折叠的 条评论
为什么被折叠?



