.net 系统操作日志

/// <summary>
/// 记录操作日志相关
/// </summary>
/// <typeparam name="T"></typeparam>
public class LogManager<T> where T : new()
{
static Database db = new Database(DBHelper.GetConnection());

/// <summary>
/// 记录日志
/// </summary>
/// <param name="operateLogPara"></param>
/// <returns></returns>
public void AddLog(OperateLogPara<T> operateLogPara)
{
if (operateLogPara != null)
{
T_OperateLog log = new T_OperateLog();
List<LogDetail> logDetailList = new List<LogDetail>();
string actionName = string.Empty;
string controlNameDesc = string.Empty;
int logPKeyVal = 0;
if (operateLogPara.controller != null)
{
Type controlType = operateLogPara.controller.GetType();
//取得Controller的Description
controlNameDesc = GetDescriptionHelper.GetDescription(operateLogPara.controller) ?? controlType.Name;

#region 取得Action的Description

operateLogPara.controller.RouteData.Values.TryGetValue("action", out object val);
var members = controlType.GetMethods();
foreach (var member in members)
{
if (member.Name == val.ToString())
{
actionName = GetDescriptionHelper.GetMethodDesc(member) ?? val.ToString();
}
}
#endregion
}
if (!string.IsNullOrEmpty(operateLogPara.operationUser))
{
try
{
var attrs = System.Attribute.GetCustomAttributes(typeof(T));

#region 构建logModel
log.OperateUser = operateLogPara.operationUser;
log.OperateType = operateLogPara.operateType;
log.TblName = typeof(T).Name;
log.TblNameDesc = attrs.Length > 0 ? ((DescriptionAttribute)attrs[0]).Description : string.Empty;
log.ModulName = string.IsNullOrEmpty(operateLogPara.modulName) ? controlNameDesc : operateLogPara.modulName;
log.BusinesName = string.IsNullOrEmpty(operateLogPara.businesName) ? actionName : operateLogPara.businesName;
log.CreateDate = DateTime.Now;
log.Remark = operateLogPara.remark;
#endregion

using (var db = new Database(AppConfig.DefaultConnectionString, "SqlServer"))
{
var logPKey = db.Insert("T_OperateLog", "LogId", log);
if (logPKey != null)
{
logPKeyVal = Convert.ToInt32(logPKey);
}
}

if (operateLogPara.newMdData != null)
{
//取得m的Type实例
Type t = operateLogPara.newMdData.GetType();

switch (operateLogPara.operateType)
{
case OperateType.Add:
AddDelOperation(operateLogPara.operateType, t, logPKeyVal, operateLogPara.newMdData);
break;
case OperateType.Edit:
EditOperation(t, logPKeyVal, operateLogPara.oldMdData, operateLogPara.newMdData);
break;
case OperateType.Del:
AddDelOperation(operateLogPara.operateType, t, logPKeyVal, operateLogPara.newMdData);
break;
//case OperateType.Query:
//EditOperation(t, logPKeyVal, oldMd, newMd);
// break;
}
}
}
catch (Exception ex)
{
LogService.WriteErrorLog(ex.ToString());
throw;
}
}
}
}

/// <summary>
/// 编辑操作
/// </summary>
/// <param name="t"></param>
/// <param name="logKey"></param>
/// <param name="oldMd"></param>
/// <param name="newMd"></param>
private void EditOperation(Type t, int logKey, T oldMd, T newMd)
{
//取得类的属性名并获取属性值和Description
foreach (PropertyInfo property in t.GetProperties()) //循环遍历
{
LogDetail logDetail = new LogDetail();
string propertyDsc = string.Empty;
var oldVal = string.Empty;
if (oldMd != null)
{
var oldObj = oldMd.GetType().GetProperty(property.Name).GetValue(oldMd, null);
oldVal = oldObj == null ? oldVal : oldObj.ToString();
}

var newObj = newMd.GetType().GetProperty(property.Name).GetValue(newMd, null);
var newVal = newObj == null ? "" : newObj.ToString();
if (!string.IsNullOrEmpty(oldVal) && oldVal != newVal)
{
logDetail.LogId = logKey;
logDetail.ColumnName = property.Name;
logDetail.ColumnDesc = GetDescriptionHelper.GetCustomerDesc(property);
logDetail.OldVal = oldVal;
logDetail.NewVal = newVal;
logDetail.CreateDate = DateTime.Now;

using (var db = new Database(AppConfig.DefaultConnectionString, "SqlServer"))
{
db.Insert("T_LogDetail", "ID", logDetail);
}
}
}
}

/// <summary>
/// 新增/删除操作
/// </summary>
/// <param name="operateType"></param>
/// <param name="t"></param>
/// <param name="logKey"></param>
/// <param name="newMd"></param>
private void AddDelOperation(OperateType operateType, Type t, int logKey, T newMd)
{
//取得类的属性名并获取属性值和Description
foreach (PropertyInfo property in t.GetProperties()) //循环遍历
{
LogDetail logDetail = new LogDetail();
string propertyDsc = string.Empty;
var newObj = newMd.GetType().GetProperty(property.Name).GetValue(newMd, null);
var newVal = newObj == null ? "" : newObj.ToString();
logDetail.LogId = logKey;
logDetail.ColumnName = property.Name;
logDetail.ColumnDesc = GetDescriptionHelper.GetCustomerDesc(property);
logDetail.OldVal = operateType == OperateType.Del ? newVal : "";
logDetail.NewVal = operateType == OperateType.Add ? newVal : "";
logDetail.CreateDate = DateTime.Now;

using (var db = new Database(AppConfig.DefaultConnectionString, "SqlServer"))
{
db.Insert("T_LogDetail", "ID", logDetail);
}
}
}
}

