• 类库里面添加日志记录 log4net


    第一步:

    新建一个公共类库common,添加CustomLog4jLogger.cs 并引用log4net.dll

    /// <summary>
        /// 日志记录
        /// </summary>
        public class CustomLog4jLogger
        {
            /// <summary>
            /// 日志
            /// </summary>
            public static ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    
            /// <summary>
            /// 日志
            /// </summary>
            /// <param name="funcstr">方法名称</param>
            /// <param name="message">日志描述</param>
            /// <param name="args">方法参数</param>
            /// <param name="type">类别,1:错误日志,2:操作日志</param>
            private static void Log(string apikey, string funcstr, string message, string args, int type)
            {
                switch (type)
                {
                    case 1:
                        Logger.Error(string.Format("Func:{0} | Error:{1} | Args:{2}", funcstr, message, args));
                        break;
                    case 2:
                        Logger.Info(string.Format("Key:{0} | Func:{1} | Message:{2}", apikey, funcstr, message));
                        break;
                    case 3:
                        Logger.Warn(message);
                        break;
                }
            }
            /// <summary>
            /// 错误日志
            /// </summary>
            /// <param name="funcstr">方法名称</param>
            /// <param name="message">错误信息</param>
            /// <param name="args">方法参数</param>
            public static void LogDebug(string funcstr, string message, string args)
            {
                Log("", funcstr, message, args, 1);
            }
            /// <summary>
            /// 错误日志
            /// </summary>
            /// <param name="funcstr">方法名称</param>
            /// <param name="message">错误信息</param>
            public static void LogDebug(string funcstr, string message)
            {
                LogDebug(funcstr, message, "");
            }
            /// <summary>
            /// 错误日志
            /// </summary>
            /// <param name="message">错误信息</param>
            public static void LogDebug(string message)
            {
                LogDebug("", message);
            }
    
            /// <summary>
            /// 运行日志
            /// </summary>
            /// <param name="message">运行信息</param>
            public static void LogInfo(string message)
            {
                LogInfo("", message);
            }
            /// <summary>
            /// 运行日志
            /// </summary>
            /// <param name="funcstr">方法名称</param>
            /// <param name="message">运行信息</param>
            /// <param name="args">方法参数</param>
            public static void LogInfo(string funcstr, string message)
            {
                LogInfo("", funcstr, message);
            }
            /// <summary>
            /// 运行日志
            /// </summary>
            /// <param name="apikey">应用的apikey</param>
            /// <param name="funcstr">方法名称</param>
            /// <param name="message">运行信息</param>
            /// <param name="args">方法参数</param>
            public static void LogInfo(string apikey, string funcstr, string message)
            {
                Log(apikey, funcstr, message, "", 2);
            }
    
            public static void LogEvent(string message)
            {
                Log("", "", message, "", 3);
            }
        }
    

      第二步:

    在需要记录日志的类库里,引用common类库,调用:

    CustomLog4jLogger.LogInfo("xxxxxx");
    第三步:
    在调用类库的程序里添加log4net.config(在附件里)并引用log4net.dll
    程序调用:
      static void Main(string[] args)
            {
                try
                {
                    log4net.Config.XmlConfigurator.Configure(new FileInfo(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "log4net.config")));
    }
    }
    

      

    附件
  • 相关阅读:
    CentOS 下安装apt-get
    Centos 6.5升级到Git2.1.2的步骤
    JAVA常识积累
    Android网络编程概述
    解读ClassLoader
    android ndk调用OpenGL 实现纹理贴图Texture
    android Bind机制(二)
    Android Binder机制原理(史上最强理解,没有之一)(转)
    NDK常见问题
    根因分析初探:一种报警聚类算法在业务系统的落地实施
  • 原文地址:https://www.cnblogs.com/yhdkzy/p/3981514.html
Copyright © 2020-2023  润新知