zoukankan      html  css  js  c++  java
  • 类库封装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);
            }
    
        }
    请随手写下你的想法!!!
  • 相关阅读:
    webservice 测试窗体只能用于来自本地计算机的请求
    Derby 数据库 客户端 ij使用
    Liunx 命令大全
    Linux 日志命令
    Git with SVN
    Git 重写历史 filter-branch
    Git you are not allowed to push code to protected branches on this project?
    sqlldr 用法
    hibernate_sequence.nextval 序列不存在
    redis持久化方案
  • 原文地址:https://www.cnblogs.com/flywing/p/14663018.html
Copyright © 2011-2022 走看看