首页 > 代码库 > log4Net控制台输出

log4Net控制台输出

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Config;

namespace ConsoleLog4Net
{
    class Program
    {
        static void Main(string[] args)
        {
            var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
            XmlConfigurator.ConfigureAndWatch(logCfg);
            ILog logger = LogManager.GetLogger(typeof(Program));

            logger.Debug("Debug");
            logger.Info("Info");
            logger.Warn("Warn");
            logger.Error("Error");
            logger.Fatal("Fatal");

            Console.ReadLine();
        }
    }
}

log4net.conifg

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
    </sectionGroup>
  </configSections>
  <log4net>
    <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value=http://www.mamicode.com/"ERROR" />
        <foreColor value=http://www.mamicode.com/"White" />
        <backColor value=http://www.mamicode.com/"Red, HighIntensity" />
      </mapping>
      <mapping>
        <level value=http://www.mamicode.com/"DEBUG" />
        <backColor value=http://www.mamicode.com/"Green" />
      </mapping>

      <mapping>
        <level value=http://www.mamicode.com/"Info" />
        <backColor value=http://www.mamicode.com/"Yellow" />
      </mapping>
      
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value=http://www.mamicode.com/"%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value=http://www.mamicode.com/"Debug"/>
        <param name="LevelMax" value=http://www.mamicode.com/"Fatal"/>
      </filter>
    </appender>

    <root>
      <level value=http://www.mamicode.com/"ALL" />
      <appender-ref ref="ColoredConsoleAppender" />
    </root>

    <logger>
      <level value=http://www.mamicode.com/"ALL" />
    </logger>

  </log4net>
</configuration>

 

log4Net控制台输出