C# 写txt 文档日志
2014-06-03 14:36
176 查看
public static void ErrorLog(string mssg)
{
//string FilePath = System.Web.HttpContext.Current.Server.MapPath("log/ErrorLog.txt");
string FilePath = MapPath("../../log/ErrorLog.txt");
try
{
if (File.Exists(FilePath))
{
using (StreamWriter tw = File.AppendText(FilePath))
{
tw.WriteLine(DateTime.Now.ToString() + "> " + mssg);
}
}
else
{
TextWriter tw = new StreamWriter(FilePath);
tw.WriteLine(DateTime.Now.ToString() + "> " + mssg);
tw.Flush();
tw.Close();
tw = null;
}
}
catch (Exception)
{
}
#endregion
}
public static string MapPath(string strPath)
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("\\"))
{
//strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
strPath = strPath.TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}
public static void ErrorLog(string mssg)
{
//string FilePath = System.Web.HttpContext.Current.Server.MapPath("log/ErrorLog.txt");
string FilePath = MapPath("../../log/ErrorLog.txt");
try
{
if (File.Exists(FilePath))
{
using (StreamWriter tw = File.AppendText(FilePath))
{
tw.WriteLine(DateTime.Now.ToString() + "> " + mssg);
}
}
else
{
TextWriter tw = new StreamWriter(FilePath);
tw.WriteLine(DateTime.Now.ToString() + "> " + mssg);
tw.Flush();
tw.Close();
tw = null;
}
}
catch (Exception)
{
}
#endregion
}
public static string MapPath(string strPath)
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("\\"))
{
//strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
strPath = strPath.TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}
相关文章推荐
- 将TXT文档中数据存入数组中 C#
- 【转自百度文档】用C#操作TXT格式文档
- c# 读取记事本txt文档到DataTable中
- C#实现EXCEL数据到TXT文档的转换
- C# 简单的往txt中写日志,调试时很有用(转载)
- C#实现EXCEL数据转换到TXT文档
- C#提取TXT文档指定内容
- 获取txt文本文档的编码类型(c++,c#)
- 『C#基础』C#读写TXT文档
- c#读写txt文本文档
- c#读写txt文档代码
- 解决C#中txt文档导入数据库时,中文显示乱码的问题
- C# 对文本文档进行内容修改 作为日志文件
- C#通过LPT控制打印机——txt文档读取指令打印条码
- C#编程清空txt文本文档中内容 不是删除
- 将TXT文档中数据导入XML文件中 C#
- 记录系统错误日志,并保存成文本文档(.txt)
- C#多文档 含父窗体 Word文档读取文本 转换txt
- C#判断txt文档codepage
- C# 保存文本txt 保存日志 保存.txt