Beispiel #1
0
        /// <summary>
        /// Gets a bit mask in which each bit is associated with a log level with the same id
        /// and expresses whether the corresponding log level is active for the specified writer.
        /// </summary>
        /// <param name="writer">Log writer to get the active log level mask for.</param>
        /// <returns>The requested active log level mask.</returns>
        public override LogLevelBitMask GetActiveLogLevelMask(LogWriter writer)
        {
            lock (Sync)
            {
                // get the first matching log writer settings
                LogWriterConfiguration settings = null;
                foreach (var configuration in File.LogWriterSettings)
                {
                    if (configuration.NamePatterns.Any(x => x.Regex.IsMatch(writer.Name)))
                    {
                        // found match by log writer name, check tags now
                        // - if no tags are configured => match always
                        // - if tags are configured => match, if at least one tag matches
                        if (!configuration.TagPatterns.Any() || configuration.TagPatterns.Any(x => writer.Tags.Any <string>(y => x.Regex.IsMatch(y))))
                        {
                            settings = configuration;
                            break;
                        }
                    }
                }

                if (settings != null)
                {
                    LogLevelBitMask mask;

                    // enable all log levels that are covered by the base level
                    var level = LogLevel.GetAspect(settings.BaseLevel);                     // returns predefined log levels as well
                    if (level == LogLevel.All)
                    {
                        mask = new LogLevelBitMask(LogLevel.MaxId + 1, true, false);
                    }
                    else
                    {
                        mask = new LogLevelBitMask(LogLevel.MaxId + 1, false, false);
                        mask.SetBits(0, level.Id + 1);
                    }

                    // add log levels explicitly included
                    foreach (string include in settings.Includes)
                    {
                        level = LogLevel.GetAspect(include);
                        mask.SetBit(level.Id);
                    }

                    // disable log levels explicitly excluded
                    foreach (string exclude in settings.Excludes)
                    {
                        level = LogLevel.GetAspect(exclude);
                        mask.ClearBit(level.Id);
                    }

                    return(mask);
                }

                // no matching settings found
                // => disable all log levels...
                return(new LogLevelBitMask(0, false, false));
            }
        }
        [InlineData(128, 1, 126, new uint[] { 0xFFFFFFFEu, 0xFFFFFFFFu, 0xFFFFFFFFu, 0x7FFFFFFFu })]         // all bits except the first and the last one
        public void SetBits(
            int size,
            int fromBit,
            int count,
            uint[] expectedMaskArray)
        {
            var mask = new LogLevelBitMask(size, false, false);

            // check the actual size of the mask in bits
            int effectiveSize = (size + 31) & ~31;

            Assert.Equal(effectiveSize, mask.Size);

            // clear bit
            mask.SetBits(fromBit, count);

            // check underlying buffer
            uint[] maskArray = mask.AsArray();
            Assert.Equal(expectedMaskArray, maskArray);
        }