programing tip

Log4net이 로그 파일을 쓰지 않습니다

itbloger 2020. 6. 10. 22:44
반응형

Log4net이 로그 파일을 쓰지 않습니다


Log4net을 사용하여 간단한 시나리오를 만들었지 만 메시지가 로그 파일에 추가되지 않기 때문에 어 펜더가 작동하지 않는 것 같습니다.

web.config 파일에 다음을 추가했습니다.

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

<log4net>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
            <file value="D:\MyData\Desktop\LogFile.txt" />
            <appendToFile value="true" />
            <encoding value="utf-8" />
            <layout type="log4net.Layout.SimpleLayout" />
    </appender>


    <root>
        <level value="INFO" />
        <appender-ref ref="LogFileAppender" />
    </root>
</log4net>

글로벌 ASAX 파일 내에서 다음을 추가했습니다.

ILog logger = LogManager.GetLogger(typeof(MvcApplication));

그리고 Application_Start 메소드 내에서 :

logger.Info("Starting the application...");

내가 뭘 잘못 했어?


전화하세요

log4net.Config.XmlConfigurator.Configure();

log4net이 구성을 읽을 수있는 곳이 있습니까? 예를 들어 Global.asax에서 :

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup

    // Initialize log4net.
    log4net.Config.XmlConfigurator.Configure();
}

이 FAQ 페이지를 사용하십시오 : Apache log4net 자주 묻는 질문

약 3/4는 응용 프로그램 추적을 사용하여 log4net 디버깅을 활성화하는 방법을 알려줍니다. 문제가 어디에 있는지 알려줍니다.

기본 사항은 다음과 같습니다.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
</configuration>

그리고 당신은 표준 출력에서 ​​추적을 볼


@AndreasPaulsson이 제안했듯이 구성해야합니다. AssemblyInfo파일 에서 구성을 수행하고 있습니다 . configuration file name여기에 지정합니다 .

// Log4Net Configuration.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

또한 다음 그림과 같이 [log4net] .config에 대해 "항상 복사" 옵션이 선택되어 있는지 확인하십시오 .

여기에 이미지 설명을 입력하십시오


사이트에서 실행중인 프로세스 (계정)에 출력 디렉토리에 쓸 수있는 권한이 있는지 확인하십시오.

IIS 7 이상에서는 응용 프로그램 풀에서 구성되며 일반적으로 AppPool Identity 이며 일반적으로 모든 디렉토리에 쓸 수있는 권한이 없습니다.

이벤트 로그 (애플리케이션 및 보안)를 점검하여 예외가 발생했는지 확인하십시오.


끼워 넣다:

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

AssemblyInfo.cs 파일 끝에서


필자의 경우 프로젝트 이름에 공백이있어 log4net이 제대로 로깅되지 않았습니다. 왜 같은 코드가 다른 프로젝트에서 잘 작동했지만 새로운 코드에서는 작동하지 않는지 알 수 없었습니다. 공백. 간단한 공간.

So, beware spaces in project names. I've learned my lesson.


For me I moved the location of the logfiles and it was only when I changed the name of the file to something else it started again.

It seems if there is a logfile with the same name already existing, nothing happens.

Afterwards I rename the old file and changed the log filename in the config back again to what it was.


In my case I had to give the IIS_IUSRS Read\write permission to the log file.


Make sure the following line code should be there in AssemblyInfo.cs file.

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]

and also check for this line in Application_start() method.

log4net.Config.XmlConfigurator.Configure();

참고URL : https://stackoverflow.com/questions/3618380/log4net-does-not-write-the-log-file

반응형