c# log4net 配置使用

2021-03-28 05:24

阅读:340

标签:composite   none   .com   操作   document   onclick   添加   gif   cat   

新增配置文件log4net.config,内容如下

技术分享图片技术分享图片
xml version="1.0" encoding="utf-8" ?>
configuration>
  configSections>
    section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  configSections>
  log4net>
    
    
    
    appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
      
      file value="log\\Info\\"/>
      
      appendToFile value="true"/>
      
      rollingStyle value="Composite"/>
      
      datePattern value="yyyy-MM\\yyyy-MM-dd‘.txt‘"/>
      
      staticLogFileName value="false"/>
      
      param name="MaxSizeRollBackups" value="100"/>
      
      maximumFileSize value="10240KB" />
      
      layout type="log4net.Layout.PatternLayout">
        
        
        

        
        
        conversionPattern value="%n==========
                                  %n【记录时间】%date
                                  %n【记录的类】%logger 属性[%property{NDC}]
                                  %n【内容描述】%message"/>
      layout>
    appender>
    appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
      
      file value="log\\Error\\"/>
      
      appendToFile value="true"/>
      
      rollingStyle value="Composite"/>
      
      datePattern value="yyyy-MM\\yyyy-MM-dd‘.txt‘"/>
      
      staticLogFileName value="false"/>
      
      param name="MaxSizeRollBackups" value="100"/>
      
      maximumFileSize value="10240KB" />
      
      layout type="log4net.Layout.PatternLayout">
        
        
        

        
        
        conversionPattern value="%n==========
                                  %n【日志级别】%-5level
                                  %n【记录时间】%date
                                  %n【线程编号】[%thread]
                                  %n【执行时间】[%r]毫秒
                                  %n【出错文件】%F
                                  %n【出错行号】%L
                                  %n【出错的类】%logger 属性[%property{NDC}]
                                  %n【错误描述】%message
                                  %n【错误详情】%newline"/>
      layout>
    appender>
    logger name="info_logo">
      level value="ALL" />
      appender-ref ref="InfoAppender" />
    logger>
    logger name="error_logo">
      level value="ALL" />
      appender-ref ref="ErrorAppender" />
    logger>
  log4net>
configuration>
View Code

写个帮助类

技术分享图片技术分享图片
 public class Log4NetHelper
    {
        private static string m_logFile;
        private static Dictionarystring, log4net.ILog> m_lstLog = new Dictionarystring, log4net.ILog>();
        public static void InitLog4Net(string strLog4NetConfigFile)
        {
            log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(strLog4NetConfigFile));
            m_logFile = strLog4NetConfigFile;
            m_lstLog["info_logo"] = log4net.LogManager.GetLogger("info_logo");
            m_lstLog["error_logo"] = log4net.LogManager.GetLogger("error_logo");
        }

        /// summary>
        /// 功能描述:写入常规日志
        /// summary>
        /// param name="strInfoLog">strInfoLogparam>
        public static void WriteInfoLog(string strInfoLog)
        {
            if (m_lstLog["info_logo"].IsInfoEnabled)
            {
                m_lstLog["info_logo"].Info(strInfoLog);
            }
        }

        /// summary>
        /// 功能描述:写入错误日志
        /// summary>
        /// param name="strErrLog">strErrLogparam>
        /// param name="ex">exparam>
        public static void WriteErrorLog(string strErrLog, Exception ex = null)
        {
            if (m_lstLog["error_logo"].IsErrorEnabled)
            {
                m_lstLog["error_logo"].Error(strErrLog, ex);
            }
        }

        /// summary>
        /// 功能描述:写入日志
        /// summary>
        /// param name="strType">日志类型(对应log4net配置文件中logger.nama)param>
        /// param name="strLog">strLogparam>
        public static void WriteByLogType(string strType, string strLog)
        {
            if (!m_lstLog.ContainsKey(strType))
            {
                //判断是否存在节点
                if (!HasLogNode(strType))
                {
                    WriteErrorLog("log4net配置文件不存在【" + strType + "】配置");
                    return;
                }
                m_lstLog[strType] = log4net.LogManager.GetLogger(strType);
            }
            m_lstLog[strType].Error(strLog);
        }

        /// summary>
        /// 功能描述:是否存在指定的配置
        /// summary>
        /// param name="strNodeName">strNodeNameparam>
        /// returns>返回值returns>
        private static bool HasLogNode(string strNodeName)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(m_logFile);
            var lstNodes = doc.SelectNodes("//configuration/log4net/logger");
            foreach (XmlNode item in lstNodes)
            {
                if (item.Attributes["name"].Value.ToLower() == strNodeName)
                    return true;
            }
            return false;
        }
    }
View Code

程序启动时初始化

Log4NetHelper.InitLog4Net(Application.StartupPath + "\\log4net.config");    

需要的地方调用函数写日志就可以了

技术分享图片

 

c# log4net 配置使用

标签:composite   none   .com   操作   document   onclick   添加   gif   cat   

原文地址:https://www.cnblogs.com/bfyx/p/9342512.html


评论


亲,登录后才可以留言!