C# log4.net 用代码实现配置

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sumirry/article/details/80432949

主要有两个思路,一个是用类的属性来设置,一个是动态生成xml然后进行配置

良心之作,代码首创(我也是查了很久没找到,没办法才自己写的)

这样就可以污染程序配置文件进行写日志操作了。

干货直接上代码,个人觉得xml更靠谱些,因为网上关于xml配置的文章较多,类的要自己研究各种属性的设置

1.通过类属性配置

        log4net.Appender.RollingFileAppender[] appender = new log4net.Appender.RollingFileAppender[5];
            appender[0].File = System.IO.Path.Combine("Log\\Info\\");
            appender[0].Name = "Info";
            appender[1].File = System.IO.Path.Combine("Log\\Debug\\");
            appender[1].Name = "Debug";
            appender[2].File = System.IO.Path.Combine("Log\\Error\\");
            appender[2].Name = "Error";
            appender[3].File = System.IO.Path.Combine("Log\\Warn\\");
            appender[3].Name = "Warn";
            appender[4].File = System.IO.Path.Combine("Log\\Fatal\\");
            appender[4].Name = "Fatal";
            appender[5].File = System.IO.Path.Combine("Log\\Job\\");
            appender[5].Name = "Job";
            foreach (var a in appender)
            {
                a.AppendToFile = true;
                a.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Composite;
                a.MaxSizeRollBackups = -1;
                a.MaximumFileSize = "1MB";
                a.LockingModel = new MinimalLock();
                a.StaticLogFileName = false;
                a.DatePattern = "yyyy-MM-dd'.txt'";
                a.Layout = new log4net.Layout.PatternLayout("$$%date%message%newline");
                a.ActivateOptions();// 这个要调用一下呢
                log4net.Config.BasicConfigurator.Configure(a);
            }
通过动态生成xml配置
 XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));
            XmlElement configuration = xmlDoc.CreateElement("configuration");
            XmlElement root = xmlDoc.CreateElement("log4net");
       
         

            string[] leves = new string[] { "Info", "Debug", "Error", "Warn", "Fatal", "Job" };

            foreach(string myleve in leves)
            {

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

                XmlElement file = xmlDoc.CreateElement("file");
                file.SetAttribute("value", "Log\\"+ myleve + "\\");
                appender.AppendChild(file);

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

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

                XmlElement maxSizeRollBackups = xmlDoc.CreateElement("maxSizeRollBackups");
                maxSizeRollBackups.SetAttribute("value", "-1");
                appender.AppendChild(maxSizeRollBackups);

                XmlElement maximumFileSize = xmlDoc.CreateElement("maximumFileSize");
                maximumFileSize.SetAttribute("value", "1MB");
                appender.AppendChild(maximumFileSize);

                XmlElement lockingModel = xmlDoc.CreateElement("lockingModel");
                lockingModel.SetAttribute("type", "log4net.Appender.FileAppender+MinimalLock");
                appender.AppendChild(lockingModel);

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

                XmlElement DatePattern = xmlDoc.CreateElement("DatePattern");
                DatePattern.SetAttribute("value", "yyyy-MM-dd'.txt'");
                appender.AppendChild(DatePattern);

                XmlElement layout = xmlDoc.CreateElement("layout");
                layout.SetAttribute("type", "log4net.Layout.PatternLayout");

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

                XmlElement logger = xmlDoc.CreateElement("logger");
                logger.SetAttribute("name", myleve);
                XmlElement level = xmlDoc.CreateElement("level");
                level.SetAttribute("value", "ALL");
                XmlElement appender_ref = xmlDoc.CreateElement("appender-ref");
                appender_ref.SetAttribute("ref", "Tast"+ myleve);
                logger.AppendChild(level);
                logger.AppendChild(appender_ref);


                root.AppendChild(appender);
                root.AppendChild(logger);
            }
    
            configuration.AppendChild(root);
            xmlDoc.AppendChild(configuration);
      
            byte[] array = Encoding.ASCII.GetBytes(xmlDoc.OuterXml);
            MemoryStream stream = new MemoryStream(array);                
            StreamReader reader = new StreamReader(stream);
            log4net.Config.XmlConfigurator.Configure(reader.BaseStream);



展开阅读全文

没有更多推荐了,返回首页