• .Net Core3.1 MVC + EF Core+ AutoFac+LayUI+Sqlserver的框架搭建log4net日志


        首先新建一个log4net.config文件,配置如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
        </configSections>
    
        <log4net>
            <!-- 信息日志类 -->
            <logger name="loginfo">
                <level value="ALL" />
                <appender-ref ref="AdoNetAppender" />
                <appender-ref ref="InfoAppender" />
            </logger>
            <!-- 警告日志类 -->
            <logger name="logwarn">
                <level value="ALL" />
                <appender-ref ref="WarnAppender" />
            </logger>
            <!-- 错误日志类-->
            <logger name="logerror">
                <level value="ALL" />
                <appender-ref ref="AdoNetAppender" />
                <appender-ref ref="ErrorAppender" />
            </logger>
            <!-- 异常日志类-->
            <logger name="logfatal">
                <level value="ALL" />
                <appender-ref ref="AdoNetAppender" />
                <appender-ref ref="FatalAppender" />
            </logger>
    
            <!-- 文件保存日志 -->
            <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
                <!--日志路径: 兼容相对路径和绝对路径 -->
                <param name= "File" value= "Log\info\"/>
                <!--是否是向文件中追加日志-->
                <param name= "AppendToFile" value= "true"/>
                <!--保留天数-->
                <param name= "MaxSizeRollBackups" value= "10"/>
                <!--日志文件名是否固定不变的-->
                <param name= "StaticLogFileName" value= "false"/>
                <!--日志文件名格式为:20170112.log-->
                <param name= "DatePattern" value= "yyyyMMdd&quot;.log&quot;"/>
                <!--日志根据日期滚动-->
                <param name= "RollingStyle" value= "Date"/>
                <layout type="log4net.Layout.PatternLayout">
                    <param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-5p %m%n" />
                </layout>
            </appender>
    
            <appender name="WarnAppender" type="log4net.Appender.RollingFileAppender">
                <param name= "File" value= "Log\warn\"/>
                <param name= "AppendToFile" value= "true"/>
                <param name= "MaxSizeRollBackups" value= "10"/>
                <param name= "StaticLogFileName" value= "false"/>
                <param name= "DatePattern" value= "yyyyMMdd&quot;.log&quot;"/>
                <param name= "RollingStyle" value= "Date"/>
                <layout type="log4net.Layout.PatternLayout">
                    <param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-5p %m%n" />
                </layout>
            </appender>
    
            <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
                <param name= "File" value= "Log\error\"/>
                <param name= "AppendToFile" value= "true"/>
                <param name= "MaxSizeRollBackups" value= "10"/>
                <param name= "StaticLogFileName" value= "false"/>
                <param name= "DatePattern" value= "yyyyMMdd&quot;.log&quot;"/>
                <param name= "RollingStyle" value= "Date"/>
                <layout type="log4net.Layout.PatternLayout">
                    <param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-5p %m%n" />
                </layout>
            </appender>
    
            <appender name="FatalAppender" type="log4net.Appender.RollingFileAppender">
                <param name= "File" value= "Log\fatal\"/>
                <param name= "AppendToFile" value= "true"/>
                <param name= "MaxSizeRollBackups" value= "10"/>
                <param name= "StaticLogFileName" value= "false"/>
                <param name= "DatePattern" value= "yyyyMMdd&quot;.log&quot;"/>
                <param name= "RollingStyle" value= "Date"/>
                <layout type="log4net.Layout.PatternLayout">
                    <param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-5p %m%n" />
                </layout>
            </appender>
    
            <!-- 控制台输出日志 -->
            <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
                <mapping>
                    <level value="ERROR" />
                    <foreColor value="Red, HighIntensity" />
                </mapping>
                <mapping>
                    <level value="Info" />
                    <foreColor value="Green" />
                </mapping>
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%n%date{HH:mm:ss.fff} %-5level %m" />
                </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=test_log;integrated security=true" />
                <commandText value="INSERT INTO log([userid],[level],[ip],[msg],[exception]) VALUES (@userid,@level,@ip, @msg,@exception)" />
                <parameter>
                    <parameterName value="@userid"/>
                    <dbType value="Int64"/>
                    <size value="64"/>
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%X{userid}"/>
                    </layout>
                </parameter>
                <parameter>
                    <parameterName value="@level" />
                    <dbType value="String" />
                    <size value="50" />
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%level" />
                    </layout>
                </parameter>
                <parameter>
                    <parameterName value="@ip" />
                    <dbType value="String" />
                    <size value="50" />
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%X{ip}" />
                    </layout>
                </parameter>
                <parameter>
                    <parameterName value="@msg" />
                    <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>
    
        </log4net>
    </configuration>

    新建一个系统日志通用类:LogHelper

    using log4net;
    using log4net.Config;
    using log4net.Repository;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    
    namespace Core.Net.Common.Core.Net.Core.Nlog4
    {
        /// <summary>
        /// 系统日志记录通用类
        /// </summary>
        public class LogHelper
        {
            private const string repositoryName = "NETCoreRepository";
            private const string configFile = "log4net.config";
            private static ILoggerRepository repository { get; set; }
    
            private static readonly ILog _loginfo = LogManager.GetLogger(repositoryName, "loginfo");
            private static readonly ILog _logerror = LogManager.GetLogger(repositoryName, "logerror");
            private static readonly ILog _logwarn = LogManager.GetLogger(repositoryName, "logwarn");
            private static readonly ILog _logfatal = LogManager.GetLogger(repositoryName, "logfatal");
    
            public static void Configure()
            {
                repository = LogManager.CreateRepository(repositoryName);
                XmlConfigurator.Configure(repository, new FileInfo(configFile));
            }
            /// <summary>
            /// 基本信息日志插入
            /// </summary>
            /// <param name="msg"></param>
            public static void Info(string msg)
            {
                _loginfo.Info(msg);
            }
            /// <summary>
            /// 警告信息日志插入
            /// </summary>
            /// <param name="msg"></param>
            public static void Warn(string msg)
            {
                _logwarn.Warn(msg);
            }
            /// <summary>
            /// 错误信息日志插入
            /// </summary>
            /// <param name="msg"></param>
            public static void Error(string msg)
            {
                _logerror.Error(msg);
            }
            /// <summary>
            //异常信息插入
            /// </summary>
            /// <param name="msg"></param>
            public static void Fatal(string msg)
            {
                _logfatal.Fatal(msg);
            }
        }
    }

    在ConfigureServices方法中注册:

     LogHelper.Configure(); //Nlog4日志

    调用:

                LogHelper.Error("11111");
                LogHelper.Info("11111");
                LogHelper.Warn("11111");
                LogHelper.Fatal("111111");

    查看:

    .Net Core
  • 相关阅读:
    memcache详解
    redis详解
    laravel5表单验证
    MySQL添加字段和修改字段
    delete和truncate区别
    IOC 和DI(转载)
    JPA和SpringData知识梳理
    spring和springmvc配置分离
    springboot 整合 mybatis
    mongodb安装及配置
  • 原文地址:https://www.cnblogs.com/zpy1993-09/p/15573340.html
Copyright © 2020-2023  润新知