2

我正在尝试为我的团队编写一个简单的 log4net 包装器,但我无法摆脱我遇到的 log4net 配置错误。

有人可以帮助我吗?

包装类

namespace Common
{
    public enum LogType
    {
        Info,Warn,Fatal,Error,Debug
    }

    public class DtntLogger
    {
        public static string LOG4NET_CONFIG_FILE = "log4net.xml";
        public LogType LogType { get; protected set; }

        /******************************************************************************************/

        protected static readonly log4net.ILog FileAppenderLog = log4net.LogManager.GetLogger("LogFileAppender1");
            protected static readonly log4net.ILog Eventlog = log4net.LogManager.GetLogger("EventLogAppener1");

            //protected static readonly log4net.ILog log = log4net.LogManager.GetLogger("LogFileAppender");
            //(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /******************************************************************************************/

        protected static DtntLogger _info;
        public static DtntLogger Info {
            get {
                return getLogger(LogType.Info, _info);
            }
        }

        protected static DtntLogger _error;
        public static DtntLogger Error
        {
            get
            {
                return getLogger(LogType.Error, _error);
            }
        }

        protected static DtntLogger _debug;
        public static DtntLogger Debug
        {
            get
            {
                return getLogger(LogType.Debug, _debug);
            }
        }

        protected static DtntLogger _fatal;
        public static DtntLogger Fatal
        {
            get
            {
                return getLogger(LogType.Fatal, _fatal);
            }
        }

        protected static DtntLogger _warn;

        public static DtntLogger Warn
        {
            get
            {
                return getLogger(LogType.Warn, _warn);
            }
        }


        private static DtntLogger getLogger(LogType type, DtntLogger logger)
        {
            if (logger == null) 
                logger = new DtntLogger(type);
            lock (logger)
            {
                return logger;
            }
        }

        #region CTORS
        protected DtntLogger(LogType type) 
        {   this.LogType = type;
            LoadLoggerConfig();
        }

        private void LoadLoggerConfig()
        {

            FileInfo file = new FileInfo(LOG4NET_CONFIG_FILE);
            if (!file.Exists)
            {
                throw new FileLoadException(string.Format("The configuration file for the logger cannot be found in {0}", file.Directory.FullName), file.FullName);
            }
            log4net.Config.XmlConfigurator.Configure(file);
        }
        #endregion


        public static string EncodeConsole(string message, params object[] args)
        {
            string content = message;
            string pattern = @"{(.*?)}";
            MatchCollection mc = Regex.Matches(content, pattern
                , RegexOptions.Compiled); //compiled is faster

            #region make better Exception that string.format
            bool badStringFormat = false;
            try { string.Format(message, args); }
            catch (FormatException fe)
            {
                badStringFormat = true;
            }
            if (badStringFormat || mc.Count > args.Length)
            {

                string format1 = "you supplied {0} params, but require {1} in the template";
                ///error message
                string err = string.Format(format1, args.Length, "more");
                throw new FormatException(err);
            }
            #endregion

            char delimiter = '~';
            char filler = '#';
            string encoded = message;
            foreach (Match item in mc)
                encoded = encoded.Replace(item.Value + "", delimiter.ToString() + filler.ToString() + delimiter.ToString() + "").Replace(delimiter.ToString() + delimiter.ToString(), delimiter.ToString());

            if (encoded.EndsWith(delimiter.ToString()))
                encoded = encoded.Substring(0, encoded.Length - 1);
            if (encoded.StartsWith(delimiter.ToString())) encoded = encoded.Substring(1);
            var arr = encoded.Split(delimiter);
            string out_ = "";
            int matchnum = 0;

            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i][0] == filler)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.BackgroundColor = ConsoleColor.Blue;
                    var oldmatch = mc[matchnum].Value;
                    var g = oldmatch.Split(':');
                    var clear = g[0].Replace("{", "").Replace("}", "");
                    int value = Int32.Parse(clear);
                    //will always be 0 for data
                    string match = "{0}";
                    if (g.Length > 1)
                        match = "{0:" + g[1];
                    string args_matchnum = args[value] + "";
                    var data = string.Format(match, args[value]);
                    arr[i] = data;
                    matchnum++;
                }
                else
                    Console.ResetColor();

                string arr_i = arr[i].ToString();
                Console.Write(arr[i]);
                out_ += arr[i];

            }
            Console.WriteLine("");

            return out_;
        }

        public string Write(string message, params object[] args)
        {
            string s = Write(FileAppenderLog, message, args);
            return s;
        }

        protected string Write(log4net.ILog log, string message, params object[] args)
        {
            var newmessage = message;

            string format = "{2} - {1} | {0}";
            string s1 = string.Format(format, 
                message, 
                DateTime.Now.ToString("T"),
                Thread.CurrentThread.Name
                );

            string out_ = string.Format(message, args);
            logWrite(log, out_);
            logWrite(Eventlog, out_);
            EncodeConsole(s1, args);
            return out_;

        }

        private void logWrite(ILog log, string message)
        {
            if (!log.Logger.Repository.Configured)
                LoadLoggerConfig();

            switch (this.LogType)
            {
                case LogType.Info: 
                    if (log.IsInfoEnabled) 
                        log.Info(message);
                    break;
                case LogType.Warn: 
                    if (log.IsWarnEnabled) 
                        log.Warn(message);
                    break;
                case LogType.Fatal: 
                    if (log.IsFatalEnabled) 
                        log.Fatal(message);
                    break;
                case LogType.Error: 
                    if (log.IsErrorEnabled) 
                        log.Error(message);
                    break;
                case LogType.Debug:
                    if (log.IsDebugEnabled)
                        log.Debug(message);
                    break;
                default: 
                    throw new InvalidOperationException("LogType is an Enum, what are you doing ???");

            }   
        }
    }
}

