(一)使用log4net生成日志文件_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > (一)使用log4net生成日志文件

(一)使用log4net生成日志文件

 2014/11/5 21:11:43  julyweb  程序员俱乐部  我要评论(0)
  • 摘要:1、引入log4net.dll1.1Nuget安装或http://logging.apache.org/log4net/下载log4net的源代码,编译后把log4net.dll引入项目。2、配置log4net.config2.1在Web.config文件中进行添加configSections的节点<configSections><sectionname="log4net"type="log4net.Config
  • 标签:使用 文件 net

1、引入log4net.dll

1.1 Nuget安装 或 http://logging.apache.org/log4net/下载log4net的源代码,编译后把log4net.dll引入项目。

2、配置log4net.config

2.1 在Web.config文件中进行添加configSections的节点

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>

2.2 添加log4net.config(设置为始终复制到输出目录)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <log4net>
    <!-- 信息日志类 -->
    <logger name="loginfo">
      <level value="ALL" />
      <appender-ref ref="InfoAppender" />
    </logger>
    
    <!-- 信息日志附加介质 -->
    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
      <!-- 输出到什么目录 -->
      <param name="File" value="Log\\LogInfo\\" />
      <!-- 是否覆写到文件中 -->
      <param name="AppendToFile" value="true" />
      <!-- 备份文件的个数 -->
      <param name="MaxSizeRollBackups" value="50" />
      <!-- 单个日志文件最大的大小 -->
      <param name="MaxFileSize" value="10240" />
      <!-- 是否使用静态文件名 -->
      <param name="StaticLogFileName" value="false" />
      <!-- 日志文件名 -->
      <param name="DatePattern" value="yyyyMMdd&quot;.htm&quot;" />
      <param name="RollingStyle" value="Date" />
      <!-- 布局,更多参数可参照官网说明 -->
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="&lt;HR COLOR=red&gt;
          %n级别:%-5p &lt;BR&gt;
          %n时间:%d [%t] &lt;BR&gt;
          %n描述:%m &lt;BR&gt;"/>
      </layout>
    </appender>
  </log4net>
</configuration>

2.3 在AssemblyInfo.cs文件中指定配置log4net.config

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

3、代码使用示例

3.1 封装工具类LogHelper

namespace July.Demo.Win
{
    public class LogHelper
    {
        static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");

        public static void WriteLog(string info)
        {
            if (loginfo.IsInfoEnabled)
            {
                loginfo.Info(info);
            }
        }
    }
}

3.2 使用示例

private void Form1_Load(object sender, EventArgs e)
{
    LogHelper.WriteLog("窗体生成");
}

4、运行结果

4.1 效果图如下

adbnYHPYFJWzomqJr8Jn2A1+DkXIBrp3GEnlFO+Oza1PL20HC8/F6KnMr86znwatb956OTy/aPDJhdWt3dSMvZvb169e+vr5CofD9+/fT09PDw8OXL1/2lLl///7k5GQ6nf769avx0GvXHl9639czv2cad4bGf2lXb9JQTVw0NZntaHba2to8eWc7mqXvZ4dljrBxzbRwsEHHbVUuQqRBQdsdlLciGy5WqYhqzJu+Tu1Rnv7BAUTaJJ05i8xnFP4qBVL/aSbGVFYrXyNTLX6Pu2xHs7otSrX5kqpTSsmE7KuJqxEpWr7otefl5MrC5l/bh991NjQ6ef32YCrz/O6jkZ4Hr95mVwyJRdvYPZpf9nRiJicnnz59WiwWi8VioVDo6em57WVqaqqnp2dyclIONNvRLB95uOLwnR/+06XszOI0KicRk2Y7mmO/v1ReWqUTqHIE9ldFqq0xja4ngii0R0wTnfZUU/m1IHoiqjGvs/oaB/XpH1wUETZJlawj8hmZv+D9ra8doekQr4nqzfTZJ6E5KelGmyqGVJvT3FytL2XKkhi1tUmNn+D+7D/G44y8W+08fbewsn2s7fGMfOy6PXh78Hnv45H049G7D1/93vf46q2B/97o/c9vd//z293/dN9NDQwp910vfJ2ZXxEzGxwcfPfu3dra2uTkZF9f3y2Jhw8frqysDA4OyoHWrz2W0hOmg+BN2qi22xefvMkje6k279lnX0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