標籤:
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="ERROR" /> <foreColor value="White" /> <backColor value="Red, HighIntensity" /> </mapping> <mapping> <level value="DEBUG" /> <backColor value="Green" /> </mapping> <mapping> <level value="Info" /> <backColor value="Yellow" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Debug"/> <param name="LevelMax" value="Fatal"/> </filter> </appender> <root> <level value="ALL" /> <appender-ref ref="ColoredConsoleAppender" /> </root> <logger> <level value="ALL" /> </logger> </log4net></configuration>
log4Net控制台輸出