2013-06-14 52 views
20

私はlog4netを使用してC#でテストコンソールアプリケーションを書いた:設定ファイルでlog4netが認識されないのはなぜですか?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

using log4net; 
using log4net.Config; 

[assembly: log4net.Config.XmlConfigurator(Watch = true)] 

namespace Log4Net_Test 
{ 
    class Program 
    { 
     private static readonly ILog log = LogManager.GetLogger(typeof(Program)); 

     static void Main(string[] args) 
     {  
      log.Info("Entering application");  
      log.Debug("Debug message");  
      log.Info("Leaving application"); 
     } 
    } 
} 

マイApp.configファイルは次のようになります。テストプログラムを実行

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <appSettings> 
    <add key="log4net.Internal.Debug" value="true"/> 
    </appSettings> 
    <log4net> 
    <!-- A1 is set to be a ConsoleAppender --> 
    <appender name="A1" type="log4net.Appender.FileAppender"> 
     <file value="logfile.txt" /> 
     <appendToFile value="false" /> 

     <!-- A1 uses PatternLayout --> 
     <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" /> 
     </layout> 
    </appender> 

    <!-- Set root logger level to DEBUG and its only appender to A1 --> 
    <root> 
     <level value="DEBUG" /> 
     <appender-ref ref="A1" /> 
    </root> 
    </log4net> 
</configuration> 

は、次のエラーメッセージで終わる:

log4net:ERROR Exception while reading ConfigurationSettings. Check your .config 
file is well formed XML. 
System.Configuration.ConfigurationErrorsException: Configuration system failed t 
o initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognize 
d configuration section log4net. 

設定ファイルに問題がありますか?

UPDATE 1: configSections´ part was missing, as pointed out in the accepted answer. But I also had to remove theスタートアップsection, otherwise the same error appeared. I do not know why the startup`セクションでは、あまりにも、問題を引き起こしています。おそらくもっと経験豊富な人がコメントを書いたり書き込んだりするかもしれません。 log4netのすることができます

フレームワーク.NET4.5をサポートしていないため

答えて

29

はあなたにも削除するセクションブロック

<configSections> 

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> 

</configSections> 
+0

ありがとうございました、このセクションは欠落していました。 –

11

必要なスタートアップのセクションでもlog4netを追加する必要があります私の似たような答えをここに見てください:https://stackoverflow.com/a/13236410/1783224

また、サポートされているフレームワークはドキュメントにあります:例えば

Exception while reading ConfigurationSettings. Check your .config file is well formed XML.

::また< 設定下でなければなりません> < configSectionsそのセクションのブロックを覚えておく必要があります

+5

私はlog4netのconfigSectionがその前に来る限り、4.5起動ブロックでうまく動作しています。 – nathanchere

+2

fix @ nathanchere suggest – tyh

+2

を確認できますこれは間違っています。 Log4Netは.NET 4.5を確実にサポートします。 – sean717

4

>はそれ以外の場合は、エラーが発生します

<configuration> 
    <configSections> 
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> 
    </configSections> 
    <!-- Log4net Logging Setup --> 
    <log4net> 
    <appender name="FileAppender" type="log4net.Appender.FileAppender,log4net"> 
     <file value="service.log" /> 
     <appendToFile value="true" /> 
     <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 
     <!-- 
     <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%date [%thread] %level %logger - %message%newline" /> 
     </layout> 
     --> 
     <layout type="log4net.Layout.PatternLayout"> 
     <param name="ConversionPattern" value="%date [%thread] %-5level %logger.%method - %message%newline" /> 
     </layout> 
     <filter type="log4net.Filter.LevelRangeFilter"> 
     <levelMin value="INFO" /> 
     <levelMax value="FATAL" /> 
     </filter> 
    </appender> 
    <root> 
     <level value="DEBUG" /> 
     <appender-ref ref="FileAppender" /> 
    </root> 
    </log4net> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> 
    </startup> 
</configuration> 
+2

うわー - これが私を助けました!しかし、さらに多くのものがあります。configSections-nodeは、設定ノードの下の最初のノードでなければなりません。 – Igor

関連する問題