.net system operation logs

/// <summary>
    /// operation log associated
     ///  </ Summary> 
    ///  <typeParam name = "T"> </ typeParam> 
    public  class the LogManager <T> WHERE T: new new () 
    { 
        static Database DB = new new Database (the DBHelper. the GetConnection ()); 

        ///  <Summary> 
        /// logging
         ///  </ Summary> 
        ///  <param name = "operationUser"> the user currently logged </ param> 
        ///  <param name = "operateType "> operation type </ param> 
        ///  <param name =" newMd "> </ param>
        ///  <param name = "oldMd"> when new operation, which may be empty</param>
        public void AddLog(string operationUser, OperateType operateType, T newMd, T oldMd)
        {
            T_OperateLog log = new T_OperateLog();
            List<LogDetail> logDetailList = new List<LogDetail>();

            //取得m的Type实例
            Type t = newMd.GetType();

            log.OperateUser = operationUser;
            log.OperateType = operateType;
            log.TblName = typeof(T).Name;
            log.CreateDate = DateTime.Now;
            log.TblNameDesc = ((DescriptionAttribute)System.Attribute.GetCustomAttributes(typeof(T))[0]).Description;

            using (var db = new Database(AppConfig.DefaultConnectionString, "SqlServer"))
            {
                int logPKeyVal = 0;
                var logPKey = db.Insert("T_OperateLog", "LogId", log);
                if (logPKey != null)
                {
                    logPKeyVal = Convert.ToInt32(logPKey);
                }
                switch (operateType)
                {
                    case OperateType.Add:
                        AddDelOperation(operateType, t, logPKeyVal, newMd);
                        break;
                    case OperateType.Edit:
                        EditOperation(t, logPKeyVal, oldMd, newMd);
                        break;
                    case OperateType.Del:
                        AddDelOperation(operateType, t, logPKeyVal, newMd);
                        break;
                    case OperateType.Query:
                        //EditOperation (T, logPKeyVal, oldMd, newMd); 
                        BREAK ; 
                } 
            } 


        } 

        ///  <Summary> 
        /// editing operation
         ///  </ Summary> 
        ///  <param name = "T"> </ param> 
        / //  <param name = "logKey"> </ param> 
        ///  <param name = "oldMd"> </ param> 
        ///  <param name = "newMd"> </ param> 
        Private  void EditOperation (the Type T , int logKey, oldMd T, T newMd) 
        { 
            // obtain class attribute names and attribute values and obtain the Description 
            the foreach (PropertyInfo property in T.GetProperties ()) // Loop through
            {
                LogDetail logDetail = new LogDetail();
                string propertyDsc = string.Empty;
                var oldObj = oldMd.GetType().GetProperty(property.Name).GetValue(oldMd, null);
                var oldVal = oldObj == null ? "" : oldObj.ToString();
                var newObj = newMd.GetType().GetProperty(property.Name).GetValue(newMd, null);
                var newVal = newObj == null ? "" : newObj.ToString();
                if (oldVal != newVal)
                {
                    logDetail.LogId = logKey;
                    logDetail.ColumnName = property.Name;
                    logDetail.ColumnDesc = 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)
        {
            // property name acquired class and getting a property value and the Description 
            the foreach (PropertyInfo Property in t.GetProperties ()) // Loop through 
            { 
                LogDetail logdetail = new 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 = 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);
                }
            }
        }

        /// <summary>
        /// 获取Model的DescriptionAttribute
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public string GetCustomerDesc(PropertyInfo property)
        {
            string propertyDsc = string.Empty;
            var arrProDesc = property.GetCustomAttributes(typeof(DescriptionAttribute), true);
            if (arrProDesc != null && arrProDesc.Length > 0)
            {
                propertyDsc = ((DescriptionAttribute)arrProDesc[0]).Description;
            }
            return propertyDsc;
        }
    }

 

Call the method:

LogManager<T_UserInfo> log = new LogManager<T_UserInfo>();
log.AddLog(currentUserName, OperateType.Del, appUser, null);

enumerate:

enum OperateType public
{
/// <Summary>
/// increase
/// </ Summary>
[the Description ( "increase")]
the Add =. 1,

/// <Summary>
/// modify
/// </ Summary>
[the Description ( "modify")]
the Edit = 2,

/// <Summary>
/// Remove
/// </ Summary>
[the Description ( "Delete")]
Del =. 3,

/// <Summary>
/// Query
/// </ Summary>
[the Description ( "query")]
Query =. 4
}

 Built table:

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>
/// operation type
/// </ Summary>
public OperateType OperateType {GET; SET;}

public string TblName { get; set; }

public string TblNameDesc { get; set; }

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

/// <summary>
/// 备注
/// </summary>
[MaxLength(500)]
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; }
}

effect:

 

 

 

Guess you like

Origin www.cnblogs.com/nayilvyangguang/p/11983084.html