最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net core - Log4Net not logging information in .NET 8 - Stack Overflow

programmeradmin1浏览0评论

I'm trying to use Log4Net in my .NET 8 application

This is my log4net config:

<?xml version="1.0" encoding="utf-8"?>
<log4net>
    <root>
        <level value="ALL" />
        <appender-ref ref="RollingXMLFile" />
    </root>
    <appender name="RollingXMLFile" type="log4net.Appender.RollingFileAppender">
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <file value="C:\\AppPath\\Log.txt" />
        <appendToFile value="true" />
        <maximumFileSize value="5000KB" />
        <maxSizeRollBackups value="2" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="INFO" />
      <levelMax value="FATAL" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
</log4net>

When I try to run the application, I only see entries for ERROR and CRITICAL levels. Every other level is ignored.

Log4Net worked fine for my .NET 4.8 application. Is there any setting/configuration I am missing for .NET 8?

I'm trying to use Log4Net in my .NET 8 application

This is my log4net config:

<?xml version="1.0" encoding="utf-8"?>
<log4net>
    <root>
        <level value="ALL" />
        <appender-ref ref="RollingXMLFile" />
    </root>
    <appender name="RollingXMLFile" type="log4net.Appender.RollingFileAppender">
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <file value="C:\\AppPath\\Log.txt" />
        <appendToFile value="true" />
        <maximumFileSize value="5000KB" />
        <maxSizeRollBackups value="2" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="INFO" />
      <levelMax value="FATAL" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
</log4net>

When I try to run the application, I only see entries for ERROR and CRITICAL levels. Every other level is ignored.

Log4Net worked fine for my .NET 4.8 application. Is there any setting/configuration I am missing for .NET 8?

Share Improve this question edited Feb 28 at 2:22 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Feb 12 at 20:37 user3865946user3865946 536 bronze badges 6
  • Did you solve this problem? If not, can you share how you configured Log4Net in the Program.cs file and how you use the Log4Net service to log messages? According to your log4net.config file, I create a sample to test it, it seems that everything works well, the result like this. – Zhi Lv Commented Feb 26 at 8:35
  • This is how I have set up in program.cs and controller <ibb.co/60TM7cpW> – user3865946 Commented Feb 26 at 20:13
  • Might be security reason, I can't view the image, can you directly post the code or you can create a minimal sample and share it via github? – Zhi Lv Commented Feb 27 at 1:38
  • code image Please let me know if this works – user3865946 Commented Feb 27 at 15:02
  • Are you sure your config is even being loaded? If so see the log4net debugging guide - note that To enable log4net's internal debug programmatically you need to set the log4net.Util.LogLog.InternalDebugging property to true. – stuartd Commented Feb 27 at 15:30
 |  Show 1 more comment

1 Answer 1

Reset to default 0

code image Please let me know if this works

Based on your code, you can refer to the following sample (it works well on my side):

  1. Create a Net 8 MVC application and install the following package via NuGet:

       <ItemGroup>
         <PackageReference Include="log4net" Version="3.0.4" />
         <PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="8.0.0" />
       </ItemGroup>
    
  2. Create a log4net.config config file in the application’s root folder

     <?xml version="1.0" encoding="utf-8"?>
     <log4net>
         <root>
             <level value="ALL" />
             <appender-ref ref="RollingXMLFile" />
         </root>
         <appender name="RollingXMLFile" type="log4net.Appender.RollingFileAppender">
             <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
             <file value="app.txt" />
             <appendToFile value="true" />
             <maximumFileSize value="5000KB" />
             <maxSizeRollBackups value="2" />
             <layout type="log4net.Layout.PatternLayout">
                 <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
             </layout>
             <filter type="log4net.Filter.LevelRangeFilter">
                 <levelMin value="INFO" />
                 <levelMax value="FATAL" />
             </filter>
             <filter type="log4net.Filter.DenyAllFilter" />
         </appender>
     </log4net>
    
  3. Create a Services folder, and add a Log4netExtensions.cs class in the Service folder:

     public static class Log4netExtensions
     {
         public static void AddLog4net(this IServiceCollection services, string log4netconfig)
         {
             XmlConfigurator.Configure(new FileInfo(log4netconfig));
             services.AddSingleton(LogManager.GetLogger(typeof(Program)));
         }
     }
    
  4. In the Program.cs file, register the Log4Net service use the following code:

     builder.Logging.AddLog4Net("log4net.config");
    
  5. In the controller, inject the logger and add logs:

     public class HomeController : Controller
     {
         private readonly ILogger<HomeController> _logger;
    
         public HomeController(ILogger<HomeController> logger)
         {
             _logger = logger;
         }
    
         public IActionResult Index()
         {
             _logger.LogInformation("This is a default informational log.");
             _logger.LogDebug("This is a default LogDebug log.");
             _logger.LogWarning("This is a default LogWarning log.");
             _logger.LogError("This is a default LogError log.");
             _logger.LogCritical("This is a default LogCritical log.");
    
             return View();
         }
    

The result as below: in the application folder, find the app.txt file from bin\debug\net8.0 folder.

发布评论

评论列表(0)

  1. 暂无评论