Log4net

环境:.net6

开发工具: VS2022 SQL2014

 

 

日志组件Log4net

   上线的项目是必须要有监控日志,便于发现以及处理各种异常信息

 

 1添加Nuget程序包

     1添加最新版本的log4net程序包,添加完成后,可以在依赖项的包下看到添加的log4net版本

     2添加Microsoft.Extensions.Logging.Log4Net.AspNetCore

2 配置文件

      官网地址: https://logging.apache.org/log4net/download_log4net.html

 在官网下载log4net的配置文件,xml格式,配置文件里大部分参数会有说明,部分未说明的参数单独列举出来说明

 

1appender节点参数

   Name 可以随意写,但是注意不能重复

   Type  指定写入的类型(文本:  log4net.Appender.RollingFileAppender   数据库:  log4net.Appender.AdoNetAppender )

2Root节点

   <level value="INFO"/>                       设置日志级别

   <appender-ref ref="rollingAppender" />         引用设置的写入类型(这里需要和appender节点里的Name一致)

 

 

3 注册服务

    Program.cs文件里进行注册

       : var builder = WebApplication.CreateBuilder(args);

从以上我们可以看出其实返回的是一个WebApplicationBuilder,为了方便使用进行修改

       WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

 

builder.Logging.AddLog4Net("CfgFile/log4net.Config");//配置文件存放的地址

 

 

4 Log4net写入

1文本写入

log4net.Config配置

1 添加appender节点

  appender标签里的Name 可以随意写,但是注意不能重复

  appender标签里的Type  文本:  log4net.Appender.RollingFileAppender   

2Root节点

   <level value="INFO"/>                       设置日志级别

   <appender-ref ref="rollingAppender" />         引用设置的写入类型(这里需要和appender节点里的Name一致)

 

3写入日志相关代码

   public class SecondController : Controller

    {

 

        /// <summary>

        /// 构造属性注入

        /// </summary>

        private readonly ILogger<SecondController> _logger;

        private readonly ILoggerFactory _Factory;

 

        public  SecondController(ILogger<SecondController> logger,ILoggerFactory factory)

        {

            this._logger = logger;

            this._logger.LogInformation($"{this.GetType().Name} 被构造了。。。。_logger ");

 

 

            //通过创建工厂

            this._Factory = factory;

            ILogger<SecondController> logger1 = this._Factory.CreateLogger<SecondController>();

            logger1.LogInformation($"{this.GetType().Name}被构造了。。。。。logger1");

 

        }

 

        public IActionResult Index()

        {

            this._logger.LogInformation($"Index 被执行了");

            ILogger<SecondController> _Logger3 = this._Factory.CreateLogger<SecondController>();

            _Logger3.LogInformation($"Index 被执行了。。。。。_Logger3");
   public class SecondController : Controller

    {

 

        /// <summary>

        /// 构造属性注入

        /// </summary>

        private readonly ILogger<SecondController> _logger;

        private readonly ILoggerFactory _Factory;

 

        public  SecondController(ILogger<SecondController> logger,ILoggerFactory factory)

        {

            this._logger = logger;

            this._logger.LogInformation($"{this.GetType().Name} 被构造了。。。。_logger ");

 

 

            //通过创建工厂

            this._Factory = factory;

            ILogger<SecondController> logger1 = this._Factory.CreateLogger<SecondController>();

            logger1.LogInformation($"{this.GetType().Name}被构造了。。。。。logger1");

 

        }

 

        public IActionResult Index()

        {

            this._logger.LogInformation($"Index 被执行了");

            ILogger<SecondController> _Logger3 = this._Factory.CreateLogger<SecondController>();

            _Logger3.LogInformation($"Index 被执行了。。。。。_Logger3");

 

 

 

    

2写入SQL

      注意: 需要在数据库添加日志写入的表

Nuget添加

System.Data.SqlClient

log4net.Config配置

添加appender节点

  appender标签里的Name 可以随意写,但是注意不能重复

  appender标签里的Type  数据库:  log4net.Appender.AdoNetAppender )

添加connectionString标签

    appender节点下添加<connectionString value="Data Source=;Initial Catalog=LogManager;Persist Security Info=True;User ID=;Password=" />

添加commandText标签

  appender节点下添加commandText value="INSERT INTO Log4Net ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />

   

Root节点

   <level value="INFO"/>                       设置日志级别

   <appender-ref ref="rollingAppender" />         引用设置的写入类型(这里需要和appender节点里的Name一致)

 

写入日志相关代码

 

   public class SecondController : Controller

    {

 

        /// <summary>

        /// 构造属性注入

        /// </summary>

        private readonly ILogger<SecondController> _logger;

        private readonly ILoggerFactory _Factory;

 

        public  SecondController(ILogger<SecondController> logger,ILoggerFactory factory)

        {

            this._logger = logger;

            this._logger.LogInformation($"{this.GetType().Name} 被构造了。。。。_logger ");

 

 

            //通过创建工厂

            this._Factory = factory;

            ILogger<SecondController> logger1 = this._Factory.CreateLogger<SecondController>();

            logger1.LogInformation($"{this.GetType().Name}被构造了。。。。。logger1");

 

        }

 

        public IActionResult Index()

        {

            this._logger.LogInformation($"Index 被执行了");

            ILogger<SecondController> _Logger3 = this._Factory.CreateLogger<SecondController>();

            _Logger3.LogInformation($"Index 被执行了。。。。。_Logger3");

 

 

            return View();

        }

    }

 

posted @ 2023-02-07 20:42  唐什么来着  阅读(156)  评论(0编辑  收藏  举报