做个简单的程序日志记录文件
来源:互联网 发布:约瑟夫环 java 递归 编辑:程序博客网 时间:2024/05/16 05:21
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());
}
}
0 0
- 做个简单的程序日志记录文件
- 简单日志记录文件
- 今天查了查msdn,做了个简单的批量文件更名的程序!
- 简单的日志记录
- 记录自己做的一个简单的局域网聊天程序
- 分享个C++日志记录类以及日志记录程序
- 分享一个简单的Android日志文件记录类
- 分享一个简单的Android日志文件记录类
- 分析ngnix日志文件的开发程序记录
- log4j向多个文件记录日志
- log4j向多个文件记录日志
- 简单的日志记录类。
- android 简单的记录日志
- 利用JAVASCRIPT做个简单的文件管理器
- 开始学习使用写日志的方式来记录写程序做过的修改
- 做个简单的公告栏
- 一个简单的今天面试的sql题,做个记录
- 做了个备忘程序,记录自己的一些备注、琐事、心得之类
- UIBezierPath 画线 圆 弧
- 报表或BI的价值在哪?
- Java-----instanceof、isInstance、isAssignableFrom
- WPF性能优化经验总结
- structs2拦截器原理
- 做个简单的程序日志记录文件
- 华为路由器配置简单NAT实例
- kafka深度解析
- floor random 随机抽奖
- Bear and Three Balls
- uc/os-iii学习笔记-中断管理
- JEECG 3.6.5版本发布,企业级JAVA快速开发平台
- ImageLoader的简单分析(五)
- 欧拉回路路径求解