XML 配置文件 log4net.xml

< ?xml version="1.0" encoding="utf-8" ?>
    <log4net>
     <root>
      <level value="all" />
        <!--
        <appender-ref ref="EventLogAppender" /> 
        <appender-ref ref="LogFileAppender" />
        -->

     </root>

  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <File value="MyApp.log" />
    <AppendToFile value="true" />

    <staticLogFileName value="true" />
    <maximumFileSize value="1000MB" />
    <rollingStyle value="Date" />
    <datePattern value="yyyMMdd"/>
    <maxSizeRollBackups value="10" />

    <threshold value="WARN" />

    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%-5p%t%d{yyyy-MM-dd HH:mm:ss} – %m%n" />
    </layout>

    <evaluator type="log4net.Core.LevelEvaluator">
      <threshold value="WARN" />
    </evaluator>
  </appender>
  <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
    <applicationName value="MyApp" />
    <threshold value="WARN" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>
     <logger name="EventLogAppender1">
       <level value="ALL" />
      <param value="All" name="Threshold" />

    <threshold value="ALL" />
        <appender-ref ref="EventLogAppender" />
    </logger>
     <logger name="LogFileAppender1">
       <level value="ALL" />
       <maximumfilesize value="1000MB" />
       <param value="All" name="Threshold" />
      <appender-ref ref="LogFileAppender" />
     </logger>
    </log4net>

Log4NET 调试数据(控制台输出)

