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

推荐订阅源

G
GRAHAM CLULEY
V
V2EX
WordPress大学
WordPress大学
博客园 - Franky
Last Week in AI
Last Week in AI
博客园 - 司徒正美
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
V
Visual Studio Blog
C
CERT Recently Published Vulnerability Notes
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
腾讯CDC
The Hacker News
The Hacker News
Hugging Face - Blog
Hugging Face - Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
J
Java Code Geeks
人人都是产品经理
人人都是产品经理
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
A
Arctic Wolf
量子位
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
News and Events Feed by Topic
雷峰网
雷峰网
博客园_首页
Google Online Security Blog
Google Online Security Blog
Spread Privacy
Spread Privacy
罗磊的独立博客
H
Hacker News: Front Page
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
IT之家
IT之家
The Cloudflare Blog
爱范儿
爱范儿
博客园 - 叶小钗
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 谢绝围观

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