Exemplo n.º 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);
            }
        }
Exemplo n.º 2
0
Arquivo: L.cs Projeto: 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);
            }
        }