简体   繁体   中英

Log4Net is not creating logs in bin folder in Asp.Net Core 3.1 API

I am trying to write logs in Asp.NEt Core 3.1 API. but it's not writing to the bin folder. instead, it has created the log file in the Temp folder in project directory.

Microsoft.Extensions.Logging.Log4Net.AspNetCore version 3.1.0

log4net.config

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <file value="Temp\" />
    <datePattern value="yyyy-MM-dd.'txt'"/>
    <staticLogFileName value="false"/>
    <appendToFile value="true"/>
    <rollingStyle value="Date"/>
    <maxSizeRollBackups value="100"/>
    <maximumFileSize value="15MB"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level App  %newline %message %newline %newline"/>
    </layout>
  </appender>
  <root>
    <level value="ALL"/>
    <appender-ref ref="RollingLogFileAppender"/>
  </root>
</log4net>

Startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddLog4Net();
}

This line is setting the folder to Temp: <file value="Temp\" /> .

Change it to the full path of the bin folder or to the path of the bin folder relative to your working directory of your application.

For example: .\ or .\bin\

By default Visual Studio will run your project and set the working directory to the project path.

On your project settings, in the Debug section you can manually override the working directory to bin, or you can change Launch mode from Project to Executable and select the executable on your bin folder.

log4net will always use current directory (working directory)to as the root of realitve path defined in config file, so when application was started via VS, it is pointing to project folder, when application was started directly in bin folder, log file would be placed in bin folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM