Asp.Net 6.0集成 Log4Net

环境

需要安装NuGet包,明细如下:

  • log4net
  • Microsoft.Extensions.Logging.Log4Net.AspNetCore

配置文件

文件名称 log4net.config,默认可以放在与启动类Program.cs同级目录下

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <!-- Define the appender for info level -->
  <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
    <file value="./LOG/info/" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="'info.'yyyy-MM-dd'.log'" />
    <staticLogFileName value="false" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
    <!-- Define a filter to only accept info level -->
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="INFO" />
      <levelMax value="INFO" />
    </filter>
  </appender>

  <!-- Define the appender for error level -->
  <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
    <file value="./LOG/error/" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="'error.'yyyy-MM-dd'.log'" />
    <staticLogFileName value="false" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline%exception%newline" />
    </layout>
    <!-- Define a filter to only accept error level -->
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="ERROR" />
      <levelMax value="ERROR" />
    </filter>
  </appender>

  <!-- Set root logger level to ALL and its appenders to InfoAppender and ErrorAppender -->
  <root>
    <level value="ALL"/>
    <!-- You can also use other levels such as DEBUG, WARN, FATAL etc. -->
    <!-- See https://logging.apache.org/log4net/release/sdk/log4net.Core.Level.html for more details -->

    <appender-ref ref ="InfoAppender"/>
    <appender-ref ref ="ErrorAppender"/>
  </root>
</log4net>


配置

在启动类中进行配置(Program.cs)

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
#region 配置 log4Net 需要安装log4net、Microsoft.Extensions.Logging.Log4Net.AspNetCore
builder.Services.AddLogging(cfg =>
{
    cfg.AddLog4Net();
    //默认的配置文件路径是在根目录,且文件名为log4net.config
    //如果文件路径或名称有变化,需要重新设置其路径或名称
    //比如在项目根目录下创建一个名为cfg的文件夹,将log4net.config文件移入其中,并改名为log.config
    //则需要使用下面的代码来进行配置
    //cfg.AddLog4Net(new Log4NetProviderOptions()
    //{
    //    Log4NetConfigFileName = "cfg/log.config",
    //    Watch = true
    //});
});
#endregion

使用


[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{

    private readonly ILogger<WeatherForecastController> _logger;
// 构造函数注入
    public WeatherForecastController(ILogger<WeatherForecastController> logger, IUnitOfWork uow)
    {
        _logger = logger;
        Uow = uow;
    }

    public IUnitOfWork Uow { get; }

    [HttpGet("GetUsers")]
    public async Task<ApiResponse> GetUsers()
    {
        IRepository<User> repository = Uow.GetRepository<User>();
        IList<User> users = await repository.GetAllAsync(orderBy: f => f.OrderByDescending(x => x.UpdateDate));
        _logger.LogInformation("查询所有用户");
        return new ApiResponse(true, users);
    }

}

效果

在这里插入图片描述文章来源地址https://www.uudwc.com/A/pjYy5/

原文地址:https://blog.csdn.net/xiuyuandashen/article/details/132814411

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

上一篇 2023年09月12日 05:56
iOS pod repo push 报错 ld: file not found: libarclite_iphoneos.a 问题解决方案
下一篇 2023年09月12日 05:56