log4net: log4net assembly [log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a]. Loaded from [C:\XXXYYYXXX\TestResults\DEV1\Out\log4net.dll]. (.NET Runtime [4.0.30319.261] on Microsoft Windows NT 6.1.7601 Service Pack 1)
log4net: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository for assembly [DtntLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net: Assembly [DtntLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\XXXYYYXXX\TestResults\DEV1\Out\DtntLogger.dll]
log4net: Assembly [DtntLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: Assembly [DtntLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
log4net: configuring repository [log4net-default-repository] using file [log4net.xml]
log4net: configuring repository [log4net-default-repository] using stream
log4net: loading XML configuration
log4net: Configuring Repository [log4net-default-repository]
log4net: Configuration update mode [Merge].
log4net: Logger [root] Level string is [all].
log4net: Logger [root] level set to [name="ALL",value=-2147483648].
log4net: Retrieving an instance of log4net.Repository.Logger for logger [EventLogAppender1].
log4net: Setting [EventLogAppender1] additivity to [True].
log4net: Logger [EventLogAppender1] Level string is [ALL].
log4net: Logger [EventLogAppender1] level set to [name="ALL",value=-2147483648].
log4net: Loading Appender [EventLogAppender] type: [log4net.Appender.EventLogAppender]
log4net: Setting Property [ApplicationName] to String value [AppApp]
log4net: Setting Property [Threshold] to Level value [WARN]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [ConversionPattern] to String value [%date [%thread] %-5level %logger [%property{NDC}] - %message%newline]
log4net: Converter [date] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [thread] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [] ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [level] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [logger] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [property] Option [NDC] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [] - ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: Source [AppApp] is registered to log []
log4net: reated Appender [EventLogAppender]
log4net: Adding appender named [EventLogAppender] to logger [EventLogAppender1].
log4net: Retrieving an instance of log4net.Repository.Logger for logger [LogFileAppender1].
log4net: Setting [LogFileAppender1] additivity to [True].
log4net: Logger [LogFileAppender1] Level string is [ALL].
log4net: Logger [LogFileAppender1] level set to [name="ALL",value=-2147483648].
log4net: Loading Appender [LogFileAppender] type: [log4net.Appender.RollingFileAppender]
log4net: Setting Property [File] to String value [App.log]
log4net: Setting Property [AppendToFile] to Boolean value [True]
log4net: Setting Property [StaticLogFileName] to Boolean value [True]
log4net: Setting Property [MaximumFileSize] to String value [1000MB]
log4net: Setting Property [RollingStyle] to RollingMode value [Date]
log4net: Setting Property [DatePattern] to String value [yyyMMdd]
log4net: Setting Property [MaxSizeRollBackups] to Int32 value [10]
log4net: Setting Property [Threshold] to Level value [WARN]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [ConversionPattern] to String value [%-5p%t%d{yyyy-MM-dd HH:mm:ss} – %m%n]
log4net: Converter [p] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: Converter [t] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [d] Option [yyyy-MM-dd HH:mm:ss] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ – ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [m] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [n] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: Type = [0], r0 = [19700101], r1 = [19700101]
log4net: Type = [1], r0 = [19700101], r1 = [19700101]
log4net: Type = [2], r0 = [19700101], r1 = [19700101]
log4net: Type = [3], r0 = [19700101], r1 = [19700102]
log4net: Searched for existing files in [C:\XXXYYYXXX\TestResults\DEV1\Out]
log4net: curSizeRollBackups starts at [0]
log4net: Opening file for writing [C:\XXXYYYXXX\TestResults\DEV1\Out\App.log] append [True]
log4net: reated Appender [LogFileAppender]
log4net: Adding appender named [LogFileAppender] to logger [LogFileAppender1].
log4net: Hierarchy Threshold []
log4net: No appenders could be found for logger [EventLogAppener1] repository [log4net-default-repository]
log4net: Please initialize the log4net system properly.
log4net:     Current AppDomain context information: 
log4net:        BaseDirectory   : C:\XXXYYYXXX\TestResults\DEV1\Out
log4net:        FriendlyName    : TestAppDomain: 5dfd9ec0-bf14-4d65-9b74-b78d054c9ca0
log4net:        DynamicDirectory: 
Agent: adapter run thread for test 'TestWrite' with id '4f072f2c-d52e-466c-bf4d-87b1a0d3cc40' - 13:02:58 | debugging

Log4NET 调试数据(标准错误输出)

log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [Threshold] to set object on [log4net.Repository.Hierarchy.DefaultLoggerFactory+LoggerImpl]
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [threshold] to set object on [log4net.Repository.Hierarchy.DefaultLoggerFactory+LoggerImpl]
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [maximumfilesize] to set object on [log4net.Repository.Hierarchy.DefaultLoggerFactory+LoggerImpl]
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [Threshold] to set object on [log4net.Repository.Hierarchy.DefaultLoggerFactory+LoggerImpl]
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [evaluator] to set object on [log4net.Appender.RollingFileAppender]

调试跟踪

log4net: log4net assembly [log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a]. Loaded from [C:\XXXYYYXXX\TestResults\DEV1\Out\log4net.dll]. (.NET Runtime [4.0.30319.261] on Microsoft Windows NT 6.1.7601 Service Pack 1)
log4net: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository for assembly [DtntLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net: Assembly [DtntLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\XXXYYYXXX\TestResults\DEV1\Out\DtntLogger.dll]
log4net: Assembly [DtntLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: Assembly [DtntLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
log4net: configuring repository [log4net-default-repository] using file [log4net.xml]
log4net: configuring repository [log4net-default-repository] using stream
log4net: loading XML configuration
log4net: Configuring Repository [log4net-default-repository]
log4net: Configuration update mode [Merge].
log4net: Logger [root] Level string is [all].
log4net: Logger [root] level set to [name="ALL",value=-2147483648].
log4net: Retrieving an instance of log4net.Repository.Logger for logger [EventLogAppender1].
log4net: Setting [EventLogAppender1] additivity to [True].
log4net: Logger [EventLogAppender1] Level string is [ALL].
log4net: Logger [EventLogAppender1] level set to [name="ALL",value=-2147483648].
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [Threshold] to set object on [log4net.Repository.Hierarchy.DefaultLoggerFactory+LoggerImpl]
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [threshold] to set object on [log4net.Repository.Hierarchy.DefaultLoggerFactory+LoggerImpl]
log4net: Loading Appender [EventLogAppender] type: [log4net.Appender.EventLogAppender]
log4net: Setting Property [ApplicationName] to String value [AppApp]
log4net: Setting Property [Threshold] to Level value [WARN]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [ConversionPattern] to String value [%date [%thread] %-5level %logger [%property{NDC}] - %message%newline]
log4net: Converter [date] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [thread] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [] ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [level] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [logger] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [property] Option [NDC] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [] - ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: Source [AppApp] is registered to log []
log4net: reated Appender [EventLogAppender]
log4net: Adding appender named [EventLogAppender] to logger [EventLogAppender1].
log4net: Retrieving an instance of log4net.Repository.Logger for logger [LogFileAppender1].
log4net: Setting [LogFileAppender1] additivity to [True].
log4net: Logger [LogFileAppender1] Level string is [ALL].
log4net: Logger [LogFileAppender1] level set to [name="ALL",value=-2147483648].
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [maximumfilesize] to set object on [log4net.Repository.Hierarchy.DefaultLoggerFactory+LoggerImpl]
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [Threshold] to set object on [log4net.Repository.Hierarchy.DefaultLoggerFactory+LoggerImpl]
log4net: Loading Appender [LogFileAppender] type: [log4net.Appender.RollingFileAppender]
log4net: Setting Property [File] to String value [App.log]
log4net: Setting Property [AppendToFile] to Boolean value [True]
log4net: Setting Property [StaticLogFileName] to Boolean value [True]
log4net: Setting Property [MaximumFileSize] to String value [1000MB]
log4net: Setting Property [RollingStyle] to RollingMode value [Date]
log4net: Setting Property [DatePattern] to String value [yyyMMdd]
log4net: Setting Property [MaxSizeRollBackups] to Int32 value [10]
log4net: Setting Property [Threshold] to Level value [WARN]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [ConversionPattern] to String value [%-5p%t%d{yyyy-MM-dd HH:mm:ss} – %m%n]
log4net: Converter [p] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: Converter [t] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [d] Option [yyyy-MM-dd HH:mm:ss] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ – ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [m] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [n] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [evaluator] to set object on [log4net.Appender.RollingFileAppender]
log4net: Type = [0], r0 = [19700101], r1 = [19700101]
log4net: Type = [1], r0 = [19700101], r1 = [19700101]
log4net: Type = [2], r0 = [19700101], r1 = [19700101]
log4net: Type = [3], r0 = [19700101], r1 = [19700102]
log4net: Searched for existing files in [C:\XXXYYYXXX\TestResults\DEV1\Out]
log4net: curSizeRollBackups starts at [0]
log4net: Opening file for writing [C:\XXXYYYXXX\TestResults\DEV1\Out\App.log] append [True]
log4net: reated Appender [LogFileAppender]
log4net: Adding appender named [LogFileAppender] to logger [LogFileAppender1].
log4net: Hierarchy Threshold []
log4net: No appenders could be found for logger [EventLogAppener1] repository [log4net-default-repository]
log4net: Please initialize the log4net system properly.
log4net:     Current AppDomain context information: 
log4net:        BaseDirectory   : C:\XXXYYYXXX\TestResults\DEV1\Out
log4net:        FriendlyName    : TestAppDomain: 5dfd9ec0-bf14-4d65-9b74-b78d054c9ca0
log4net:        DynamicDirectory:
4

1 回答 1

1

在我的博客http://mperlstein.blogspot.com/2012/04/log4net-configuration-trouble.html中回答了这个问题

要点是 log4net 使用反射来更新属性,并且阈值不作为相关对象的可写属性/属性存在。

再也不

于 2012-04-03T08:57:38.587 回答