码迷,mamicode.com
首页 > Web开发 > 详细

asp.net core3.1 log4net 整合

时间:2020-03-23 15:28:46      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:returns   pre   name   dex   ram   epo   time   array   you   

第一步: NuGet管理安装log4net组件。

 

第二步:添加log4net.config配置文件

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <root>
    <level value="DEBUG"/>
    <appender-ref ref="SysAppender"/>
    <appender-ref ref="consoleApp"/>
  </root>
  <!--信息日志配置-->
  <appender name="SysAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="D:\\Logs\\" />
    <param name="AppendToFile" value="true" />
    <param name="MaximumFileSize" value="20MB" />
    <param name="MaxSizeRollBackups" value="100" />
    <param name="StaticLogFileName" value="false" />
    <param name="DatePattern" value="\\yyyyMM\\yyyyMMdd&quot;.log&quot;" />
    <param name="RollingStyle" value="Composite" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%n&#13;&#10;--------------------------------------------------------------------------------------&#13;&#10;%n日志时间:%d [%t] %n日志级别:%-5p %n日 志 类:%c [%x] %n%m %n" />
      <param name="Header" value="&#13;&#10;======================================================================================&#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>
  <logger name="WebLogger">
    <level value="DEBUG"/>
  </logger>

</log4net>

 

第三步:定义一个配置,添加一个类UseLog4netApplicationBuilder.cs

public static class UseLog4netApplicationBuilder
    {
        /// <summary>
        /// 使用log4net配置
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseLog4net(this IApplicationBuilder app)
        {
            var logRepository = log4net.LogManager.CreateRepository(System.Reflection.Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));
            log4net.Config.XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
            return app;
        }
    }

 

第四步: Startup.cs添加Configure

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseLog4net();    //这里是添加的

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        } 

 

第六步: Controller里面使用

 public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILog log;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            this.log = LogManager.GetLogger(typeof(WeatherForecastController)) ;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            log.Error("Error 90000: Error Error Error Error Error Error Error Error!");
            log.Info("Info 10000: Info Info Info Info Info Info Info Info Info Info Info Info Info Info!");
            log.Warn("Warn 20000: Warn Warn Warn Warn Warn Warn Warn Warn Warn Warn Warn Warn Warn Warn!");

            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }

 

第七步: 查看日志, 找到你定义的路径,打开log文件

======================================================================================


--------------------------------------------------------------------------------------

日志时间:2020-03-23 14:38:20,372 [9] 
日志级别:ERROR 
日 志 类:AspNetCoreApp.Controllers.WeatherForecastController [(null)] 
Error 90000: Error Error Error Error Error Error Error Error! 


--------------------------------------------------------------------------------------

日志时间:2020-03-23 14:38:20,394 [9] 
日志级别:INFO  
日 志 类:AspNetCoreApp.Controllers.WeatherForecastController [(null)] 
Info 10000: Info Info Info Info Info Info Info Info Info Info Info Info Info Info! 


--------------------------------------------------------------------------------------

日志时间:2020-03-23 14:38:20,395 [9] 
日志级别:WARN  
日 志 类:AspNetCoreApp.Controllers.WeatherForecastController [(null)] 
Warn 20000: Warn Warn Warn Warn Warn Warn Warn Warn Warn Warn Warn Warn Warn Warn! 

 

asp.net core3.1 log4net 整合

标签:returns   pre   name   dex   ram   epo   time   array   you   

原文地址:https://www.cnblogs.com/whtydn/p/12552120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!