2013-07-25 4 views
3

누군가가 저를 도울 수 있기를 바랍니다. 그것은 정말로 나를 미치게합니다.
간단한 간단한 Windows Forms 응용 프로그램이 있고 로깅을 위해 log4net 라이브러리를 사용하려고합니다. (주 프로젝트에서 제대로 작동하지 않았기 때문에이 프로젝트에서 테스트하고 있습니다.)Windows Forms 응용 프로그램에서 log4net이 작동하지 않습니다.

그래서 정규 Form1.cs, app.config, AssemblyInfo.cs 및 Program.cs가 있습니다. 내의 app.config에서

내가 가진 : 나타나면 Form1.cs에서

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <log4net> 
    <root> 
     <level value="DEBUG" /> 
     <appender-ref ref="LogFileAppender" /> 
    </root> 
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> 
     <file value="log-file.txt" /> 
     <appendToFile value="true" /> 
     <layout type="log4net.Layout.PatternLayout"> 
     <header value="[Your Header text here]" /> 
     <footer value="[Your Footer text here]" /> 
     <conversionPattern value="%date [%thread] %-5level %logger [%ndc] 
       &lt;%property{auth}&gt; - %message%newline" /> 
     </layout> 
    </appender> 

    </log4net> 

    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
</startup> 
</configuration> 

:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)] 
:
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using log4net; 
using log4net.Config; 

namespace log4net.test 
{ 
    public partial class Form1 : Form 
    {   
     private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 
     public Form1() 
     { 

      InitializeComponent();  
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      log4net.Config.XmlConfigurator.Configure(); 
      log.Debug("This is a DEBUG level message. The most VERBOSE level."); 
      log.Info("Extended information, with higher importance than the Debug call"); 
      log.Warn("An unexpected but recoverable situation occurred"); 
     } 
    } 
} 

그리고있는 AssemblyInfo.cs의 끝에

내가 추가 한 파일

디버깅하고 button1_Click으로 이동하면 아무 것도 일어나지 않는 것을 볼 수 있습니다. 로그 객체의 내용은 다음과 같습니다.
IsInfoEnabled, IsDebugEnabled, IsErrorEnabled, IsWarnEnabled가 false로 설정되고 아무 일도 일어나지 않습니다.

저는 하루 종일 해결책을 찾고자했습니다. 누군가 도울 수 있습니까?

+0

을, 당신의 app.config를 추정 무시됩니다. 그렇지 않으면 log4net 섹션이 app.config에서 올바르지 않다는 예외가 발생합니다. –

답변

0

당신은 구성의 루트 로깅 수준을 설정해야합니다

런타임시
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)] 

, 어떤 파일 "의 app.config"에서가 없습니다 : 문제는이 라인에

</appender> 

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

이미 설정되어 있습니다. 요소입니다. appender 요소를 시도했지만 동일합니다. – user2128702

+0

@ user2128702에서 [this post] (http://stackoverflow.com/questions/10295945/log4net-log-being-created-but-remaining-empty?rq=1)를 확인하십시오. –

+0

나는 당신의 대답을 찬찬히 고맙습니다 !!! 감사합니다 !!! 나는 하루 종일 그 일을하려고 노력해 왔지만, 그 동안 내가 를 추가하기를 간절히 바랬다. 나는 너에게 묻고 싶다. 왜 그런가? 그렇지 않으면 설정이 읽히지 않습니다. – user2128702

0

bin 디렉토리. 이 같은 올바른 파일 이름을 지정해야합니다 중 하나

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "<your assembly name>.exe.config", Watch = true)] 

또는 더 나은 그냥 내가 최근에 비슷한 문제로 실행

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

이름을 생략합니다. 로깅이 작동하지 않으면 일반적으로 다음과 같이 디버그 출력을 활성화합니다. How to track down log4net problems

0

또한 log4net에 대한 구성 섹션을 설정해야합니다. 구성 섹션을 설정하면 log4net은 아래를 참조 app.config에서 설정을 읽을 수 있습니다 : 당신이의 app.config의 configSections 섹션에 소개 된 log4net 구성 섹션을 가지고 있지 않기 때문에

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 


    /// Add this section before the log4net node 
    <configSections> 
     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 
    </configSections> 


    <log4net> 
    <root> 
     <level value="DEBUG" /> 
     <appender-ref ref="LogFileAppender" /> 
    </root> 
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> 
     <file value="log-file.txt" /> 
     <appendToFile value="true" /> 
     <layout type="log4net.Layout.PatternLayout"> 
     <header value="[Your Header text here]" /> 
     <footer value="[Your Footer text here]" /> 
     <conversionPattern value="%date [%thread] %-5level %logger [%ndc] 
       &lt;%property{auth}&gt; - %message%newline" /> 
     </layout> 
    </appender> 

    </log4net> 

    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
</startup> 
</configuration> 
관련 문제