Example #1
0
        /// <summary>
        /// Reads the log configuration from the given <paramref name="fileContent"/>.
        /// If the <paramref name="fileContent"/> is NULL or empty, a default configuration
        /// is returned.
        /// </summary>
        public static LogConfiguration ReadText(string fileContent)
        {
            LogConfiguration fileConfig = new LogConfiguration();

            // No content, no configuration
            if (string.IsNullOrEmpty(fileContent))
            {
                return(fileConfig);
            }

            Dictionary <string, ILogStream> streamSet = new Dictionary <string, ILogStream>(10);

            // Parse lines
            string[] lines = fileContent.Split('\n');
            for (int i = -1; ++i != lines.Length;)
            {
                string line = lines[i];
                if (string.IsNullOrEmpty(line))
                {
                    continue;                             // Empty line
                }
                if (line[0] == '#')
                {
                    continue;                 // Comment line
                }
                string[] linePair = line.Split('=');
                if (linePair.Length != 2)
                {
                    continue;                       // Invalid format
                }
                char[] bias  = { ' ', '\t', '\r' };
                string key   = linePair[0].Trim(bias).ToLower();
                string value = linePair[1].Trim(bias);

                if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
                {
                    continue;
                }

                // Reserved word
                if (key == "level")
                {
                    ELogLevel.TryParse(value, out ELogLevel level);
                    fileConfig.SetLevel(level);
                    continue;
                }

                if (key == "pattern")
                {
                    fileConfig.SetPattern(value);
                    continue;
                }

                if (key == "streams")
                {
                    string[] streamNames = value.Split(',');
                    for (int j = -1; ++j != streamNames.Length;)
                    {
                        string streamName = streamNames[j].Trim();
                        if (string.IsNullOrEmpty(streamName))
                        {
                            continue;
                        }
                        streamSet[streamName] = null;
                    }
                    continue;
                }

                // Dynamic content
                int        psIdx = key.IndexOf('.');
                string     streamId;
                string     streamProp;
                ILogStream logStream;

                // No dot, no stream property. But the stream kind.
                if (psIdx == -1)
                {
                    streamId            = key;
                    logStream           = StreamStore.GetStreamInstance(value);
                    streamSet[streamId] = logStream;
                    fileConfig.AddStream(logStream);
                    continue;
                }

                streamId   = key.Substring(0, psIdx);
                streamProp = key.Substring(psIdx + 1);
                if (!streamSet.TryGetValue(streamId, out logStream))
                {
                    continue;
                }
                SetStreamProperty(logStream, streamProp, value);
            }

            return(fileConfig);
        }
Example #2
0
 /// <summary>
 /// Returns TRUE if the given <paramref name="level"/> is greater or equal
 /// as the current.
 /// </summary>
 public bool IsGreaterOrEqual(ELogLevel level)
 {
     return(level._severity >= _severity);
 }