错误日志写入和管理

前台界面:      

  
        
前台的代码比较简单,看上图,所以就不写出来了.都是调用下面的方法得到的.
DropDownList绑定  

ddlErrorFile.DataSource = TSWebControls.TSError.GetErrorFileList();
ddlErrorFile.DataTextField = "fileName";
ddlErrorFile.DataValueField = "filePath";

代码:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 提供经常需要使用的一些出错处理
ExpandedBlockEnd.gif
/// </summary>

None.gif public   class  TSError
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 日志文件的扩展名,最好是一些客户端禁止请求的文件扩展名.如:.config
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    private static string fileExtend = "printer";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 将错误写入一个文本文件,日志年月分类
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <param name="msg">出错的信息</param>  

InBlock.gif    public static void ToLog(string msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string fileName = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "." + fileExtend;
InBlock.gif        
string filePath = System.Web.HttpContext.Current.Server.MapPath
InBlock.gif                        (System.Web.HttpContext.Current.Request.ApplicationPath
InBlock.gif                        
+ "/Error/" + fileName);
InBlock.gif
InBlock.gif        
if (!File.Exists(fileName)) File.CreateText(fileName);
InBlock.gif
InBlock.gif        StreamWriter sw 
= File.AppendText(filePath);
InBlock.gif        sw.WriteLine(
"日期:" + DateTime.Now.ToString());
InBlock.gif        sw.WriteLine(
"內容:" + msg);
InBlock.gif        sw.WriteLine(
"--------------------------------------------------");
InBlock.gif        sw.WriteLine();
InBlock.gif        sw.Flush();
InBlock.gif        sw.Close();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 得到错误日志文件的所有文件列表,把列表绑定到DropDownList
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <returns>返回文件列表</returns>

InBlock.gif    public static System.Collections.ArrayList GetErrorFileList()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Collections.ArrayList fileDirs 
= new System.Collections.ArrayList();
InBlock.gif
InBlock.gif        
string dirs = System.Web.HttpContext.Current.Server.MapPath
InBlock.gif                        (System.Web.HttpContext.Current.Request.ApplicationPath 
InBlock.gif                        
+ "/Error");
InBlock.gif        
string[] errorFiles = Directory.GetFiles(dirs,"*." + fileExtend);
InBlock.gif
InBlock.gif        
foreach (string dir in errorFiles)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string fileName = dir.Substring(dir.LastIndexOf("\\"+ 1 );
InBlock.gif            fileName 
= fileName.Substring(0,fileName.LastIndexOf("."));
InBlock.gif            
string filePath = dir;
InBlock.gif            FileItem FI 
= new FileItem(fileName,filePath);
InBlock.gif            fileDirs.Add(FI);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return fileDirs;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 查看某个日志文件的内容
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="filePath">文件路径</param>
ExpandedSubBlockEnd.gif    
/// <returns>返回文件内容</returns>

InBlock.gif    public static string GetErrorContent(string filePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string fileContent = string.Empty;
InBlock.gif        StreamReader SR 
= new StreamReader(filePath);   
InBlock.gif        fileContent 
=  SR.ReadToEnd();
InBlock.gif        SR.Close();
InBlock.gif
InBlock.gif        
return fileContent;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 删除日志文件
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="FileName">文件名</param>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

InBlock.gif    public static bool DelErrorFile(string FileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string filePath = System.Web.HttpContext.Current.Server.MapPath
InBlock.gif                (System.Web.HttpContext.Current.Request.ApplicationPath 
InBlock.gif                
+ "/Error/" + FileName + "." + fileExtend);
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            File.Delete(filePath);
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif
/// 临时转换日志文件,保存文件名和文件路径
ExpandedBlockEnd.gif
/// </summary>

None.gif class  FileItem
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private string fileName;
InBlock.gif    
private string filePath;
InBlock.gif
InBlock.gif    
public FileItem(string fileName,string filePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.fileName = fileName;
InBlock.gif        
this.filePath = filePath;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public string FileName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return fileName;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            fileName 
= value;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public string FilePath
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return filePath;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            filePath 
= value;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


如果哪里有写的不好的地方,请各位指出,各位的批评也是我水平提高的捷径.

转载于:https://www.cnblogs.com/lxxnet/archive/2006/08/07/470217.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值