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());
}
}