Log4Net配置

 第一步,下载Log4Net.dll

http://download.csdn.net/detail/cpcpc/4011877

第二部,添加配置文件app.config,添加位置可在控制台应用程序下,Windows应用程序下,Windows服务下,Asp.Net网站下,Silverlight下

<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>


  <log4net>

    <!--1定义输出到文件中-->
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <!--定义文件存放位置-->
      <file type="log4net.Util.PatternString" value="%日志\%date{yyyMMdd}\log.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <!--输出格式-->
        <conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger - 错误描述:%message%newline" />
      </layout>
      <!--<filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="WARN" />
        <param name="LevelError" value="ERROR" />
        <param name="LevelFatal" value="FATAL" />
        <param name="LevelInfo" value="INFO" />
      </filter>-->
    </appender>

    <!--2定义输出到控制台命令行中-->
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger - 错误描述:%message%newline" />
      </layout>
      <!--<filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="FATAL" />
      </filter>-->
    </appender>

    <!--3定义输出到windows事件中-->
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger - 错误描述:%message%newline" />
      </layout>
    </appender>

    <!--4定义输出到数据库中,这里举例输出到Access数据库中,数据库为C盘的log4net.mdb-->
    <appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender">
      <connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\log4net.mdb" />
      <commandText value="INSERT INTO LogDetails ([LogDate],[Thread],[Level],[Logger],[Message]) VALUES (@logDate, @thread, @logLevel, @logger,@message)" />
      <!--定义各个参数-->
      <parameter>
        <parameterName value="@logDate" />
        <dbType value="String" />
        <size value="240" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="240" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logLevel" />
        <dbType value="String" />
        <size value="240" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="240" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="240" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
    </appender>

    <!--定义日志的输出媒介,下面定义日志以四种方式输出。也可以下面的按照一种类型或其他类型输出。-->
    <root>
      <!--文件形式记录日志-->
      <appender-ref ref="LogFileAppender" />
      <!--控制台控制显示日志-->
      <!--<appender-ref ref="ConsoleAppender" />-->
      <!--Windows事件日志-->
      <!--<appender-ref ref="EventLogAppender" />-->
      <!-- 如果不启用相应的日志记录,可以通过这种方式注释掉-->
      <!--<appender-ref ref="AdoNetAppender_Access" />-->
    </root>

    <logger name="AppLogging">
      <!--<level value="Info"/>-->
      <level value="All"/>
    </logger>

  </log4net>

</configuration>


第三步,添加log4Net.dll引用,创建LogOust类

public class LogOuts
    {
        public static void Debug(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            if(log.IsDebugEnabled)
            {
                log.Debug(message);
            }
        }

        public static void Error(string message)
        {
            //log4net.ILog log = log4net.LogManager.GetLogger("log4net");
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            if(log.IsErrorEnabled)
            {
                log.Error(message);
            }
        }

        public static void Fatal(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            if(log.IsFatalEnabled)
            {
                log.Fatal(message);
            }

        }

        public static void Info(string message)
        {
            //log4net.ILog log = log4net.LogManager.GetLogger("log4net");
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            if(log.IsInfoEnabled)
            {
                log.Info(message);
            }

        }

        public static void Warn(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            if(log.IsWarnEnabled)
            {
                log.Warn(message);
            }
        }
    }

第四步,在LogOuts类所在的类库中的Properties下的AssemblyInfo.cs中的添加

对于WINFORM应用程序,你可以加入
[assembly:log4net.Config.DOMConfigurator()]或
[assembly:log4net.Config.XmlConfigurator()]
对于WEBFORM你可以加入
[assembly:log4net.Config.DOMConfigurator(ConfigFile="web.config",Watch=true)]

第五步,调用,写入日志。在你需要记录日志的地方做如下调用即可

LogOuts.Info( “日志记录成功!”);

你可能感兴趣的:(thread,String,assembly,layout,log4net,newline)