net core中動態給log4net添加日志類型


        private static object m_Lock = new object();

        /// <summary>
        /// 根據類型獲取對應的日志操作類
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public static ILog GetLog(string typeName)
        {
            lock (m_Lock)
            {
                var testRepository = LogManager.GetRepository("MeIsTestRepository");

                var log = LogManager.Exists("MeIsTestRepository", typeName);
                if (log != null)
                {
                    return log;
                }
                CreateXMLLog(testRepository, typeName);
                return LogManager.GetLogger("MeIsTestRepository", typeName);
            }
        }

        /// <summary>
        /// 動態添加日志類型
        /// </summary>
        /// <param name="log"></param>
        /// <param name="typeName"></param>
        private static void CreateXMLLog(log4net.Repository.ILoggerRepository logRepository, string typeName)
        {
            var xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));
            var configuration = xmlDoc.CreateElement("configuration");
            var root = xmlDoc.CreateElement("log4net");


            var appender = xmlDoc.CreateElement("appender");
            appender.SetAttribute("name", typeName + "Appender");
            appender.SetAttribute("type", "log4net.Appender.RollingFileAppender");
            var param = xmlDoc.CreateElement("param");
            param.SetAttribute("name", "Encoding");
            param.SetAttribute("value", "utf-8");
            appender.AppendChild(param);

            var file = xmlDoc.CreateElement("file");
            file.SetAttribute("value", "logfile/");
            appender.AppendChild(file);

            var appendToFile = xmlDoc.CreateElement("appendToFile");
            appendToFile.SetAttribute("value", "true");
            appender.AppendChild(appendToFile);

            var rollingStyle = xmlDoc.CreateElement("rollingStyle");
            rollingStyle.SetAttribute("value", "Composite");
            appender.AppendChild(rollingStyle);

            var staticLogFileName = xmlDoc.CreateElement("staticLogFileName");
            staticLogFileName.SetAttribute("value", "false");
            appender.AppendChild(staticLogFileName);

            var DatePattern = xmlDoc.CreateElement("DatePattern");
            DatePattern.SetAttribute("value", $"'{typeName}'-yyyyMMdd'.log'");
            appender.AppendChild(DatePattern);


            var maxSizeRollBackups = xmlDoc.CreateElement("maxSizeRollBackups");
            maxSizeRollBackups.SetAttribute("value", "10");
            appender.AppendChild(maxSizeRollBackups);

            var maximumFileSize = xmlDoc.CreateElement("maximumFileSize");
            maximumFileSize.SetAttribute("value", "1MB");
            appender.AppendChild(maximumFileSize);
            
            var layout = xmlDoc.CreateElement("layout");
            layout.SetAttribute("type", "log4net.Layout.PatternLayout");

            var conversionPattern = xmlDoc.CreateElement("conversionPattern");
            conversionPattern.SetAttribute("value", "$$%date%message%newline");
            layout.AppendChild(conversionPattern);
            appender.AppendChild(layout);

            var logger = xmlDoc.CreateElement("logger");
            logger.SetAttribute("name", typeName);
            var level = xmlDoc.CreateElement("level");
            level.SetAttribute("value", "DEBUG");
            var appender_ref = xmlDoc.CreateElement("appender-ref");
            appender_ref.SetAttribute("ref", typeName + "Appender");
            logger.AppendChild(level);
            logger.AppendChild(appender_ref);


            root.AppendChild(appender);
            root.AppendChild(logger);

            configuration.AppendChild(root);
            xmlDoc.AppendChild(configuration);

            var array = Encoding.ASCII.GetBytes(xmlDoc.OuterXml);
            var stream = new MemoryStream(array);
            var reader = new StreamReader(stream);
            log4net.Config.XmlConfigurator.Configure(logRepository, reader.BaseStream);
          
        }

一開始,是想着可以動態將不同的日志寫到不同的文件中去,這樣方便分類,后面類型太多,不想在log4net.config中一個個添加,所以干脆就動態生成日志配置項好了。

注意,以上方法生成的日志配應該是保存在內存中的,如果停止運行會消失,不會保存到log4net.config文件中。

 

類似在config文件中的:

    <logger name="TestLog">
      <level value="DEBUG"/>
      <appender-ref ref="TestLogAppender" />
    </logger>
    <appender name="TestLogAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logfile/" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <staticLogFileName value="false" />
      <datePattern value="'TestLog'-yyyyMMdd'.log'" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message%newline" />
      </layout>
    </appender>

 

 

net core 版本2.0/2.1都測試過了。   

log4net版本2.0.8

測試代碼太簡單就不放了。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com