通过代码来调用log4net写日志

使用如下配置

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="errorAppender" type="log4net.Appender.RollingFileAppender">
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="ERROR" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    <File value="Logs\err.log" />
    <PreserveLogFileNameExtension value="true" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="infoAppender" type="log4net.Appender.RollingFileAppender">
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="INFO" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    <File value="Logs\info.log" />
    <PreserveLogFileNameExtension value="true" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="debugAppender" type="log4net.Appender.RollingFileAppender">
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="DEBUG" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    <File value="Logs\debug.log" />
    <PreserveLogFileNameExtension value="true" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="perfAppender" type="log4net.Appender.RollingFileAppender">
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="INFO" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    <File value="Logs\perf.log" />
    <PreserveLogFileNameExtension value="true" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %logger - %message%newline" />
    </layout>
  </appender>
  <root>
    <level value="ALL" />
    <appender-ref ref="errorAppender" />
    <appender-ref ref="infoAppender" />
    <appender-ref ref="debugAppender" />
  </root>
  <logger name="Performance" additivity="false">
    <level value="ALL" />
    <appender-ref ref="perfAppender" />
  </logger>
</log4net>

然后需要调用

 string ConfigFile;

        private void LoadLogConfig()
        {
            if (ConfigFile == null)
            {
                return;
            }
            log4net.Config.XmlConfigurator.Configure(new FileInfo(ConfigFile));
        }

        protected void LogFactoryBase(string configFile)
        {
            if (Path.IsPathRooted(configFile))
            {
                ConfigFile = configFile;
                return;
            }
            var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);

            if (File.Exists(filePath))
            {
                ConfigFile = filePath;
                return;
            } 
            
            filePath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config"), configFile);//这里的config是debug下的子文件夹

            if (File.Exists(filePath))
            {
                ConfigFile = filePath;
                return;
            }
        }

需要调用2个函数,调用的先后顺序是

LogFactoryBase("App1.config");
LoadLogConfig();

需要设置App1.config输出到目录

 

调用的代码,Type是引发这个错误的类

public static void WriteLog(Type t, Exception ex)
       {
          log4net.ILog log = log4net.LogManager.GetLogger(t);
          //log.IsErrorEnabled = true;
           log.Error("Error", ex);
       }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。