错误日志写入和管理
前台界面:
前台的代码比较简单,看上图,所以就不写出来了.都是调用下面的方法得到的.
DropDownList绑定
ddlErrorFile.DataSource = TSWebControls.TSError.GetErrorFileList();
ddlErrorFile.DataTextField = "fileName";
ddlErrorFile.DataValueField = "filePath";
代码:
/// <summary>
/// 提供经常需要使用的一些出错处理
/// </summary>
public class TSError
{
/// <summary>
/// 日志文件的扩展名,最好是一些客户端禁止请求的文件扩展名.如:.config
/// </summary>
private static string fileExtend = "printer";
/// <summary>
/// 将错误写入一个文本文件,日志年月分类
/// </summary>
/// <param name="msg">出错的信息</param>
public static void ToLog(string msg)
{
string fileName = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "." + fileExtend;
string filePath = System.Web.HttpContext.Current.Server.MapPath
(System.Web.HttpContext.Current.Request.ApplicationPath
+ "/Error/" + fileName);
if (!File.Exists(fileName)) File.CreateText(fileName);
StreamWriter sw = File.AppendText(filePath);
sw.WriteLine("日期:" + DateTime.Now.ToString());
sw.WriteLine("內容:" + msg);
sw.WriteLine("--------------------------------------------------");
sw.WriteLine();
sw.Flush();
sw.Close();
}
/// <summary>
/// 得到错误日志文件的所有文件列表,把列表绑定到DropDownList
/// </summary>
/// <returns>返回文件列表</returns>
public static System.Collections.ArrayList GetErrorFileList()
{
System.Collections.ArrayList fileDirs = new System.Collections.ArrayList();
string dirs = System.Web.HttpContext.Current.Server.MapPath
(System.Web.HttpContext.Current.Request.ApplicationPath
+ "/Error");
string[] errorFiles = Directory.GetFiles(dirs,"*." + fileExtend);
foreach (string dir in errorFiles)
{
string fileName = dir.Substring(dir.LastIndexOf("\\") + 1 );
fileName = fileName.Substring(0,fileName.LastIndexOf("."));
string filePath = dir;
FileItem FI = new FileItem(fileName,filePath);
fileDirs.Add(FI);
}
return fileDirs;
}
/// <summary>
/// 查看某个日志文件的内容
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns>返回文件内容</returns>
public static string GetErrorContent(string filePath)
{
string fileContent = string.Empty;
StreamReader SR = new StreamReader(filePath);
fileContent = SR.ReadToEnd();
SR.Close();
return fileContent;
}
/// <summary>
/// 删除日志文件
/// </summary>
/// <param name="FileName">文件名</param>
/// <returns></returns>
public static bool DelErrorFile(string FileName)
{
string filePath = System.Web.HttpContext.Current.Server.MapPath
(System.Web.HttpContext.Current.Request.ApplicationPath
+ "/Error/" + FileName + "." + fileExtend);
try
{
File.Delete(filePath);
return true;
}
catch
{
return false;
}
}
}
/// <summary>
/// 临时转换日志文件,保存文件名和文件路径
/// </summary>
class FileItem
{
private string fileName;
private string filePath;
public FileItem(string fileName,string filePath)
{
this.fileName = fileName;
this.filePath = filePath;
}
public string FileName
{
get
{
return fileName;
}
set
{
fileName = value;
}
}
public string FilePath
{
get
{
return filePath;
}
set
{
filePath = value;
}
}
}
/// 提供经常需要使用的一些出错处理
/// </summary>
public class TSError
{
/// <summary>
/// 日志文件的扩展名,最好是一些客户端禁止请求的文件扩展名.如:.config
/// </summary>
private static string fileExtend = "printer";
/// <summary>
/// 将错误写入一个文本文件,日志年月分类
/// </summary>
/// <param name="msg">出错的信息</param>
public static void ToLog(string msg)
{
string fileName = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "." + fileExtend;
string filePath = System.Web.HttpContext.Current.Server.MapPath
(System.Web.HttpContext.Current.Request.ApplicationPath
+ "/Error/" + fileName);
if (!File.Exists(fileName)) File.CreateText(fileName);
StreamWriter sw = File.AppendText(filePath);
sw.WriteLine("日期:" + DateTime.Now.ToString());
sw.WriteLine("內容:" + msg);
sw.WriteLine("--------------------------------------------------");
sw.WriteLine();
sw.Flush();
sw.Close();
}
/// <summary>
/// 得到错误日志文件的所有文件列表,把列表绑定到DropDownList
/// </summary>
/// <returns>返回文件列表</returns>
public static System.Collections.ArrayList GetErrorFileList()
{
System.Collections.ArrayList fileDirs = new System.Collections.ArrayList();
string dirs = System.Web.HttpContext.Current.Server.MapPath
(System.Web.HttpContext.Current.Request.ApplicationPath
+ "/Error");
string[] errorFiles = Directory.GetFiles(dirs,"*." + fileExtend);
foreach (string dir in errorFiles)
{
string fileName = dir.Substring(dir.LastIndexOf("\\") + 1 );
fileName = fileName.Substring(0,fileName.LastIndexOf("."));
string filePath = dir;
FileItem FI = new FileItem(fileName,filePath);
fileDirs.Add(FI);
}
return fileDirs;
}
/// <summary>
/// 查看某个日志文件的内容
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns>返回文件内容</returns>
public static string GetErrorContent(string filePath)
{
string fileContent = string.Empty;
StreamReader SR = new StreamReader(filePath);
fileContent = SR.ReadToEnd();
SR.Close();
return fileContent;
}
/// <summary>
/// 删除日志文件
/// </summary>
/// <param name="FileName">文件名</param>
/// <returns></returns>
public static bool DelErrorFile(string FileName)
{
string filePath = System.Web.HttpContext.Current.Server.MapPath
(System.Web.HttpContext.Current.Request.ApplicationPath
+ "/Error/" + FileName + "." + fileExtend);
try
{
File.Delete(filePath);
return true;
}
catch
{
return false;
}
}
}
/// <summary>
/// 临时转换日志文件,保存文件名和文件路径
/// </summary>
class FileItem
{
private string fileName;
private string filePath;
public FileItem(string fileName,string filePath)
{
this.fileName = fileName;
this.filePath = filePath;
}
public string FileName
{
get
{
return fileName;
}
set
{
fileName = value;
}
}
public string FilePath
{
get
{
return filePath;
}
set
{
filePath = value;
}
}
}
如果哪里有写的不好的地方,请各位指出,各位的批评也是我水平提高的捷径.