Devlib是自己封装的一个包含有数据操作模式,共用操作方法层,控件层三个组件的集合.
下面介绍Devlib中的日志处理类.封装源码如下:
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
/**/ /// <summary>
/// DevLib提供的异常日志处理日志
/// </summary>
[Serializable]
public class DevException : Exception
{
/**//// <summary>
/// 指定用户消息
/// </summary>
/// <param name="message">用户 消息</param>
public DevException(string message):base(message)
{
}
/**//// <summary>
/// 指定用户消息及异常信息
/// </summary>
/// <param name="message">用户消息</param>
/// <param name="innerException">系统异常</param>
public DevException(string message, Exception innerException):base(message,innerException)
{
}
/**//// <summary>
/// 默认异常
/// </summary>
public DevException()
{
}
}
/**/ /// <summary>
/// 日志操作类,日志写入过程是线程安全的
/// </summary>
public class EventLog
{
static EventLog()
{
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;
}
/**//// <summary>
/// 日志信息解析类
/// </summary>
private static BindingHelper<EventLogInfo> BindingHelper;
private static string LogName;
private static string ProjectName;
private static object locker = new object();
/**//// <summary>
/// 创建提示消息日志
/// </summary>
/// <param name="message">消息内容</param>
/// <returns></returns>
public static EventLogInfo CreateLog(string message)
{
return CreateLog(message, "");
}
/**//// <summary>
/// 创建操作记录日志
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="userName">操作员</param>
/// <returns></returns>
public static EventLogInfo CreateLog(string message, string userName)
{
return CreateLog(message, userName, EventLogEntryType.Information);
}
/**//// <summary>
/// 创建错误记录日志
/// </summary>
/// <param name="userName">操作员</param>
/// <param name="e">错误详细信息</param>
/// <returns></returns>
public static EventLogInfo CreateLog(string userName, Exception e)
{
return CreateLog("", userName, e);
}
/**//// <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)
{
return CreateLog(message, userName, e, EventLogEntryType.Error);
}
/**//// <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)
{
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;
}
/**//// <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)
{
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;
}
/**//// <summary>
/// 将日志信息写入系统日志
/// </summary>
/// <param name="message">消息内容</param>
/// <returns></returns>
public static bool WriteToSystemLog(string message)
{
return WriteToSystemLog(CreateLog(message));
}
/**//// <summary>
/// 将日志信息写入系统日志
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="userName">操作员</param>
/// <returns></returns>
public static bool WriteToSystemLog(string message, string userName)
{
return WriteToSystemLog(CreateLog(message, userName));
}
/**//// <summary>
/// 将日志信息写入系统日志
/// </summary>
/// <param name="e">错误详细信息</param>
/// <returns></returns>
public static bool WriteToSystemLog(Exception e)
{
return WriteToSystemLog("", e);
}
/**//// <summary>
/// 将日志信息写入系统日志
/// </summary>
/// <param name="userName">操作员</param>
/// <param name="e">错误详细信息</param>
/// <returns></returns>
public static bool WriteToSystemLog(string userName, Exception e)
{
return WriteToSystemLog(CreateLog(userName,e));
}
/**//// <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)
{
return WriteToSystemLog(CreateLog(message, userName, e));
}
/**//// <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)
{
return WriteToSystemLog(CreateLog(message, userName, type));
}
/**//// <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)
{
return WriteToSystemLog(CreateLog(message,userName,e,type));
}
/**//// <summary>
/// 将日志信息写入文件系统
/// </summary>
/// <param name="message">消息内容</param>
public static bool WriteToFile(string message)
{
return WriteToFile(CreateLog(message));
}
/**//// <summary>
/// 将日志信息写入文件系统
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="userName">操作员</param>
/// <returns></returns>
public static bool WriteToFile(string message, string userName)
{
return WriteToFile(CreateLog(message, userName));
}
/**//// <summary>
/// 将日志信息写入文件系统
/// </summary>
/// <param name="e">错误详细信息</param>
/// <returns></returns>
public static bool WriteToFile(Exception e)
{
return WriteToFile("", e);
}
/**//// <summary>
/// 将日志信息写入文件系统
/// </summary>
/// <param name="userName">操作员</param>
/// <param name="e">错误详细信息</param>
/// <returns></returns>
public static bool WriteToFile(string userName, Exception e)
{
return WriteToFile(CreateLog(userName,e));
}
/**//// <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)
{
return WriteToFile(CreateLog(message, userName, e));
}
/**//// <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)
{
return WriteToFile(CreateLog(message, userName, type));
}
/**//// <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)
{
return WriteToFile(CreateLog(message,userName,e,type));
}
/**//// <summary>
/// 将日志信息写入文件系统
/// </summary>
/// <param name="logInfo">日志信息</param>
/// <returns></returns>
public static bool WriteToFile(EventLogInfo logInfo)
{
lock (locker)
{
//设置日志分组,与实体对应,不同的日志类型,保存的数据不同
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++)
{
if (BindingHelper.BindFieldList[i].Group.ConvertString().Contains(groupName))//得到需要保存的字段
{
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;
}
}
/**//// <summary>
/// 将日志信息写入系统日志
/// </summary>
/// <param name="logInfo">日志信息</param>
/// <returns></returns>
public static bool WriteToSystemLog(EventLogInfo logInfo)
{
lock (locker)
{
System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog("Application", logInfo.Origin, ProjectName);
eventLog.WriteEntry(logInfo.Detial, logInfo.Type);
return true;
}
}
}
/**/ /// <summary>
///日志信息类
/// </summary>
[Serializable]
[BindingTable(TableName = " LogInfo " )]
public class EventLogInfo
{
/**//// <summary>
///获取或设置日志编号
/// </summary>
[BindingField(NoShow = true,FieldName="Id",IsPK=true)]
public int? LogId { get; set; }
/**//// <summary>
///获取或设置发生的具体时间
/// </summary>
[BindingField(ChineseName = "发生时间", Group = "AB")]
public DateTime? HappenDateTime { get; set; }
/**//// <summary>
///获取或设置事件来源的计算机
/// </summary>
[BindingField(ChineseName = "计算机", Group = "AB")]
public string Origin { get; set; }
/**//// <summary>
///获取或设置事件类型
/// </summary>
[BindingField(ChineseName = "事件类型")]
public EventLogEntryType Type { get; set; }
/**//// <summary>
///获取或设置操作员
/// </summary>
[BindingField(ChineseName = "操作员", Group = "AB")]
public string Operator { get; set; }
/**//// <summary>
///获取或设置项目名称
/// </summary>
[BindingField(ChineseName = "项目名称", Group = "B")]
public string ProjectName { get; set; }
/**//// <summary>
///获取或设置文件路径
/// </summary>
[BindingField(ChineseName = "文件路径", Group = "B")]
public string FileName { get; set; }
/**//// <summary>
///获取或设置方法名称
/// </summary>
[BindingField(ChineseName = "方法名称", Group = "B")]
public string MethodName { get; set; }
/**//// <summary>
///获取或设置错误内容
/// </summary>
[BindingField(ChineseName = "错误内容", Group = "B")]
public string ErrorPoint { get; set; }
/**//// <summary>
///获取或设置帮助信息
/// </summary>
[BindingField(ChineseName = "帮助", Group = "B")]
public string HelpLink { get; set; }
/**//// <summary>
///获取或设置详细信息
/// </summary>
[BindingField(ChineseName = "错误", Group = "B")]
public string Detial { get; set; }
/**//// <summary>
/// 获取或设置用户自定义信息
/// </summary>
[BindingField(ChineseName = "信息", Group = "A")]
public string CustomerInfo { get; set; }
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
/**/ /// <summary>
/// DevLib提供的异常日志处理日志
/// </summary>
[Serializable]
public class DevException : Exception
{
/**//// <summary>
/// 指定用户消息
/// </summary>
/// <param name="message">用户 消息</param>
public DevException(string message):base(message)
{
}
/**//// <summary>
/// 指定用户消息及异常信息
/// </summary>
/// <param name="message">用户消息</param>
/// <param name="innerException">系统异常</param>
public DevException(string message, Exception innerException):base(message,innerException)
{
}
/**//// <summary>
/// 默认异常
/// </summary>
public DevException()
{
}
}
/**/ /// <summary>
/// 日志操作类,日志写入过程是线程安全的
/// </summary>
public class EventLog
{
static EventLog()
{
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;
}
/**//// <summary>
/// 日志信息解析类
/// </summary>
private static BindingHelper<EventLogInfo> BindingHelper;
private static string LogName;
private static string ProjectName;
private static object locker = new object();
/**//// <summary>
/// 创建提示消息日志
/// </summary>
/// <param name="message">消息内容</param>
/// <returns></returns>
public static EventLogInfo CreateLog(string message)
{
return CreateLog(message, "");
}
/**//// <summary>
/// 创建操作记录日志
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="userName">操作员</param>
/// <returns></returns>
public static EventLogInfo CreateLog(string message, string userName)
{
return CreateLog(message, userName, EventLogEntryType.Information);
}
/**//// <summary>
/// 创建错误记录日志
/// </summary>
/// <param name="userName">操作员</param>
/// <param name="e">错误详细信息</param>
/// <returns></returns>
public static EventLogInfo CreateLog(string userName, Exception e)
{
return CreateLog("", userName, e);
}
/**//// <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)
{
return CreateLog(message, userName, e, EventLogEntryType.Error);
}
/**//// <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)
{
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;
}
/**//// <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)
{
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;
}
/**//// <summary>
/// 将日志信息写入系统日志
/// </summary>
/// <param name="message">消息内容</param>
/// <returns></returns>
public static bool WriteToSystemLog(string message)
{
return WriteToSystemLog(CreateLog(message));
}
/**//// <summary>
/// 将日志信息写入系统日志
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="userName">操作员</param>
/// <returns></returns>
public static bool WriteToSystemLog(string message, string userName)
{
return WriteToSystemLog(CreateLog(message, userName));
}
/**//// <summary>
/// 将日志信息写入系统日志
/// </summary>
/// <param name="e">错误详细信息</param>
/// <returns></returns>
public static bool WriteToSystemLog(Exception e)
{
return WriteToSystemLog("", e);
}
/**//// <summary>
/// 将日志信息写入系统日志
/// </summary>
/// <param name="userName">操作员</param>
/// <param name="e">错误详细信息</param>
/// <returns></returns>
public static bool WriteToSystemLog(string userName, Exception e)
{
return WriteToSystemLog(CreateLog(userName,e));
}
/**//// <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)
{
return WriteToSystemLog(CreateLog(message, userName, e));
}
/**//// <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)
{
return WriteToSystemLog(CreateLog(message, userName, type));
}
/**//// <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)
{
return WriteToSystemLog(CreateLog(message,userName,e,type));
}
/**//// <summary>
/// 将日志信息写入文件系统
/// </summary>
/// <param name="message">消息内容</param>
public static bool WriteToFile(string message)
{
return WriteToFile(CreateLog(message));
}
/**//// <summary>
/// 将日志信息写入文件系统
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="userName">操作员</param>
/// <returns></returns>
public static bool WriteToFile(string message, string userName)
{
return WriteToFile(CreateLog(message, userName));
}
/**//// <summary>
/// 将日志信息写入文件系统
/// </summary>
/// <param name="e">错误详细信息</param>
/// <returns></returns>
public static bool WriteToFile(Exception e)
{
return WriteToFile("", e);
}
/**//// <summary>
/// 将日志信息写入文件系统
/// </summary>
/// <param name="userName">操作员</param>
/// <param name="e">错误详细信息</param>
/// <returns></returns>
public static bool WriteToFile(string userName, Exception e)
{
return WriteToFile(CreateLog(userName,e));
}
/**//// <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)
{
return WriteToFile(CreateLog(message, userName, e));
}
/**//// <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)
{
return WriteToFile(CreateLog(message, userName, type));
}
/**//// <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)
{
return WriteToFile(CreateLog(message,userName,e,type));
}
/**//// <summary>
/// 将日志信息写入文件系统
/// </summary>
/// <param name="logInfo">日志信息</param>
/// <returns></returns>
public static bool WriteToFile(EventLogInfo logInfo)
{
lock (locker)
{
//设置日志分组,与实体对应,不同的日志类型,保存的数据不同
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++)
{
if (BindingHelper.BindFieldList[i].Group.ConvertString().Contains(groupName))//得到需要保存的字段
{
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;
}
}
/**//// <summary>
/// 将日志信息写入系统日志
/// </summary>
/// <param name="logInfo">日志信息</param>
/// <returns></returns>
public static bool WriteToSystemLog(EventLogInfo logInfo)
{
lock (locker)
{
System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog("Application", logInfo.Origin, ProjectName);
eventLog.WriteEntry(logInfo.Detial, logInfo.Type);
return true;
}
}
}
/**/ /// <summary>
///日志信息类
/// </summary>
[Serializable]
[BindingTable(TableName = " LogInfo " )]
public class EventLogInfo
{
/**//// <summary>
///获取或设置日志编号
/// </summary>
[BindingField(NoShow = true,FieldName="Id",IsPK=true)]
public int? LogId { get; set; }
/**//// <summary>
///获取或设置发生的具体时间
/// </summary>
[BindingField(ChineseName = "发生时间", Group = "AB")]
public DateTime? HappenDateTime { get; set; }
/**//// <summary>
///获取或设置事件来源的计算机
/// </summary>
[BindingField(ChineseName = "计算机", Group = "AB")]
public string Origin { get; set; }
/**//// <summary>
///获取或设置事件类型
/// </summary>
[BindingField(ChineseName = "事件类型")]
public EventLogEntryType Type { get; set; }
/**//// <summary>
///获取或设置操作员
/// </summary>
[BindingField(ChineseName = "操作员", Group = "AB")]
public string Operator { get; set; }
/**//// <summary>
///获取或设置项目名称
/// </summary>
[BindingField(ChineseName = "项目名称", Group = "B")]
public string ProjectName { get; set; }
/**//// <summary>
///获取或设置文件路径
/// </summary>
[BindingField(ChineseName = "文件路径", Group = "B")]
public string FileName { get; set; }
/**//// <summary>
///获取或设置方法名称
/// </summary>
[BindingField(ChineseName = "方法名称", Group = "B")]
public string MethodName { get; set; }
/**//// <summary>
///获取或设置错误内容
/// </summary>
[BindingField(ChineseName = "错误内容", Group = "B")]
public string ErrorPoint { get; set; }
/**//// <summary>
///获取或设置帮助信息
/// </summary>
[BindingField(ChineseName = "帮助", Group = "B")]
public string HelpLink { get; set; }
/**//// <summary>
///获取或设置详细信息
/// </summary>
[BindingField(ChineseName = "错误", Group = "B")]
public string Detial { get; set; }
/**//// <summary>
/// 获取或设置用户自定义信息
/// </summary>
[BindingField(ChineseName = "信息", Group = "A")]
public string CustomerInfo { get; set; }
}
进行日志记录:
EventLog.WriteToFile(
string
.Format(
"
新增用户:{0}
"
, userName),
"
admin
"
);
进行异常捕获与记录日志
Code
try
{
//do.
}
catch (Exception e)
{
DevException exc = new DevException("命令行执行异常", e);
EventLog.WriteToFile(e);
}
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的实体配置信息的特点.