2013-06-14 21 views
20

ho scritto un test di applicazione di console in C# utilizzando log4net:Perché log4net non è riconosciuto nel file di configurazione?

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"); 
     } 
    } 
} 

Il mio file App.config si presenta così:

<?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> 

L'esecuzione del programma di test finisce il seguente messaggio di errore:

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. 

Cosa c'è di sbagliato nel mio file di configurazione?

UPDATE 1: Il configSections´ part was missing, as pointed out in the accepted answer. But I also had to remove the avvio section, otherwise the same error appeared. I do not know why the sezione startup` è la causa del problema, anche. Forse qualcuno più esperto può dire e scrivere un commento.

risposta

29

è necessario aggiungere log4net anche nel blocco sezione

<configSections> 

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

</configSections> 
+0

Grazie , questa sezione mancava. –

11

La sezione di avvio necessario per essere rimosso anche perché log4net non supportare .NET4.5 quadro

Puoi vedere la mia risposta simile qui: https://stackoverflow.com/a/13236410/1783224

Oppure è possibile visualizzare i framework supportati nella documentazione: http://logging.apache.org/log4net/release/features.html

+5

Ho funzionato bene con il blocco di avvio 4.5, purché la configSection con log4net venga prima di esso. – nathanchere

+2

Posso confermare la correzione suggerita da @nathanchere – tyh

+2

Questa informazione non è corretta. Log4Net supporta sicuramente .NET 4.5. – sean717

4

Si dovrebbe anche ricordare che il blocco sezione < configSections> deve essere sotto la configurazione < > altrimenti si è verificato un errore:

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

Ad esempio:

<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

Wow - questo mi ha aiutato! Ma c'è ancora di più: il nodo configSections-HA IL PRIMO nodo sotto il nodo di configurazione, altrimenti si riceverà quel messaggio che hai menzionato. – Igor

Problemi correlati