log4net

using log4net;
using log4net.Appender;
using log4net.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    /// <summary>
    /// the easy pack of log4
    /// </summary>
    public class Log4Pack
    {
        public static readonly object LOCK = new object();
        private static Log4Proxy curLog = null;
        /// <summary>
        /// 获取CommonFileLog的全局静态实例
        /// </summary>
        public static Log4Proxy CurLog
        {
            get
            {
                if (curLog == null)
                {
                    lock (LOCK)
                    {
                        if (curLog == null)
                        {
                            SetCurLog(Path.Combine(DirectoryUtil.GetCurAppDirPath(), "Log"), Level.Info);
                        }
                    }
                }
                return curLog;
            }
        }
        /// <summary>
        /// 设置CommonFileLog的全局静态实例
        /// </summary>
        /// <param name="path">日志目录</param>
        /// <param name="logLevel">日志显示的等级</param>
        public static void SetCurLog(string path, Level logLevel)
        {
            lock (LOCK)
            {
                var cfl = new Log4Pack(path, logLevel);
                curLog = cfl.Logger;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public Log4Pack(string path, Level logLevel)
        {
            DirectoryUtil.CreateOpenDir(path);
            RollingFileAppender rfa = new RollingFileAppender();
            rfa.Name = this.GetHashCode() + "Appender";
            rfa.File = Path.Combine(path, "Log");
            rfa.AppendToFile = true;
            rfa.RollingStyle = RollingFileAppender.RollingMode.Date;
            rfa.LockingModel = new FileAppender.MinimalLock();
            rfa.StaticLogFileName = false;
            rfa.Layout = new log4net.Layout.PatternLayout(
                "%date  %-5level  THD:%t    %message%newline");
            //生成Log文件名格式为:2010-12-06.log
            rfa.DatePattern = "yyyy-MM-dd\".log\"";
            rfa.ImmediateFlush = true;
            rfa.MaxSizeRollBackups = 100;
            rfa.ActivateOptions();
            rfa.Threshold = logLevel;
            log4net.Config.BasicConfigurator.Configure(rfa);
        }

        private Log4Proxy logger = null;
        /// <summary>
        /// 日志对象
        /// </summary>
        public Log4Proxy Logger
        {
            get
            {
                if (logger == null)
                {
                    logger = new Log4Proxy(LogManager.GetLogger(this.GetHashCode().ToString()));
                }
                return logger;
            }
        }

        public class Log4Proxy
        {

            public Log4Proxy(ILog log)
            {
                this.Log = log;
            }

            /// <summary>
            /// 
            /// </summary>
            protected ILog Log { get; set; }

            // 摘要:
            //     Checks if this logger is enabled for the log4net.Core.Level.Debug level.
            //
            // 备注:
            //      This function is intended to lessen the computational cost of disabled log
            //     debug statements.
            //     For some ILog interface log, when you write:
            //     log.Debug("This is entry number: " + i );
            //     You incur the cost constructing the message, string construction and concatenation
            //     in this case, regardless of whether the message is logged or not.
            //     If you are worried about speed (who isn't), then you should write:
            //     if (log.IsDebugEnabled) { log.Debug("This is entry number: " + i ); }
            //     This way you will not incur the cost of parameter construction if debugging
            //     is disabled for log. On the other hand, if the log is debug enabled, you
            //     will incur the cost of evaluating whether the logger is debug enabled twice.
            //     Once in log4net.ILog.IsDebugEnabled and once in the log4net.ILog.Debug(System.Object).
            //     This is an insignificant overhead since evaluating a logger takes about 1%
            //     of the time it takes to actually log. This is the preferred style of logging.
            //     Alternatively if your logger is available statically then the is debug enabled
            //     state can be stored in a static variable like this:
            //     private static readonly bool isDebugEnabled = log.IsDebugEnabled;
            //     Then when you come to log you can write:
            //     if (isDebugEnabled) { log.Debug("This is entry number: " + i ); }
            //     This way the debug enabled state is only queried once when the class is loaded.
            //     Using a private static readonly variable is the most efficient because it
            //     is a run time constant and can be heavily optimized by the JIT compiler.
            //     Of course if you use a static readonly variable to hold the enabled state
            //     of the logger then you cannot change the enabled state at runtime to vary
            //     the logging that is produced. You have to decide if you need absolute speed
            //     or runtime flexibility.
            public bool IsDebugEnabled
            {
                get
                {
                    return this.Log.IsDebugEnabled;
                }
            }
            //
            // 摘要:
            //     Checks if this logger is enabled for the log4net.Core.Level.Error level.
            //
            // 备注:
            //     For more information see log4net.ILog.IsDebugEnabled.
            public bool IsErrorEnabled
            {
                get
                {
                    return this.Log.IsErrorEnabled;
                }
            }
            //
            // 摘要:
            //     Checks if this logger is enabled for the log4net.Core.Level.Fatal level.
            //
            // 备注:
            //     For more information see log4net.ILog.IsDebugEnabled.
            public bool IsFatalEnabled
            {
                get
                {
                    return this.Log.IsFatalEnabled;
                }
            }
            //
            // 摘要:
            //     Checks if this logger is enabled for the log4net.Core.Level.Info level.
            //
            // 备注:
            //     For more information see log4net.ILog.IsDebugEnabled.
            public bool IsInfoEnabled
            {

                get
                {
                    return this.Log.IsInfoEnabled;
                }
            }
            //
            // 摘要:
            //     Checks if this logger is enabled for the log4net.Core.Level.Warn level.
            //
            // 备注:
            //     For more information see log4net.ILog.IsDebugEnabled.
            public bool IsWarnEnabled
            {
                get
                {
                    return this.Log.IsWarnEnabled;
                }
            }
            // 摘要:
            //     Log a message object with the log4net.Core.Level.Debug level.
            //
            // 参数:
            //   message:
            //     The message object to log.
            //
            // 备注:
            //      This method first checks if this logger is DEBUG enabled by comparing the
            //     level of this logger with the log4net.Core.Level.Debug level. If this logger
            //     is DEBUG enabled, then it converts the message object (passed as parameter)
            //     to a string by invoking the appropriate log4net.ObjectRenderer.IObjectRenderer.
            //     It then proceeds to call all the registered appenders in this logger and
            //     also higher in the hierarchy depending on the value of the additivity flag.
            //     WARNING Note that passing an System.Exception to this method will print the
            //     name of the System.Exception but no stack trace. To print a stack trace use
            //     the log4net.ILog.Debug(System.Object,System.Exception) form instead.
            public void Debug(object message)
            {
                this.Log.Debug(message);
            }

            //
            // 摘要:
            //     Log a message object with the log4net.Core.Level.Debug level including the
            //     stack trace of the System.Exception passed as a parameter.
            //
            // 参数:
            //   message:
            //     The message object to log.
            //
            //   exception:
            //     The exception to log, including its stack trace.
            //
            // 备注:
            //      See the log4net.ILog.Debug(System.Object) form for more detailed information.
            public void Debug(object message, Exception exception)
            {
                this.Log.Debug(message, exception);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Debug level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Debug(System.Object,System.Exception)
            //     methods instead.
            public void DebugFormat(string format, object arg0)
            {
                this.Log.DebugFormat(format, arg0);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Debug level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   args:
            //     An Object array containing zero or more objects to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Debug(System.Object,System.Exception)
            //     methods instead.
            public void DebugFormat(string format, params object[] args)
            {
                this.Log.DebugFormat(format, args);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Debug level.
            //
            // 参数:
            //   provider:
            //     An System.IFormatProvider that supplies culture-specific formatting information
            //
            //   format:
            //     A String containing zero or more format items
            //
            //   args:
            //     An Object array containing zero or more objects to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Debug(System.Object,System.Exception)
            //     methods instead.
            public void DebugFormat(IFormatProvider provider, string format, params object[] args)
            {
                this.Log.DebugFormat(provider, format, args);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Debug level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            //   arg1:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Debug(System.Object,System.Exception)
            //     methods instead.
            public void DebugFormat(string format, object arg0, object arg1)
            {
                this.Log.DebugFormat(format, arg0, arg1);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Debug level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            //   arg1:
            //     An Object to format
            //
            //   arg2:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Debug(System.Object,System.Exception)
            //     methods instead.
            public void DebugFormat(string format, object arg0, object arg1, object arg2)
            {
                this.Log.DebugFormat(format, arg0, arg1, arg2);
            }
            //
            // 摘要:
            //     Logs a message object with the log4net.Core.Level.Error level.
            //
            // 参数:
            //   message:
            //     The message object to log.
            //
            // 备注:
            //      This method first checks if this logger is ERROR enabled by comparing the
            //     level of this logger with the log4net.Core.Level.Error level. If this logger
            //     is ERROR enabled, then it converts the message object (passed as parameter)
            //     to a string by invoking the appropriate log4net.ObjectRenderer.IObjectRenderer.
            //     It then proceeds to call all the registered appenders in this logger and
            //     also higher in the hierarchy depending on the value of the additivity flag.
            //     WARNING Note that passing an System.Exception to this method will print the
            //     name of the System.Exception but no stack trace. To print a stack trace use
            //     the log4net.ILog.Error(System.Object,System.Exception) form instead.
            public void Error(object message)
            {
                this.Log.Error(message);
            }
            //
            // 摘要:
            //     Log a message object with the log4net.Core.Level.Error level including the
            //     stack trace of the System.Exception passed as a parameter.
            //
            // 参数:
            //   message:
            //     The message object to log.
            //
            //   exception:
            //     The exception to log, including its stack trace.
            //
            // 备注:
            //      See the log4net.ILog.Error(System.Object) form for more detailed information.
            public void Error(object message, Exception exception)
            {
                this.Log.Error(message, exception);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Error level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Error(System.Object,System.Exception)
            //     methods instead.
            public void ErrorFormat(string format, object arg0)
            {
                this.Log.ErrorFormat(format, arg0);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Error level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   args:
            //     An Object array containing zero or more objects to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Error(System.Object)
            //     methods instead.
            public void ErrorFormat(string format, params object[] args)
            {
                this.Log.ErrorFormat(format, args);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Error level.
            //
            // 参数:
            //   provider:
            //     An System.IFormatProvider that supplies culture-specific formatting information
            //
            //   format:
            //     A String containing zero or more format items
            //
            //   args:
            //     An Object array containing zero or more objects to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Error(System.Object)
            //     methods instead.
            public void ErrorFormat(IFormatProvider provider, string format, params object[] args)
            {
                this.Log.ErrorFormat(provider, format, args);
            }

            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Error level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            //   arg1:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Error(System.Object,System.Exception)
            //     methods instead.
            public void ErrorFormat(string format, object arg0, object arg1)
            {
                this.Log.ErrorFormat(format, arg0, arg1);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Error level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            //   arg1:
            //     An Object to format
            //
            //   arg2:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Error(System.Object,System.Exception)
            //     methods instead.
            public void ErrorFormat(string format, object arg0, object arg1, object arg2)
            {
                this.Log.ErrorFormat(format, arg0, arg1, arg2);
            }
            //
            // 摘要:
            //     Log a message object with the log4net.Core.Level.Fatal level.
            //
            // 参数:
            //   message:
            //     The message object to log.
            //
            // 备注:
            //      This method first checks if this logger is FATAL enabled by comparing the
            //     level of this logger with the log4net.Core.Level.Fatal level. If this logger
            //     is FATAL enabled, then it converts the message object (passed as parameter)
            //     to a string by invoking the appropriate log4net.ObjectRenderer.IObjectRenderer.
            //     It then proceeds to call all the registered appenders in this logger and
            //     also higher in the hierarchy depending on the value of the additivity flag.
            //     WARNING Note that passing an System.Exception to this method will print the
            //     name of the System.Exception but no stack trace. To print a stack trace use
            //     the log4net.ILog.Fatal(System.Object,System.Exception) form instead.
            public void Fatal(object message)
            {
                this.Log.Fatal(message);
            }
            //
            // 摘要:
            //     Log a message object with the log4net.Core.Level.Fatal level including the
            //     stack trace of the System.Exception passed as a parameter.
            //
            // 参数:
            //   message:
            //     The message object to log.
            //
            //   exception:
            //     The exception to log, including its stack trace.
            //
            // 备注:
            //      See the log4net.ILog.Fatal(System.Object) form for more detailed information.
            public void Fatal(object message, Exception exception)
            {
                this.Log.Fatal(message, exception);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Fatal level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Fatal(System.Object,System.Exception)
            //     methods instead.
            public void FatalFormat(string format, object arg0)
            {
                this.Log.FatalFormat(format, arg0);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Fatal level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   args:
            //     An Object array containing zero or more objects to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Fatal(System.Object)
            //     methods instead.
            public void FatalFormat(string format, params object[] args)
            {
                this.Log.FatalFormat(format, args);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Fatal level.
            //
            // 参数:
            //   provider:
            //     An System.IFormatProvider that supplies culture-specific formatting information
            //
            //   format:
            //     A String containing zero or more format items
            //
            //   args:
            //     An Object array containing zero or more objects to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Fatal(System.Object)
            //     methods instead.
            public void FatalFormat(IFormatProvider provider, string format, params object[] args)
            {
                this.Log.FatalFormat(provider, format, args);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Fatal level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            //   arg1:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Fatal(System.Object,System.Exception)
            //     methods instead.
            public void FatalFormat(string format, object arg0, object arg1)
            {
                this.Log.FatalFormat(format, arg0, arg1);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Fatal level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            //   arg1:
            //     An Object to format
            //
            //   arg2:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Fatal(System.Object,System.Exception)
            //     methods instead.
            public void FatalFormat(string format, object arg0, object arg1, object arg2)
            {
                this.Log.FatalFormat(format, arg0, arg1, arg2);
            }
            //
            // 摘要:
            //     Logs a message object with the log4net.Core.Level.Info level.
            //
            // 参数:
            //   message:
            //     The message object to log.
            //
            // 备注:
            //      This method first checks if this logger is INFO enabled by comparing the
            //     level of this logger with the log4net.Core.Level.Info level. If this logger
            //     is INFO enabled, then it converts the message object (passed as parameter)
            //     to a string by invoking the appropriate log4net.ObjectRenderer.IObjectRenderer.
            //     It then proceeds to call all the registered appenders in this logger and
            //     also higher in the hierarchy depending on the value of the additivity flag.
            //     WARNING Note that passing an System.Exception to this method will print the
            //     name of the System.Exception but no stack trace. To print a stack trace use
            //     the log4net.ILog.Info(System.Object,System.Exception) form instead.
            public void Info(object message)
            {
                this.Log.Info(message);
            }
            //
            // 摘要:
            //     Logs a message object with the INFO level including the stack trace of the
            //     System.Exception passed as a parameter.
            //
            // 参数:
            //   message:
            //     The message object to log.
            //
            //   exception:
            //     The exception to log, including its stack trace.
            //
            // 备注:
            //      See the log4net.ILog.Info(System.Object) form for more detailed information.
            public void Info(object message, Exception exception)
            {
                this.Log.Info(message, exception);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Info level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Info(System.Object,System.Exception)
            //     methods instead.
            public void InfoFormat(string format, object arg0)
            {
                this.Log.InfoFormat(format, arg0);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Info level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   args:
            //     An Object array containing zero or more objects to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Info(System.Object)
            //     methods instead.
            public void InfoFormat(string format, params object[] args)
            {
                this.Log.InfoFormat(format, args);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Info level.
            //
            // 参数:
            //   provider:
            //     An System.IFormatProvider that supplies culture-specific formatting information
            //
            //   format:
            //     A String containing zero or more format items
            //
            //   args:
            //     An Object array containing zero or more objects to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Info(System.Object)
            //     methods instead.
            public void InfoFormat(IFormatProvider provider, string format, params object[] args)
            {
                this.Log.InfoFormat(provider, format, args);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Info level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            //   arg1:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Info(System.Object,System.Exception)
            //     methods instead.
            public void InfoFormat(string format, object arg0, object arg1)
            {
                this.Log.InfoFormat(format, arg0, arg1);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Info level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            //   arg1:
            //     An Object to format
            //
            //   arg2:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Info(System.Object,System.Exception)
            //     methods instead.
            public void InfoFormat(string format, object arg0, object arg1, object arg2)
            {
                this.Log.InfoFormat(format, arg0, arg1, arg2);
            }
            //
            // 摘要:
            //     Log a message object with the log4net.Core.Level.Warn level.
            //
            // 参数:
            //   message:
            //     The message object to log.
            //
            // 备注:
            //      This method first checks if this logger is WARN enabled by comparing the
            //     level of this logger with the log4net.Core.Level.Warn level. If this logger
            //     is WARN enabled, then it converts the message object (passed as parameter)
            //     to a string by invoking the appropriate log4net.ObjectRenderer.IObjectRenderer.
            //     It then proceeds to call all the registered appenders in this logger and
            //     also higher in the hierarchy depending on the value of the additivity flag.
            //     WARNING Note that passing an System.Exception to this method will print the
            //     name of the System.Exception but no stack trace. To print a stack trace use
            //     the log4net.ILog.Warn(System.Object,System.Exception) form instead.
            public void Warn(object message)
            {
                this.Log.Warn(message);
            }
            //
            // 摘要:
            //     Log a message object with the log4net.Core.Level.Warn level including the
            //     stack trace of the System.Exception passed as a parameter.
            //
            // 参数:
            //   message:
            //     The message object to log.
            //
            //   exception:
            //     The exception to log, including its stack trace.
            //
            // 备注:
            //      See the log4net.ILog.Warn(System.Object) form for more detailed information.
            public void Warn(object message, Exception exception)
            {
                this.Log.Warn(message, exception);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Warn level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Warn(System.Object,System.Exception)
            //     methods instead.
            public void WarnFormat(string format, object arg0)
            {
                this.Log.WarnFormat(format, arg0);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Warn level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   args:
            //     An Object array containing zero or more objects to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Warn(System.Object)
            //     methods instead.
            public void WarnFormat(string format, params object[] args)
            {
                this.Log.WarnFormat(format, args);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Warn level.
            //
            // 参数:
            //   provider:
            //     An System.IFormatProvider that supplies culture-specific formatting information
            //
            //   format:
            //     A String containing zero or more format items
            //
            //   args:
            //     An Object array containing zero or more objects to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Warn(System.Object)
            //     methods instead.
            public void WarnFormat(IFormatProvider provider, string format, params object[] args)
            {
                this.Log.WarnFormat(provider, format, args);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Warn level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            //   arg1:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Warn(System.Object,System.Exception)
            //     methods instead.
            public void WarnFormat(string format, object arg0, object arg1)
            {
                this.Log.WarnFormat(format, arg0, arg1);
            }
            //
            // 摘要:
            //     Logs a formatted message string with the log4net.Core.Level.Warn level.
            //
            // 参数:
            //   format:
            //     A String containing zero or more format items
            //
            //   arg0:
            //     An Object to format
            //
            //   arg1:
            //     An Object to format
            //
            //   arg2:
            //     An Object to format
            //
            // 备注:
            //      The message is formatted using the String.Format method. See System.String.Format(System.String,System.Object[])
            //     for details of the syntax of the format string and the behavior of the formatting.
            //     This method does not take an System.Exception object to include in the log
            //     event. To pass an System.Exception use one of the log4net.ILog.Warn(System.Object,System.Exception)
            //     methods instead.
            public void WarnFormat(string format, object arg0, object arg1, object arg2)
            {
                this.Log.WarnFormat(format, arg0, arg1, arg2);
            }

        }
    }
}
View Code
using System;
using System.Diagnostics;
using System.IO;
namespace ConsoleApplication1
{
    public class DirectoryUtil
    {
        /// <summary>
        /// 获得当前程序运行的所在文件夹
        /// </summary>
        public static string GetCurAppDirPath()
        {
            if (DotNetUtil.IsHttpRequest())
            {
                return Path.GetFullPath(System.Web.HttpContext.Current.Server.MapPath("~"));
            }
            else
            {
                return Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            }
        }

        /// <summary>
        /// 检查文件夹路径是否存在 如不存在创建它
        /// </summary>
        /// <param name="dirPath">需创建的文件夹路径</param>
        /// <returns>文件夹DirectoryInfo对象</returns>
        public static DirectoryInfo CreateOpenDir(string dirPath)
        {
            if (Directory.Exists(dirPath)) return new DirectoryInfo(dirPath);
            return Directory.CreateDirectory(dirPath);
        }

        /// <summary>
        /// 检查文件夹路径是否存在 如不存在创建它,如果发生异常使用默认路径
        /// </summary>
        /// <param name="dirPath">需创建的文件夹路径</param>
        /// <returns>文件夹DirectoryInfo对象</returns>
        public static DirectoryInfo CreateOpenDir(ref string dirPath, string defPath)
        {
            try
            {
                if (Directory.Exists(dirPath)) return new DirectoryInfo(dirPath);
                return Directory.CreateDirectory(dirPath);
            }
            catch (Exception ex)
            {
                if (string.IsNullOrWhiteSpace(defPath)) throw ex;
                dirPath = defPath.Trim();
                return CreateOpenDir(dirPath);
            }
        }

        /// <summary>
        /// 清楚指定目录下得空目录
        /// </summary>
        /// <param name="dirPath">需要清楚空</param>
        public static void DelEmptyDir(string dirPath)
        {
            string[] childDirArr = Directory.GetDirectories(dirPath);
            foreach (string dir in childDirArr)
            {
                DelEmptyDir(dir);
            }
            string[] chilFileArr = Directory.GetFiles(dirPath);
            //判断子文件夹和子文件是否为空
            if (childDirArr.Length == 0 && chilFileArr.Length == 0)
            {
                try
                {
                    Directory.Delete(dirPath, true);
                }
                catch { }
            }
        }

        /// <summary>
        /// 移动文件夹下所有文件
        /// </summary>
        public static void MoveDirFiles(string sourceDir, string targetDir, bool ignoreException)
        {
            foreach (string file in Directory.GetFiles(sourceDir))
            {
                try
                {
                    string targetFilePath = Path.Combine(targetDir, Path.GetFileName(file));
                    File.Delete(targetFilePath);
                    File.Move(file, targetFilePath);
                }
                catch (Exception ex)
                {
                    if (ignoreException) continue;
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 拷贝文件夹下所有文件
        /// </summary>
        public static void CopyDirFiles(string sourceDir, string targetDir, bool ignoreException)
        {
            foreach (string file in Directory.GetFiles(sourceDir))
            {
                try
                {
                    string targetFilePath = Path.Combine(targetDir, Path.GetFileName(file));
                    File.Copy(file, targetFilePath, true);
                }
                catch (Exception ex)
                {
                    if (ignoreException) continue;
                    throw ex;
                }
            }
        }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    /// <summary>
    /// 辅助通用类
    /// </summary>
    public class DotNetUtil
    {
        /// <summary>
        /// 泛型比较辅助方法 如果T为引用类型Null为最小值
        /// </summary>
        /// <typeparam name="T">比较数类型</typeparam>
        /// <param name="t1">左比较数</param>
        /// <param name="t2">右比较数</param>
        /// <returns>一个值,指示要比较的对象的相对顺序。返回值的含义如下:值含义小于零此实例小于 obj。零此实例等于 obj。大于零此实例大于 obj。</returns>
        public static int Compare<T>(T t1, T t2) where T : IComparable
        {
            if (!typeof(T).IsValueType)
            {
                if (t1 == null && t2 == null) return 0;
                if (t1 == null) return -1;
            }
            return t1.CompareTo(t2);
        }

        /// <summary>
        /// 是否为Http网页请求
        /// </summary>
        /// <returns></returns>
        public static bool IsHttpRequest()
        {
            return System.Web.HttpContext.Current != null;
        }
    }
}
View Code

 

posted @ 2016-12-06 17:44  江境纣州  阅读(20)  评论(0)    收藏  举报