惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
量子位
aimingoo的专栏
aimingoo的专栏
V
Visual Studio Blog
Y
Y Combinator Blog
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog
B
Blog
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
博客园 - 聂微东
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks

博客园 - 谢绝围观

LeetCode 910. Smallest Range II EAC 抓取CD为AAC文件 Binary Indexed Tree (Fenwick Tree) Lint Code 1365. Minimum Cycle Section LintCode 896. Prime Product 简明题解 UVa 1471 - Defense Lines Windows下安装配置MinGW GCC调试环境 详解LeetCode 137. Single Number II LeetCode 309. Best Time to Buy and Sell Stock with Cooldown LeetCode 84. Largest Rectangle in Histogram 修改注册表删除Windows资源管理器 “通过QQ发送” 右键菜单项 LeetCode 312. Burst Balloons LeetCode 287. Find the Duplicate Number LeetCode 10. Regular Expression Matching LeetCode 117. Populating Next Right Pointers in Each Node II 在Windows Azure VM下搭建SSTP VPN - 谢绝围观 利用.NET Code Contracts实现运行时验证 解决Windows Server 2012 下利用RRAS创建VPN断线的问题 - 谢绝围观 慎用静态类static class
Log4net 配置实例
谢绝围观 · 2015-04-06 · via 博客园 - 谢绝围观

首先需要下载并引用Log4net的binary。这一步可以通过在Visual Studio里的Manage Nuget package for solution轻松添加。

第二步是配置config文件,可以是App.config或Web.config或者单独的config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="RollingFileAppender" />
    </root>
    <logger name="default">
    </logger>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level - %message%newline" />
      </layout>
    </appender>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="CrawlerLog.txt" />
      <threshold value="WARN"/>
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <footer value="-------------------------------------------------------------" />
        <conversionPattern value="%newline%-5level %date %newline%message%newline%exception" />
      </layout>
    </appender>
  </log4net>
</configuration>

第三步,需要告诉Log4net去哪里找这些配置,这一步可以通过在AssemblyInfo.cs里添加以下内容完成:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "<your exe name>.exe.config", ConfigFileExtension = "config", Watch = true)]

如果是web项目,可以在Global.asax里的Application_Start方法里添加一行:

log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath("web.config")));

第四步,创建一个ILog实例,这样就可以开始使用了:

using System;
using log4net;

namespace MyConsoleApp
{
    class Program
    {
        static ILog logger = log4net.LogManager.GetLogger("default");

        static int Main(string[] args)
        {
            logger.Debug("debug msg");
            logger.Info("info msg");
            logger.Warn("warn msg");
            logger.Error("error msg", new Exception("test exception"));
            Console.Read();
            return 0;
        }
    }
}

一些补充说明:

  1. 配置文件里所有的<logger>节点都继承自<root>,默认使用root的配置。
  2. 可以通过设置appender的threshold属性来限制写入log的级别,比如文中示例config就配置为所有log都可以输出到控制台,但只有WARN及以上级别的log会记录到磁盘文件log。
  3. log输出格式定义遵循C语言printf风格,更多参数的详细说明可参考:http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html
  4. 各种Appender的配置示例可参考:http://logging.apache.org/log4net/release/config-examples.html