zl程序教程

您现在的位置是:首页 >  后端

当前栏目

c#日志记录帮助类分享

c#日志 分享 记录 帮助
2023-06-13 09:15:19 时间

复制代码代码如下:


publicclassLogHelper
  {
      privatestaticvoidInfo(stringcategory,intpriority,TraceEventTypeseverity,stringmessage)
      {

          IDictionary<string,object>dic=newDictionary<string,object>();
          dic.Add("属性:",category);
          dic.Add("内容:",message);

          ICollection<string>coll=newList<string>();
          coll.Add("General");


          LogEntrylog=newLogEntry();
          log.Priority=priority;
          log.Severity=severity;

          log.Message=category;//"日志测试";
          log.TimeStamp=DateTime.Now;
          log.ExtendedProperties=dic;//记录额外的信息
          log.Categories=coll;//设置记录的日志类型

          Logger.Write(log);
      }

      publicstaticvoidDebug(stringmessage)
      {
          Info("Debug",1,TraceEventType.Information,message);

      }

      publicstaticvoidDebugFormat(stringformat,paramsobject[]args)
      {
          Info("Debug",1,TraceEventType.Information,String.Format(format,args));

      }

      publicstaticvoidTrace(stringmessage)
      {
          Info("Trace",1,TraceEventType.Information,message);

      }

      publicstaticvoidTraceFormat(stringformat,paramsobject[]args)
      {
          Info("Trace",1,TraceEventType.Information,String.Format(format,args));

      }

      publicstaticvoidError(stringmessage)
      {
          Info("Error",1,TraceEventType.Error,message);
      }

      publicstaticvoidErrorFormat(stringformat,paramsobject[]args)
      {
          Info("Error",1,TraceEventType.Error,String.Format(format,args));
      }

      publicstaticvoidError(objectobj,Exceptionex)
      {
          Info("Error",1,TraceEventType.Error,String.Format("ErrorInfo:{0},{1}",obj,ex.Message));
      }

      //日志记录
      publicstaticvoidWriteLog(stringerrorTitle,stringproperties,stringcontent)
      {
          IDictionary<string,object>dic=newDictionary<string,object>();
          dic.Add("属性:",properties);
          dic.Add("内容:",content);


          ICollection<string>coll=newList<string>();
          coll.Add("General");


          LogEntrylog=newLogEntry();
          log.Message=errorTitle;//"日志测试";
          log.TimeStamp=DateTime.Now;
          log.ExtendedProperties=dic;//记录额外的信息
          log.Categories=coll;//设置记录的日志类型

          Logger.Write(log);
      }
  }

用法

复制代码代码如下:


#region根据JobNO获取对应操作人员姓名EMPLOYEE表
      ///<summary>
      ///根据JobNO获取对应操作人员姓名
      ///</summary>
      ///<paramname="jobNo">JobNO</param>
      ///<returns></returns>
      publicstaticstringGetManagerNameByjobNo(stringjobNo)
      {
          stringstrSql="selectIN_USERfromIMPGTBILLwhereJOB_NO=@jobNo";
          try
          {
              objecttemp=SqlHelper.Instance("Conn_GM")
                  .ExecuteScalar(strSql,new[]{newSqlParameter("@jobNo",jobNo)});
              if(temp!=null)
              {
                  returntemp.ToString();
              }
              return"";
          }
          catch(Exceptione)
          {
              LogHelper.ErrorFormat("OrderTitle_DAL.GetManagerNameByjobNo:{0}",e.Message);
              returnnull;
          }
      }
      #endregion