• 二、.net6使用Log4Net


    --Nuget引入 :
    1. Log4Net 

    2. Microsoft.Extensions.Logging.Log4Net.AspNetCore 

    --注册:

     builder.Logging.AddLog4Net("CfgFile/log4net.Config");

    --Log4Net日志级别

    日志级别:控制级别,由低到高:ALL-DEBUG-INFO-WARN-ERROR-FATAL-OFF

    --log4net.Config 配置文件

    <?xml version="1.0" encoding="utf-8"?>
    <log4net>
        <!--指定日记记录方式,以滚动文件的方式(文件记录)-->
        <appender name="rollingAppender" type="log4net.Appender.RollingFileAppender">
            <!--日志路径-->
            <file value="log\log.txt" />
            <!--追加日志内容-->
            <appendToFile value="true" />
    
            <!--防止多线程时不能写Log,官方说线程非安全-->
            <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    
            <!--可以为:Once|Size|Date|Composite-->
            <!--Composite为Size和Date的组合-->
            <rollingStyle value="Composite" />
    
            <!--当备份文件时,为文件名加的后缀-->
            <datePattern value="yyyyMMdd.TXT" />
    
            <!--log保留天数-->
            <maxSizeRollBackups value="20" />
    
            <!--每个文件最大3M 可用的单位:KB|MB|GB-->
            <maximumFileSize value="3MB" />
    
            <!--日志文件名是否是固定不变的-->
            <staticLogFileName value="true" />
            <!--日志根据日期滚动-->
            <param name= "RollingStyle" value= "Date"/>
    
            <!--输出级别在INFO和ERROR之间的日志-->
            <filter type="log4net.Filter.LevelRangeFilter">
                <param name="LevelMin" value="ALL" />
                <param name="LevelMax" value="FATAL" />
            </filter>
            <!--布局-->
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
            </layout>
        </appender>
    
        <!--发邮件-->
        <appender name="LogSmtpAppender" type="log4net.Appender.SmtpAppender">
            <authentication value="Basic" />
            <!--接受邮件的邮箱-->
            <to value="7778888@qq.com" />
            <!--发送邮件的邮箱-->
            <from value="123456@126.com" />
            <!--发送邮件的邮箱-->
            <username value="123456@126.com" />
            <!--发送邮件的邮箱密码-->
            <password value="123456" />
            <!--标题-->
            <subject value="XXX应用错误日志消息" />
            <!--smtp.qq.com-->
            <smtpHost value="smtp.126.com" />
            <bufferSize value="2048" />
            <!--超长部分是否丢弃-->
            <lossy value="true" />
            <!--日志级别 大于 WARN 的才发邮件-->
            <evaluator type="log4net.Core.LevelEvaluator">
                <threshold value="ERROR"/>
            </evaluator>
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value=" %level :: %message %newlineLogger: %logger%newline Date: %date%newline" />
            </layout>
        </appender>
        
        <!-- 控制台日志 -->
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%n%d{yyyy-MM-dd HH:mm:ss} %m%n" />
            </layout>
            <!--指定范围内的等级才可以被记录-->
            <!--<filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="Info" />
        <param name="LevelMax" value="Fatal" />
       </filter>-->
        </appender >
    
        <!--数据库日记-->
        <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
            <bufferSize value="1" />
            <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <connectionString value="data source=.;initial catalog=TextDemo;integrated security=false;persist security info=True;User ID=sa;Password=123456" />
            <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
            <parameter>
                <parameterName value="@log_date" />
                <dbType value="DateTime" />
                <layout type="log4net.Layout.RawTimeStampLayout" />
            </parameter>
            <parameter>
                <parameterName value="@thread" />
                <dbType value="String" />
                <size value="255" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%thread" />
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@log_level" />
                <dbType value="String" />
                <size value="50" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%level" />
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@logger" />
                <dbType value="String" />
                <size value="255" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%logger" />
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@message" />
                <dbType value="String" />
                <size value="4000" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%message" />
                </layout>
            </parameter>
            <parameter>
                <parameterName value="@exception" />
                <dbType value="String" />
                <size value="2000" />
                <layout type="log4net.Layout.ExceptionLayout" />
            </parameter>
        </appender>
    
        <root>
            <priority value="ALL"/>
            <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
            <!--限制日志记录级别-->
            <level value="ALL"/>
            <appender-ref ref="rollingAppender" />
            <appender-ref ref="AdoNetAppender" />
        </root>
    
    
    </log4net>
    View Code

    --打印日志调用

    using Microsoft.AspNetCore.Mvc;
    
    namespace Advanced.NET6.Project.Controllers
    {
        public class SecondController : Controller
        {
            private readonly ILogger<SecondController> _logger;
            private readonly ILoggerFactory _loggerFactory;
    
    
            public SecondController(ILogger<SecondController> logger, ILoggerFactory loggerFactory)
            {
                //方式一
                this._logger = logger;
                this._logger.LogInformation($"{this.GetType().Name}被_logger构造了。。。");
    
                //方式二
                this._loggerFactory = loggerFactory;
                ILogger<SecondController> logger2 = this._loggerFactory.CreateLogger<SecondController>();
                logger2.LogInformation($"{this.GetType().Name}被logger2构造了。。。");
            }
    
            public IActionResult Index()
            {
                //方式三
                ILogger<SecondController> logger3 = this._loggerFactory.CreateLogger<SecondController>();
                logger3.LogInformation($"{this.GetType().Name}被logger3执行了。。。");
    
                this._logger.LogInformation("Index 被执行了。。。");
                return View();
            }
        }
    }
    

      

  • 相关阅读:
    Sqlserver日期函数应用
    SSRS匿名访问
    SSAS动态添加分区(一)
    BI就是报表?
    CreateEvent函数/多线程/c++
    字符编码介绍
    Win7 64下Visual C++ 6.0不兼容
    Winpcap安装,Cannot open include file 'pcap.h'
    PPT开发 * .pps 文件类型
    Visual Assist X 工具栏不显示 toolbar
  • 原文地址:https://www.cnblogs.com/wangshunyun/p/16326697.html
Copyright © 2020-2023  润新知