log4net在多层架构中的使用

在底层类库项目中定义一个日志类

   public class LogHelper
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger("MyLogger");
        public static void Debug(string content)
        {
            log.Debug(content);

        }
        public static void Info(string content)
        {
            log.Info(content);

        }
    }

在该类库中引用log4net.dll,并在

assemblyInfo.cs中添加

[assembly: log4net.Config.DOMConfigurator(ConfigFile="log4net.config",Watch = true)]

然后再该项目中添加log4net配置文件log4net.config,代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>   
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.0"/>
  </configSections>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="log/log-"/>
      <param name="AppendToFile" value="true"/>
      <param name="MaxSizeRollBackups" value="10"/>
      <param name="StaticLogFileName" value="false"/>
      <param name="DatePattern" value="yyyy-MM-dd&quot;.log&quot;"/>
      <param name="RollingStyle" value="Date"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n%n"/>
      </layout>
    </appender>
  </log4net> 
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

上层模块中可以直接调用

 

 

 

你可能感兴趣的:(log)