VS2017-NetCore项目整合Log4Net

1新建NetCore项目,我这里NetCoreSDK版本是2.2.0。

2.进入NuGet程序包官网 : https://www.nuget.org,搜索以下两个包并安装到项目中。

 Microsoft.Extensions.Logging.Log4Net.AspNetCore

 Log4Net

 

打开项目的程序包管理控制台,运行以下两条命令,安装Log4Net到项目中

 

a、Install-Package Microsoft.Extensions.Logging.Log4Net.AspNetCore -Version 2.2.10
b、Install-Package log4net -Version 2.0.8
 
3、项目新建一个目录,新增一个log4net.config的配置文件。

config内容如下。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- This section contains the log4net configuration settings -->
  <log4net>
    <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
    <!-- Set root logger level to ERROR and its appenders -->
    <root>
      <level value="ALL"/>
      <appender-ref ref="SysAppender"/>
    </root>
    <!-- Print only messages of level DEBUG or above in the packages -->
    <logger name="WebLogger">
      <level value="DEBUG"/>
    </logger>
    
    <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" >
      <param name="File" value="Content/Logs/" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout,log4net">
      <param name="ConversionPattern" value="%n日志时间:%d [%t] %n日志级别:%-5p %n日 志 类:%c [%L] %n%m %n" />
        <param name="Header" value="&#13;&#10;----------------------Header--------------------------&#13;&#10;" />
        <param name="Footer" value="&#13;&#10;----------------------Footer--------------------------&#13;&#10;" />
      </layout>
    </appender>
    
      
    <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
  </log4net>

</configuration>

 

4、在Startup.cs中的配置如下

        ...

     public static log4net.Repository.ILoggerRepository LogRepository { get; set; } public Startup(IConfiguration configuration) { Configuration = configuration; LogRepository = log4net.LogManager.CreateRepository("NetCoreRepository"); log4net.Config.XmlConfigurator.Configure(LogRepository, new FileInfo(@"Config\log4net.config")); }
    ...

 

5、新建一个Core文件夹,增加一个Log4Net.cs的日志帮助类

using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CMSCore.Web.Core
{
    public class Log4Net
    {
        private static ILog log;
        static Log4Net()
        {
            log = LogManager.GetLogger(Startup.LogRepository.Name,typeof(Log4Net));
        }
        public static void Info(string message)
        {
            log.Info(message);
        }

        public static void Warn(string message)
        {
            log.Warn(message);
        }
        public static void Warn(string message,Exception ex)
        {
            log.Warn(message,ex);
        }

        public static void Error(string message)
        {
            log.Error(message);
        }
        public static void Error(string message,Exception ex)
        {
            log.Error(message,ex);
        }

        public static void Debug(string message)
        {
            log.Debug(message);
        }
        public static void Debug(string message, Exception ex)
        {
            log.Debug(message, ex);
        }

    }
}

6、在项目中需要记录日志的类引用该命名空间,即可在项目中使用log4net的类的方法记录日志了。

 

 

posted @ 2019-05-14 18:43  黄鹏程  阅读(674)  评论(1编辑  收藏  举报