调用方法:

LogManager<T_UserInfo> log = new LogManager<T_UserInfo>();
log.AddLog(new OperateLogPara<T_UserInfo>
{
operationUser = CurrentUser.loginUser.UserName,
operateType = OperateType.Del,
controller = this,
modulName = "应用管理",
businesName = "delAppUser",
remark = "删除应用"
});

枚举:

public enum OperateType
{
/// <summary>
/// 增加
/// </summary>
[Description("增加")]
Add = 1,

/// <summary>
/// 修改
/// </summary>
[Description("修改")]
Edit = 2,

/// <summary>
/// 删除
/// </summary>
[Description("删除")]
Del = 3,

/// <summary>
/// 查询
/// </summary>
[Description("查询")]
Query = 4
}

 建表:

public class T_OperateLog
{
public T_OperateLog()
{
CreateDate = DateTime.Now;
}

public int LogId { get; set; }

/// <summary>
/// 操作者
/// </summary>
[MaxLength(50)]
public string OperateUser { get; set; }

/// <summary>
/// 操作类型
/// </summary>
public OperateType OperateType { get; set; }

/// <summary>
/// 模块名称
/// </summary>
public string ModulName { get; set; }

/// <summary>
/// 业务名称
/// </summary>
public string BusinesName { get; set; }

/// <summary>
/// 表名称
/// </summary>
public string TblName { get; set; }

/// <summary>
/// 表描述
/// </summary>
public string TblNameDesc { get; set; }

/// <summary>
/// 操作时间
/// </summary>
public DateTime CreateDate { get; set; }

/// <summary>
/// 备注
/// </summary>
public string Remark { get; set; }
}

public class LogDetail
{
public LogDetail()
{
CreateDate = DateTime.Now;
}

public int ID { get; set; }

public int LogId { get; set; }

public string ColumnName { get; set; }

public string ColumnDesc { get; set; }

public string OldVal { get; set; }

public string NewVal { get; set; }

public DateTime CreateDate { get; set; }
}

效果:

 

原文地址:https://www.cnblogs.com/nayilvyangguang/p/11983084.html