解决问题:

  1. 将log4net配置文件与app.config配置文件分开
  2. 手动读取log4net配置文件
  3. 手动创建logger
  4. 可将日志输出功能封装在类库中,应用程序引用时无需添加assembly引用及配置文件添加
 private void CreatLoggers()
        {
            XmlDocument log4netConfig = new XmlDocument();
            log4netConfig.LoadXml(@"
                    <log4net>
                        <logger name=""logerror"">
                          <level value=""ERROR"" />
                          <appender-ref ref=""ErrorAppender"" />
                        </logger>
                        <logger name=""loginfo"">
                          <level value=""INFO"" />
                          <appender-ref ref=""InfoAppender"" />
                        </logger>
                        <logger name=""logdebug"">
                          <level value=""DEBUG"" />
                          <appender-ref ref=""DebugAppender"" />
                        </logger>
                        <appender name=""DebugAppender"" type=""log4net.Appender.RollingFileAppender"">
                          <param name=""File"" value=""Log\\LogDebug\\"" />
                          <param name=""AppendToFile"" value=""true"" />
                          <param name=""MaxSizeRollBackups"" value=""100"" />
                          <param name=""MaxFileSize"" value=""10240"" />
                          <param name=""StaticLogFileName"" value=""false"" />
                          <param name=""DatePattern"" value=""yyyyMMdd&quot;.txt&quot;"" />
                          <param name=""RollingStyle"" value=""Date"" />
                          <layout type=""log4net.Layout.PatternLayout"">
                            <param name=""ConversionPattern"" value=""%d[%t] %m ;%n"" />
                          </layout>
                        </appender>
                        <appender name=""ErrorAppender"" type=""log4net.Appender.RollingFileAppender"">
                          <param name=""File"" value=""Log\\LogError\\"" />
                          <param name=""AppendToFile"" value=""true"" />
                          <param name=""MaxSizeRollBackups"" value=""100"" />
                          <param name=""MaxFileSize"" value=""10240"" />
                          <param name=""StaticLogFileName"" value=""false"" />
                          <param name=""DatePattern"" value=""yyyyMMdd&quot;.txt&quot;"" />
                          <param name=""RollingStyle"" value=""Date"" />
                          <layout type=""log4net.Layout.PatternLayout"">
                            <param name=""ConversionPattern"" value=""%d[%t] %m ;%n"" />
                          </layout>
                        </appender>
                        <appender name=""InfoAppender"" type=""log4net.Appender.RollingFileAppender"">
                          <param name=""File"" value=""Log\\LogInfo\\"" />
                          <param name=""AppendToFile"" value=""true"" />
                          <param name=""MaxFileSize"" value=""10240"" />
                          <param name=""MaxSizeRollBackups"" value=""100"" />
                          <param name=""StaticLogFileName"" value=""false"" />
                          <param name=""DatePattern"" value=""yyyyMMdd&quot;.txt&quot;"" />
                          <param name=""RollingStyle"" value=""Date"" />
                          <layout type=""log4net.Layout.PatternLayout"">
                            <param name=""ConversionPattern"" value=""%d[%t] %m ;%n "" />
                          </layout>
                        </appender>
                    </log4net>");

            ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
            XmlConfigurator.Configure(rep, log4netConfig["log4net"]);
            logdebug = LogManager.GetLogger(rep.Name, "logdebug");
            loginfo = LogManager.GetLogger(rep.Name, "loginfo");
            logerror = LogManager.GetLogger(rep.Name, "logerror");
        }

  上述XML文件内容我手动放在代码中,也可以存在xml文件中去进行读取操作,一个道理。

  值得强调的是:

  1. 在代码中形式的配置文件内容都是两个引号(“”),不是一个(”)
  2. 不需要添加那句“[assembly: log4net.Config.XmlConfigurator(Watch = true)]”

 

版权声明:本文为Khan-Sadas原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Khan-Sadas/p/12606085.html