Log4Net的使用

1)、添加对log4net.dll的引用

 

 

 2)、在配置文件中,对log4net进行配置

"1.0" encoding="utf-8" ?>


  
    
"log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> "RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> "test.txt"/> "true"/> "10"/> "1024KB"/> "Size"/> "true"/> "log4net.Layout.PatternLayout"> "%date [%thread] %-5level %logger - %message%newline"/> "DEBUG"/> ref ref="RollingLogFileAppender"/> "v4.0" sku=".NETFramework,Version=v4.5.1" />
View Code
MVC中:

    
    
"log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> "ERROR" /> ref ref="SysAppender" /> "WebLogger"> "DEBUG" /> "SysAppender" type="log4net.Appender.RollingFileAppender,log4net"> "File" value="App_Data/" /> "AppendToFile" value="true" /> "RollingStyle" value="Date" /> "DatePattern" value=""Logs_"yyyyMMdd".txt"" /> "StaticLogFileName" value="false" /> "log4net.Layout.PatternLayout,log4net"> "ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> "Header" value=" ---------header------- " /> "Footer" value=" ---------fotter------- " /> "consoleApp" type="log4net.Appender.ConsoleAppender,log4net"> "log4net.Layout.PatternLayout,log4net"> "ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
View Code

 

 

Log4Net的使用_第1张图片

 

 

 3)、从配置文件中读取log4net的配置,然后进行初始化

log4net.Config.XmlConfigurator.Configure();

4)、获取日志记录器

ILog logWriter = LogManager.GetLogger("TestDemo");

5)、写入日志(默认写入在bin目录下)

//创建一个调试级别的消息
logWriter.Debug("ssss调试级别的消息");
//创建一个错误级别的消息
logWriter.Error("ssss错误级别的消息");

 

你可能感兴趣的:(Log4Net的使用)