log4net使用環境搭建

log4net官方網址:http://logging.apache.org/log4net/ web


一、下載log4net

    至於怎麼下載這個問題就不說了,直接去官方網站上找,我這裏使用的版本是 - log4net 1.2.13,包名稱爲:log4net-1.2.13-bin-newkey.zip


二、環境搭建

    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] &lt;%logger.%method_%line&gt; : %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>



    2.3 添加內容到 AssemblyInfo.cs


也就是這個字符串:
[assembly: log4net.Config.XmlConfigurator(Watch = true)] c#


    2.4 項目中使用
static class Program
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Program));
        /// <summary>
        /// 應用程序的主入口點。
        /// </summary>
        [STAThread]
        static void Main()
        {
                log.Info("start application.");
        }
    }


三、注意的問題:

    3.1 必須保證你的項目的.net 框架版本是 .NET Framework xxx(好比:.NET Framework 4),不能是:.NET Framework xxx Client Profile,否者會出現錯誤,具體出現什麼錯誤,你們能夠試試,能夠參考這篇博客:(http://blog.csdn.net/pfe_nova/article/details/12225349
相關文章
相關標籤/搜索