做个简单的程序日志记录文件
2016-07-19 15:22
465 查看
public class LogHelper
{
/// <summary>
/// 写入异常信息记录处理
/// </summary>
/// <param name="ex">异常信息</param>
private static void WriteException(string ex)
{
try
{
//首先删除24小时以前的错误日志
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(GetRootPath() + "/Log");
if (dir.Exists)
{
DateTime nt = DateTime.Now.AddDays(-1);
foreach (System.IO.FileInfo f in dir.GetFiles())
{
if (f.CreationTime < nt)
f.Delete();
}
}
else
{
Directory.CreateDirectory(GetRootPath() + "/Log");
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("\n\r-----------------------");
stringBuilder.AppendFormat("{0},异常信息:-----------------------\n\r", DateTime.Now);
stringBuilder.AppendFormat("\n\r {0}", ex);
FileStream fs = new FileStream(string.Format(@"{0}\\{1}/errorLog.txt", GetRootPath(),"Log"), FileMode.Append);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
stringBuilder.AppendFormat("\n\r\n\r");
sw.Write(stringBuilder.ToString());
sw.Dispose();
fs.Dispose();
}
catch { }
}
/// <summary>
/// 取得网站根目录的物理路径
/// </summary>
/// <returns></returns>
private static string GetRootPath()
{
string AppPath = "";
AppPath = AppContext.BaseDirectory;
if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)
AppPath = AppPath.Substring(0, AppPath.Length - 1);
return AppPath;
}
public static void SetBusinessExceptionLog(object str)
{
WriteException(str.ToString());
}
public static void SetExceptionLog(object str)
{
WriteException(str.ToString());
}
}
{
/// <summary>
/// 写入异常信息记录处理
/// </summary>
/// <param name="ex">异常信息</param>
private static void WriteException(string ex)
{
try
{
//首先删除24小时以前的错误日志
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(GetRootPath() + "/Log");
if (dir.Exists)
{
DateTime nt = DateTime.Now.AddDays(-1);
foreach (System.IO.FileInfo f in dir.GetFiles())
{
if (f.CreationTime < nt)
f.Delete();
}
}
else
{
Directory.CreateDirectory(GetRootPath() + "/Log");
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("\n\r-----------------------");
stringBuilder.AppendFormat("{0},异常信息:-----------------------\n\r", DateTime.Now);
stringBuilder.AppendFormat("\n\r {0}", ex);
FileStream fs = new FileStream(string.Format(@"{0}\\{1}/errorLog.txt", GetRootPath(),"Log"), FileMode.Append);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
stringBuilder.AppendFormat("\n\r\n\r");
sw.Write(stringBuilder.ToString());
sw.Dispose();
fs.Dispose();
}
catch { }
}
/// <summary>
/// 取得网站根目录的物理路径
/// </summary>
/// <returns></returns>
private static string GetRootPath()
{
string AppPath = "";
AppPath = AppContext.BaseDirectory;
if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)
AppPath = AppPath.Substring(0, AppPath.Length - 1);
return AppPath;
}
public static void SetBusinessExceptionLog(object str)
{
WriteException(str.ToString());
}
public static void SetExceptionLog(object str)
{
WriteException(str.ToString());
}
}
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 如何在 Linux/Windows/MacOS 上使用 .NET 进行开发
- 如何在 Linux 中安装微软的 .NET Core SDK
- 星外ASP.Net的安全设置相关说明
- C#.NET获取拨号连接的宽带连接方法
- C#、ASP.NET通用扩展工具类之TypeParse
- C#.Net ArrayList的使用方法
- 实现ASP.NET无刷新下载并提示下载完成的开发思路
- PowerShell中使用.NET将程序集加入全局程序集缓存
- C#、ASP.NET通用扩展工具类之LogicSugar
- .net(c#)中的new关键字详细介绍
- 由vbs sort引发.NET Framework之间的关系说明
- C#难点逐个击破(6):C#数据类型与.net framework数据类型
- C#、ASP.NET通用工具类IsWhat?(可以判断数字、身份证、数据类型等等)
- 程序中常用的种代码
- .NET中的async和await关键字使用及Task异步调用实例
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- 基于.NET平台常用的框架和开源程序整理
- 在ASP.NET 2.0中操作数据之六十六:在TableAdapters中使用现有的存储过程
- RabbitMQ入门与使用篇 推荐