【.NET Core】控制台程序使用 log4net

时间:2024-03-30 16:22:04

网上看到的都是.net core mvc和webapi使用log4net的例子,很少看到控制台的

1.安装log4net 

    建立.NET Core工程 - 右键 - 管理NuGet程序包 - 搜索log4net - 安装。

【.NET Core】控制台程序使用 log4net

2.日志输出到控制台

using log4net;
using log4net.Config;
using log4net.Repository;
using System;
 
namespace LogTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository");
            BasicConfigurator.Configure(repository);
            ILog log = LogManager.GetLogger(repository.Name, "NETCorelog4net");
 
            log.Info("NETCorelog4net log");
            log.Error("error");
            log.Warn("warn");
            Console.ReadKey();
        }
    }
}

3.日志输出到文件

    添加配置文件config.xml(右键 - 属性 - 复制到输出目录 - 始终复制),文件的内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- This section contains the log4net configuration settings -->
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
    </appender>
 
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="log-file.log" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
 
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logfile/" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd'.log'" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
 
    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
      <level value="ALL" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="FileAppender" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
 
  </log4net>
</configuration>
using log4net;
using log4net.Config;
using log4net.Repository;
using System;
using System.IO;
 
namespace LogTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository");
            XmlConfigurator.Configure(repository, new FileInfo("config.xml"));
            ILog log = LogManager.GetLogger(repository.Name, "NETCorelog4net");
 
            log.Info("NETCorelog4net log");
            log.Error("error");
            log.Warn("warn");
            Console.ReadKey();
        }
    }
}

    控制台的输出如下:

【.NET Core】控制台程序使用 log4net

     另外,生成一个文件夹logfile和一个以运行时的日期命名的文件20181025.log(配置文件中指定)。

转自:https://blog.csdn.net/liyazhen2011/article/details/83382221