Devlib中的异常和日志处理

 

Devlib是自己封装的一个包含有数据操作模式,共用操作方法层,控件层三个组件的集合.

下面介绍Devlib中的日志处理类.封装源码如下:

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Diagnostics;

ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
/// DevLib提供的异常日志处理日志
/// </summary>

[Serializable]
public   class  DevException : Exception
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 指定用户消息
    
/// </summary>
    
/// <param name="message">用户 消息</param>

    public DevException(string message):base(message)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{        
    }
  
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 指定用户消息及异常信息
    
/// </summary>
    
/// <param name="message">用户消息</param>
    
/// <param name="innerException">系统异常</param>

    public DevException(string message, Exception innerException):base(message,innerException)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
    }
  
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 默认异常
    
/// </summary>

    public DevException()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
    }

}

ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
/// 日志操作类,日志写入过程是线程安全的
/// </summary>

public   class  EventLog
ExpandedBlockStart.gifContractedBlock.gif
{
    
static EventLog()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        BindingHelper 
= new BindingHelper<EventLogInfo>();
        LogName 
= ConfigHelper.GetAppSetting("LogName");
        LogName 
= FileHelper.GetPhysicalPath(LogName.IsNullOrEmpty() ?
            
string.Format("Log_{0}.log", DateTime.Now.ToString("yyyyMMdd")) :
            
string.Format("{0}_{1}.log", LogName, DateTime.Now.ToString("yyyyMMdd")));
        ProjectName 
= ConfigHelper.GetAppSetting("ProjectName");
        ProjectName 
= ProjectName.IsNullOrEmpty() ? "Devlib" : ProjectName;
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 日志信息解析类
    
/// </summary>

    private static BindingHelper<EventLogInfo> BindingHelper;
    
private static string LogName;
    
private static string ProjectName;
    
private static object locker = new object();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 创建提示消息日志
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <returns></returns>

    public static EventLogInfo CreateLog(string message)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return CreateLog(message, "");
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 创建操作记录日志
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <param name="userName">操作员</param>
    
/// <returns></returns>

    public static EventLogInfo CreateLog(string message, string userName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return CreateLog(message, userName, EventLogEntryType.Information);
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 创建错误记录日志
    
/// </summary>
    
/// <param name="userName">操作员</param>
    
/// <param name="e">错误详细信息</param>
    
/// <returns></returns>

    public static EventLogInfo CreateLog(string userName, Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return CreateLog("", userName, e);
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 创建可预知的错误记录日志
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <param name="userName">操作员</param>
    
/// <param name="e">错误详细信息</param>
    
/// <returns></returns>

    public static EventLogInfo CreateLog(string message, string userName, Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return CreateLog(message, userName, e, EventLogEntryType.Error);
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 创建可预知,自定义级别的用户日志
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <param name="userName">操作员</param>
    
/// <param name="type">日志级别</param>
    
/// <returns></returns>

    public static EventLogInfo CreateLog(string message, string userName, EventLogEntryType type)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        EventLogInfo logInfo 
= new EventLogInfo();
        logInfo.CustomerInfo 
= message;
        logInfo.Operator 
= userName;
        logInfo.HappenDateTime 
= DateTime.Now;
        logInfo.Type 
= type;
        logInfo.Detial 
= message;
        logInfo.Origin 
= NetHelper.GetIPAddress().ConvertString();
        
return logInfo;
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 创建可预知的,自定义级别的异常日志
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <param name="userName">操作员</param>
    
/// <param name="e">日志详细信息</param>
    
/// <param name="type">日志级别</param>
    
/// <returns></returns>

    public static EventLogInfo CreateLog(string message, string userName, Exception e, EventLogEntryType type)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        EventLogInfo logInfo 
= new EventLogInfo();
        logInfo.CustomerInfo 
= message;
        logInfo.Operator 
= userName;
        logInfo.HappenDateTime 
= DateTime.Now;
        logInfo.Type 
= type;

        logInfo.ErrorPoint 
= e.TargetSite.Module.Name;
        logInfo.FileName 
= e.TargetSite.DeclaringType.Name;
        logInfo.HelpLink 
= e.HelpLink;
        logInfo.MethodName 
= e.TargetSite.ToString();
        logInfo.ProjectName 
= e.Source;
        logInfo.Detial 
= e.StackTrace;
        logInfo.Origin 
= NetHelper.GetIPAddress().ConvertString();
        
return logInfo;
    }


    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入系统日志
    
/// </summary>
    
/// <param name="message">消息内容</param>   
    
/// <returns></returns>

    public static bool WriteToSystemLog(string message)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToSystemLog(CreateLog(message));
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入系统日志
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <param name="userName">操作员</param>
    
/// <returns></returns>

    public static bool WriteToSystemLog(string message, string userName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToSystemLog(CreateLog(message, userName));
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入系统日志
    
/// </summary>   
    
/// <param name="e">错误详细信息</param>
    
/// <returns></returns>

    public static bool WriteToSystemLog(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToSystemLog("", e);
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入系统日志
    
/// </summary>
    
/// <param name="userName">操作员</param>
    
/// <param name="e">错误详细信息</param>
    
/// <returns></returns>

    public static bool WriteToSystemLog(string userName, Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToSystemLog(CreateLog(userName,e));
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入系统日志
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <param name="userName">操作员</param>
    
/// <param name="e">错误详细信息</param>
    
/// <returns></returns>

    public static bool WriteToSystemLog(string message, string userName, Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToSystemLog(CreateLog(message, userName, e));
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入系统日志
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <param name="userName">操作员</param>
    
/// <param name="type">日志级别</param>
    
/// <returns></returns>

    public static bool WriteToSystemLog(string message, string userName, EventLogEntryType type)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToSystemLog(CreateLog(message, userName, type));
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入系统日志
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <param name="userName">操作员</param>
    
/// <param name="e">日志详细信息</param>
    
/// <param name="type">日志级别</param>
    
/// <returns></returns>

    public static bool WriteToSystemLog(string message, string userName, Exception e, EventLogEntryType type)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToSystemLog(CreateLog(message,userName,e,type));
    }

    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入文件系统
    
/// </summary>
    
/// <param name="message">消息内容</param>

    public static bool WriteToFile(string message)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToFile(CreateLog(message));
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入文件系统
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <param name="userName">操作员</param>
    
/// <returns></returns>

    public static bool WriteToFile(string message, string userName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToFile(CreateLog(message, userName));
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入文件系统
    
/// </summary>   
    
/// <param name="e">错误详细信息</param>
    
/// <returns></returns>

    public static bool WriteToFile(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToFile("", e);
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入文件系统
    
/// </summary>
    
/// <param name="userName">操作员</param>
    
/// <param name="e">错误详细信息</param>
    
/// <returns></returns>

    public static bool WriteToFile(string userName, Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToFile(CreateLog(userName,e));
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入文件系统
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <param name="userName">操作员</param>
    
/// <param name="e">错误详细信息</param>
    
/// <returns></returns>

    public static bool WriteToFile(string message, string userName, Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToFile(CreateLog(message, userName, e));
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入文件系统
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <param name="userName">操作员</param>
    
/// <param name="type">日志级别</param>
    
/// <returns></returns>

    public static bool WriteToFile(string message, string userName, EventLogEntryType type)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToFile(CreateLog(message, userName, type));
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入文件系统
    
/// </summary>
    
/// <param name="message">消息内容</param>
    
/// <param name="userName">操作员</param>
    
/// <param name="e">日志详细信息</param>
    
/// <param name="type">日志级别</param>
    
/// <returns></returns>

    public static bool WriteToFile(string message, string userName, Exception e, EventLogEntryType type)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return WriteToFile(CreateLog(message,userName,e,type));
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入文件系统
    
/// </summary>
    
/// <param name="logInfo">日志信息</param>
    
/// <returns></returns>

    public static bool WriteToFile(EventLogInfo logInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
lock (locker)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//设置日志分组,与实体对应,不同的日志类型,保存的数据不同
            string groupName = (logInfo.Type == EventLogEntryType.Warning ||
                logInfo.Type 
== EventLogEntryType.SuccessAudit ||
                logInfo.Type 
== EventLogEntryType.Information) ? "A" : "B";

            
int count = BindingHelper.BindFieldList.Count;
            StringBuilder log 
= new StringBuilder();
            log.Append(
"\r\n");
            
for (int i = 0; i < count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (BindingHelper.BindFieldList[i].Group.ConvertString().Contains(groupName))//得到需要保存的字段
ExpandedSubBlockStart.gifContractedSubBlock.gif
                {
                    log.Append(
string.Format("{0}:{1}\r\n",
                        BindingHelper.BindFieldList[i].ChineseName,
                        BindingHelper.BindFieldList[i].GetValue(logInfo).Value));
                }

            }

            IOHelper.AppendText(LogName, log.ToString());
//写入文件
            return true;
        }

    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 将日志信息写入系统日志
    
/// </summary>
    
/// <param name="logInfo">日志信息</param>
    
/// <returns></returns>

    public static bool WriteToSystemLog(EventLogInfo logInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
lock (locker)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            System.Diagnostics.EventLog eventLog 
= new System.Diagnostics.EventLog("Application", logInfo.Origin, ProjectName);
            eventLog.WriteEntry(logInfo.Detial, logInfo.Type);
            
return true;
        }

    }
   
}

ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
///日志信息类
/// </summary>

[Serializable]
[BindingTable(TableName
= " LogInfo " )]
public   class  EventLogInfo
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
///获取或设置日志编号
    
/// </summary>  

    [BindingField(NoShow = true,FieldName="Id",IsPK=true)]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public int? LogId getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
///获取或设置发生的具体时间
    
/// </summary>

    [BindingField(ChineseName = "发生时间", Group = "AB")]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public DateTime? HappenDateTime getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
///获取或设置事件来源的计算机
    
/// </summary>

    [BindingField(ChineseName = "计算机", Group = "AB")]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string Origin getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
///获取或设置事件类型
    
/// </summary>

    [BindingField(ChineseName = "事件类型")]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public EventLogEntryType Type getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
///获取或设置操作员
    
/// </summary>

    [BindingField(ChineseName = "操作员", Group = "AB")]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string Operator getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
///获取或设置项目名称
    
/// </summary>

    [BindingField(ChineseName = "项目名称", Group = "B")]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string ProjectName getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
///获取或设置文件路径
    
/// </summary>

    [BindingField(ChineseName = "文件路径", Group = "B")]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string FileName getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
///获取或设置方法名称
    
/// </summary>

    [BindingField(ChineseName = "方法名称", Group = "B")]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string MethodName getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
///获取或设置错误内容
    
/// </summary>

    [BindingField(ChineseName = "错误内容", Group = "B")]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string ErrorPoint getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
///获取或设置帮助信息
    
/// </summary>

    [BindingField(ChineseName = "帮助", Group = "B")]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string HelpLink getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
///获取或设置详细信息
    
/// </summary>

    [BindingField(ChineseName = "错误", Group = "B")]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string Detial getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 获取或设置用户自定义信息
    
/// </summary>

    [BindingField(ChineseName = "信息", Group = "A")]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string CustomerInfo getset; }


}

 

进行日志记录: 

EventLog.WriteToFile( string .Format( " 新增用户:{0} " , userName),  " admin " );

 

进行异常捕获与记录日志

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 try
            {
                
//do.
            }
            
catch (Exception e)
            {
                DevException exc 
= new DevException("命令行执行异常", e);
                EventLog.WriteToFile(e);   
            }

 

查看文件日志:

 

 


发生时间:
2008 - 12 - 23   11 : 14 : 31
计算机:
192.168 . 0.250
操作员:
项目名称:CMS
- W
文件路径:MainForm
方法名称:Void MainForm_Load(System.Object, System.EventArgs)
错误内容:CMS
- W.exe
帮助:
错误:   在 CMS_W.MainForm.MainForm_Load(Object sender, EventArgs e) 位置 E:\我的项目\CMS
- W\CMS - W\MainForm.cs:行号  32

发生时间:
2008 - 12 - 23   11 : 14 : 35
计算机:
192.168 . 0.250
操作员:
信息:处理:
192.168 . 0 .250时,由于目标机器积极拒绝,无法连接。  192.168 . 0.250 : 10002

总结

1.日志类型分为两种用户操作日志和系统异常日志,分别以不同的内容保存
2.日志记录分为三种系统日志,文件日志,数据库日志
 系统日志与文件日志已实现,数据库日志需向数据库安装Devlib中提供的Loginfo表,然后调用数据操作层,保存日志
3.日志的存储均基于实体的,这体现了Devlib中数据操作模型的特点
4.文件日志的保存名称是以中文显示的,这基于devlib的实体配置信息的特点.

 

 

 

 

 

转载于:https://www.cnblogs.com/devlib/archive/2008/12/23/1360329.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值