欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#日志记录帮助类分享

程序员文章站 2024-02-23 18:05:52
复制代码 代码如下:public class loghelper   {       pri...

复制代码 代码如下:

public class loghelper
   {
       private static void info(string category, int priority, traceeventtype severity, string message)
       {

           idictionary<string, object> dic = new dictionary<string, object>();
           dic.add("属性:", category);
           dic.add("内容:", message);

           icollection<string> coll = new list<string>();
           coll.add("general");


           logentry log = new logentry();
           log.priority = priority;
           log.severity = severity;

           log.message = category;//"日志测试";
           log.timestamp = datetime.now;
           log.extendedproperties = dic;//记录额外的信息
           log.categories = coll;//设置记录的日志类型

           logger.write(log);
       }

       public static void debug(string message)
       {
           info("debug", 1, traceeventtype.information, message);

       }

       public static void debugformat(string format, params object[] args)
       {
           info("debug", 1, traceeventtype.information, string.format(format, args));

       }

       public static void trace(string message)
       {
           info("trace", 1, traceeventtype.information, message);

       }

       public static void traceformat(string format, params object[] args)
       {
           info("trace", 1, traceeventtype.information, string.format(format, args));

       }

       public static void error(string message)
       {
           info("error", 1, traceeventtype.error, message);
       }

       public static void errorformat(string format, params object[] args)
       {
           info("error", 1, traceeventtype.error, string.format(format, args));
       }

       public static void error(object obj, exception ex)
       {
           info("error", 1, traceeventtype.error, string.format("error info:{0},{1}", obj, ex.message));
       }

       //日志记录
       public static void writelog(string errortitle, string properties, string content)
       {
           idictionary<string, object> dic = new dictionary<string, object>();
           dic.add("属性:", properties);
           dic.add("内容:", content);


           icollection<string> coll = new list<string>();
           coll.add("general");


           logentry log = new logentry();
           log.message = errortitle;//"日志测试";
           log.timestamp = datetime.now;
           log.extendedproperties = dic;//记录额外的信息
           log.categories = coll;//设置记录的日志类型

           logger.write(log);
       }
   }

用法

复制代码 代码如下:

#region 根据jobno获取对应操作人员姓名 employee 表
       /// <summary>
       /// 根据jobno获取对应操作人员姓名
       /// </summary>
       /// <param name="jobno">jobno</param>
       /// <returns></returns>
       public static string getmanagernamebyjobno(string jobno)
       {
           string strsql = "select in_user from impgtbill where job_no=@jobno";
           try
           {
               object temp = sqlhelper.instance("conn_gm")
                   .executescalar(strsql, new[] { new sqlparameter("@jobno", jobno) });
               if (temp != null)
               {
                   return temp.tostring();
               }
               return "";
           }
           catch (exception e)
           {
               loghelper.errorformat("ordertitle_dal.getmanagernamebyjobno:{0}", e.message);
               return null;
           }
       }
       #endregion