示例#1
0
        /// <summary>
        /// Formats the given information and logs it.
        /// </summary>
        /// <param name="label">Label to use when logging</param>
        /// <param name="content">A string with a message or an object to call ToString() on it</param>
        public void Log(string label, object content)
        {
            if (label == null)
            {
                throw new ArgumentNullException("label");
            }

            if (content == null)
            {
                throw new ArgumentNullException("content");
            }

            label = Normalize(label);

            if (_enabledLabels.Any() && !_enabledLabels.Contains(label))
            {
                return;
            }

            _longestLabel = Math.Max(_longestLabel, label.Length);

            var date          = Now;
            var formattedDate = date.ToString(_dateTimeFormat, CultureInfo.InvariantCulture);
            var padding       = new string(' ', _longestLabel - label.Length);

            var line = $"{formattedDate} {label} {padding}{content}";

            if (_printToStandardOut)
            {
                Console.WriteLine(line);
            }

            lock (_lock)
            {
                if (_disposed)
                {
                    throw new ObjectDisposedException("Cannot access a disposed object.");
                }

                _openStreams.Append(date, line);
            }
        }
示例#2
0
文件: L.cs 项目: llenroc/net-L
        /// <summary>
        /// Formats the given information and logs it.
        /// </summary>
        /// <param name="label">Label to use when logging</param>
        /// <param name="content">A string with a message or an object to call ToString() on it</param>
        public void Log(string label, object content)
        {
            if (label == null)
            {
                throw new ArgumentNullException("label");
            }

            if (content == null)
            {
                throw new ArgumentNullException("content");
            }

            label = _sanitizeLabel(label);

            if (_configuration.EnabledLabels.Any() && !_configuration.EnabledLabels.Contains(label))
            {
                return;
            }

            _longestLabel = Math.Max(_longestLabel, label.Length);

            var date          = Now;
            var formattedDate = date.ToString(_configuration.DateTimeFormat, CultureInfo.InvariantCulture);
            var padding       = new string(' ', _longestLabel - label.Length);

            var line = string.Format(CultureInfo.InvariantCulture, "{0} {1} {2}{3}", formattedDate, label, padding,
                                     content);

            lock (_lock)
            {
                if (_disposed)
                {
                    throw ObjectDisposedException;
                }

                _openStreams.Append(date, line);
            }
        }