Пример #1
0
        /// <summary>
        /// Processes that a new log writer tag was added to the logging subsystem.
        /// </summary>
        /// <param name="tag">the new log writer tag.</param>
        internal void ProcessLogWriterTagAdded(LogWriterTag tag)
        {
            Debug.Assert(Monitor.IsEntered(Log.Sync));

            lock (Sync)
            {
                // call OnLogWriterTagAdded() of this stage
                try
                {
                    OnLogWriterTagAdded(tag);
                }
                catch (Exception ex)
                {
                    // swallow exception to avoid crashing the application, if the exception is not handled properly
                    Debug.Fail("The pipeline stage threw an exception processing a notification about a new log writer tag", ex.ToString());
                }

                // call OnLogWriterTagAdded() of following stages
                for (int i = 0; i < mNextStages.Length; i++)
                {
                    try
                    {
                        mNextStages[i].ProcessLogWriterTagAdded(tag);
                    }
                    catch (Exception ex)
                    {
                        // swallow exception to avoid crashing the application, if the exception is not handled properly
                        Debug.Fail("A following pipeline stage threw an exception processing a notification about a new log writer tag", ex.ToString());
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Removes the specified tags from the set returning a new set.
        /// </summary>
        /// <param name="left">The set to remove the specified tags from.</param>
        /// <param name="right">Tags to remove from the set.</param>
        /// <returns>The resulting set.</returns>
        public static TagSet operator -(TagSet left, IEnumerable <string> right)
        {
            if (left == null)
            {
                throw new ArgumentNullException(nameof(left));
            }
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }
            foreach (string tag in right)
            {
                LogWriterTag.CheckTag(tag);
            }

            var newSet = new HashSet <string>(left, StringComparer.Ordinal);

            newSet.ExceptWith(right);
            if (newSet.Count == left.Count)
            {
                return(left);
            }
            var tags = new List <string>(newSet);

            return(new TagSet(tags));
        }
Пример #3
0
        /// <summary>
        /// Adds the specified tag to the set returning a new set.
        /// </summary>
        /// <param name="left">The set to add the specified tag to.</param>
        /// <param name="right">Tag to add to the set.</param>
        /// <returns>The resulting set.</returns>
        public static TagSet operator +(TagSet left, string right)
        {
            if (left == null)
            {
                throw new ArgumentNullException(nameof(left));
            }
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }
            LogWriterTag.CheckTag(right);

            var newSet = new HashSet <string>(left, StringComparer.Ordinal)
            {
                right
            };

            if (newSet.Count == left.Count)
            {
                return(left);
            }
            var tags = new List <string>(newSet);

            return(new TagSet(tags));
        }
        public void CheckTag_Invalid()
        {
            var builder = new StringBuilder();
            var random  = new Random(0);

            for (int sample = 0; sample < 10000; sample++)
            {
                builder.Clear();

                // generate a valid tag
                int length = random.Next(1, 50);
                for (int i = 0; i < length; i++)
                {
                    int j = random.Next(0, ValidCharSet.Length - 1);
                    builder.Append(ValidCharSet[j]);
                }

                // find a character that is not valid and inject it into the tag
                char c;
                do
                {
                    c = (char)random.Next(0, 65535);
                } while (ValidCharSet.Contains(c));

                int index = random.Next(0, builder.Length - 1);
                builder.Insert(index, c);

                // check the tag
                string tag = builder.ToString();
                Assert.Throws <ArgumentException>(() => LogWriterTag.CheckTag(tag));
            }
        }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TagSet"/> class with the specified tags.
 /// </summary>
 /// <param name="tags">Tags to keep in the collection.</param>
 /// <exception cref="ArgumentException">At least one of the tags is invalid.</exception>
 public TagSet(IEnumerable <string> tags)
 {
     if (tags == null)
     {
         throw new ArgumentNullException(nameof(tags));
     }
     foreach (string tag in tags)
     {
         LogWriterTag.CheckTag(tag);
     }
     mTags = new List <string>(new HashSet <string>(tags, StringComparer.Ordinal));
     mTags.Sort(StringComparer.OrdinalIgnoreCase);
     mHashCode = CalculateHashCode(mTags);
 }
        public void CheckTag_Valid()
        {
            var builder = new StringBuilder();
            var random  = new Random(0);

            for (int sample = 0; sample < 10000; sample++)
            {
                builder.Clear();

                // generate a valid tag
                int length = random.Next(1, 50);
                for (int i = 0; i < length; i++)
                {
                    int j = random.Next(0, ValidCharSet.Length - 1);
                    builder.Append(ValidCharSet[j]);
                }

                // check the tag
                string tag = builder.ToString();
                LogWriterTag.CheckTag(tag);
            }
        }
Пример #7
0
 /// <summary>
 /// Checks whether the specified tag equals the current one.
 /// </summary>
 /// <param name="other">Tag to compare with.</param>
 /// <returns>
 /// <c>true</c> if the specified tag equals the current one;
 /// otherwise <c>false</c>.
 /// </returns>
 private bool Equals(LogWriterTag other)
 {
     return(Id == other.Id && Name == other.Name);
 }
Пример #8
0
 /// <summary>
 /// Is called when a new log writer tag was added to the logging subsystem
 /// (the pipeline stage lock <see cref="Sync"/> is acquired when this method is called).
 /// This method must not throw exceptions.
 /// </summary>
 /// <param name="tag">The new log writer tag.</param>
 protected virtual void OnLogWriterTagAdded(LogWriterTag tag)
 {
 }