类库封装log4net

1、类库nuget添加log4net应用

2、创建log4net.xml文件,并设置文件属性【复制到输出目录:始终复制】。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="log4net" allowLocation="true" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="RollingLogFileAppender" />
        </root>
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
            <param name="File" value="log/log-"/>
            <param name="AppendToFile" value="true"/>
            <param name="MaxSizeRollBackups" value="10"/>
            <param name="MaximumFileSize" value="10MB"/>
            <param name="StaticLogFileName" value="false"/>
            <param name="DatePattern" value="yyyyMMdd" log=""/>
            <param name="RollingStyle" value="Date"/>
            <layout type="log4net.Layout.PatternLayout">
                <param name="ConversionPattern" value="%d  [%t]  %-5p  %c  [%x]  -  %m%n"/>
            </layout>
        </appender>
    </log4net>
</configuration>

3、类库AssemblyInfo.cs文件,添加:[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.xml", Watch = true)]

4、创建帮助类。

public class LogHelper
    {
        private static readonly ILog Log= LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        //static LogHelper()
        //{
        //    Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        //}

        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="ex">信息</param>
        public static void Debug(object message)
        {
            Log.Debug(message);
        }

        /// <summary>
        /// 记录警告信息
        /// </summary>
        /// <param name="ex">信息</param>
        public static void Warn(object message)
        {
            Log.Warn(message);
        }

        /// <summary>
        /// 记录错误信息
        /// </summary>
        /// <param name="ex">信息</param>
        public static void Error(object message)
        {
            Log.Error(message);
        }

        /// <summary>
        /// 记录重要提示信息
        /// </summary>
        /// <param name="ex">信息</param>
        public static void Info(object message)
        {
            Log.Info(message);
        }

        /// <summary>
        /// 记录信息和异常信息
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <param name="ex">异常对象</param>
        public static void Debug(object message, Exception ex)
        {
            Log.Debug(message, ex);
        }

        /// <summary>
        /// 记录信息和异常信息
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <param name="ex">异常对象</param>
        public static void Warn(object message, Exception ex)
        {
            Log.Warn(message, ex);
        }

        /// <summary>
        /// 记录信息和异常信息
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <param name="ex">异常对象</param>
        public static void Error(object message, Exception ex)
        {
            Log.Error(message, ex);
        }

        /// <summary>
        /// 记录信息和异常信息
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <param name="ex">异常对象</param>
        public static void Info(object message, Exception ex)
        {
            Log.Info(message, ex);
        }

    }

 

posted @ 2021-04-15 16:20  随学·笔记  阅读(228)  评论(0编辑  收藏  举报