C#写txt格式日志方法
2017-11-24 10:10
218 查看
/// <summary>
/// 写Txt日志 到当前程序根目录
/// </summary>
/// <param name="strLog"></param>
public void WriteLog(string strLog)
{
string sFilePath = Environment.CurrentDirectory + "\\" + DateTime.Now.ToString("yyyyMM");
string sFileName = "CheckSpecialStrsLog" + DateTime.Now.ToString("dd") + ".log";
sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
if (!Directory.Exists(sFilePath))//验证路径是否存在
{
Directory.CreateDirectory(sFilePath);
//不存在则创建
}
FileStream fs;
StreamWriter sw;
if (File.Exists(sFileName))
//验证文件是否存在,有则追加,无则创建
{
fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
}
else
{
fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
}
sw = new StreamWriter(fs);
sw.WriteLine(strLog);
sw.Close();
fs.Close();
}
/// 写Txt日志 到当前程序根目录
/// </summary>
/// <param name="strLog"></param>
public void WriteLog(string strLog)
{
string sFilePath = Environment.CurrentDirectory + "\\" + DateTime.Now.ToString("yyyyMM");
string sFileName = "CheckSpecialStrsLog" + DateTime.Now.ToString("dd") + ".log";
sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
if (!Directory.Exists(sFilePath))//验证路径是否存在
{
Directory.CreateDirectory(sFilePath);
//不存在则创建
}
FileStream fs;
StreamWriter sw;
if (File.Exists(sFileName))
//验证文件是否存在,有则追加,无则创建
{
fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
}
else
{
fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
}
sw = new StreamWriter(fs);
sw.WriteLine(strLog);
sw.Close();
fs.Close();
}
相关文章推荐
- .Net下几种日志管理方法(C# 将Diagnostics.Trace 调试输出 保存到txt)
- C#日期格式处理方法
- c# 获取网页源码,自动判断编码格式新方法
- C#读写txt文件的两种方法介绍
- C#简单读写txt文件的方法
- c#快速写本地日志方法
- txt批量转换成pdf格式的方法
- Logstash处理json格式日志文件的三种方法
- C#读写txt文件的两种方法介绍 Copy:http://blog.csdn.net/ybhjx/article/details/53706715
- C#读写txt文件的两种方法介绍
- 用C#读取txt文件的方法
- C#读写txt文件的两种方法介绍
- c# 简单日志记录 txt
- C# 简单的写日志的方法
- 在C#中如何把decimal型输出成大写货币格式(非字符串代换方法)
- C#读写txt文件的两种方法介绍
- C#写日志 方法
- 将Oracle数据库中的数据导出为txt格式(方法2:)
- C#中打日志导出日志到txt文本
- C#读写txt文件的两种方法介绍