huorswords / Microsoft.Extensions.Logging.Log4Net.AspNetCore

Allows to configure Log4net as Microsoft Extensions Logging handler on any ASP.NET Core application. Original code proposal by @anuraj --> https://dotnetthoughts.net/how-to-use-log4net-with-aspnetcore-for-logging/
Apache License 2.0
249 stars 123 forks source link

Since 3.1.0 + , log files cannot be generated to the current project directory. #98

Open anjoy8 opened 4 years ago

anjoy8 commented 4 years ago

Hi,huorswords,

My .netcore project was upgraded to the latest.net5.0, and I also upgraded to the latest version of Log4Net-related NuGet.

  <PackageReference Include="log4net" Version="2.0.12" />
  <PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="5.0.0" />

However, after testing, it could not be written to the log file during development, so I went back to the previous version and it was ok.

   <PackageReference Include="log4net" Version="2.0.8" />
   <PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="3.1.0" />

Here's how I configured it:

Program.cs

 Host.CreateDefaultBuilder(args)
     .ConfigureWebHostDefaults(webBuilder =>
     {
         webBuilder
          .UseStartup<Startup>()
          .ConfigureLogging((hostingContext, builder) =>
          {
              builder.AddFilter("System", LogLevel.Error);
              builder.AddFilter("Microsoft", LogLevel.Error);
              builder.AddFilter("Microsoft.Hosting.Lifetime", LogLevel.Error);

              var path = Path.Combine(Directory.GetCurrentDirectory(), "Log4net.config");
              builder.AddLog4Net(path);
          });
     });

Log4net.config

<?xml version="1.0" encoding="utf-8"?>
<log4net>
  <!--Error-->
  <appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
    <file value="Log/"/>
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="&quot;GlobalExceptionLogs_&quot;yyyyMMdd&quot;.log&quot;" />
    <StaticLogFileName value="false"/>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date| %-5level %newline%message%newline--------------------------------%newline" />
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="ERROR" />
      <levelMax value="FATAL" />
    </filter>
  </appender>
  <!--Error-->

  <root>
    <level value="ALL" />
    <appender-ref ref="ErrorLog" />
  </root>
</log4net>

Controller.cs

 private readonly ILogger<WeatherForecastController> _logger;

 public WeatherForecastController( ILogger<WeatherForecastController> logger  )
 {
     _logger = logger;
 }

 [HttpGet]
 public IEnumerable<WeatherForecast> Get()
 {
     _logger.LogError("this is an error");
}

I don't know where the problem is, please help to put forward Suggestions, thank you very much!

anjoy8 commented 4 years ago

Sorry, after checking, I found that the log files were generated under the Bin folder. Before, they were all generated under the root directory of the file.

<appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
    <file value="Log/"/>
...

Since 3.1.0, log files have been generated into the bin folder instead of the current project directory.