log4net官方網址:http://logging.apache.org/log4net/ web
2.1 引入log4net.dll文件,將log4net.dll文件放到以下位置:項目根目錄下、bin/Debug,bin/Release三個地方
2.2 配置當前項目,這裏我使用了其中的一中方式,即直接配置到項目的.config文件中,web項目在Web.config中配置,非web項目在App.config中配置,配置內容以下: apache
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.windows.forms jitDebugging="true"/> <!-- log4net配置 --> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <log4net> <appender name="RollingFile" type="log4net.Appender.RollingFileAppender"> <file value="heatingBalance.log" /> <appendToFile value="true" /> <maximumFileSize value="1MB" /> <maxSizeRollBackups value="10" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="{%level} [%date - %thread] <%logger.%method_%line> : %message%newline" /> </layout> </appender> <!-- Set root logger level to DEBUG and its only appender to RollingFile --> <root> <level value="INFO" /> <appender-ref ref="RollingFile" /> </root> </log4net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
|
static class Program { private static readonly ILog log = LogManager.GetLogger(typeof(Program)); /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { log.Info("start application."); } }