public string LogFile = "";
/// <summary>
/// 在Logs文件夹(不存在则自动创建)下创建一个日志文件
/// </summary>
/// <param name="FileName"></param>
public LogHelper(string FileName)
{
string directory = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
LogFile = directory + "\\" + FileName + ".txt";
if (!File.Exists(LogFile))
{
FileStream fs = File.Create(LogFile);
fs.Close();
}
}
/// <summary>
/// 追加一条信息
/// </summary>
/// <param name="text"></param>
public void Write(string text)
{
using (StreamWriter sw = new StreamWriter(LogFile, true, Encoding.UTF8))
{
sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
}
}
/// <summary>
/// 追加一行信息
/// </summary>
/// <param name="text"></param>
public void WriteLine(string text)
{
text += "\r\n";
using (StreamWriter sw = new StreamWriter(LogFile, true, Encoding.UTF8))
{
sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
}
}
日志文件-轮子
最新推荐文章于 2022-06-13 14:22:12 发布