/// <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)); } }
/// <summary> /// Checks whether the specified object equals the current one /// (does not take the <see cref="IsDefault"/> property into account). /// </summary> /// <param name="other">Object to compare with.</param> /// <returns>true, if the specified object equals the current one; otherwise false.</returns> public bool Equals(LogWriterConfiguration other) { return(mBaseLevel == other.mBaseLevel && NamePatterns.SequenceEqual(other.NamePatterns) && TagPatterns.SequenceEqual(other.TagPatterns) && Includes.SequenceEqual(other.Includes) && Excludes.SequenceEqual(other.Excludes)); }
/// <summary> /// Initializes a new instance of the <see cref="LogWriterConfiguration"/> class by copying another instance. /// </summary> /// <param name="other">Instance to copy.</param> internal LogWriterConfiguration(LogWriterConfiguration other) { mNamePatterns.AddRange(other.mNamePatterns); // the patterns are immutable mTagPatterns.AddRange(other.mTagPatterns); // the patterns are immutable mBaseLevel = other.BaseLevel; // immutable IsDefault = other.IsDefault; // immutable mIncludes.AddRange(other.mIncludes); // the log levels are immutable mExcludes.AddRange(other.mExcludes); // the log levels are immutable }
public void Log_Configuration_Should_Let_Messages_Of_Included_Levels_Pass(string levelToInclude) { // initialize the logging subsystem int callbackInvokedCount = 0; Log.Initialize <VolatileLogConfiguration>( config => { // // configuration // // set configuration to block the excluded log level only var settings = new LogWriterConfiguration[1]; settings[0] = LogWriterConfigurationBuilder .New .MatchingWildcardPattern("*") .WithBaseLevel(LogLevel.None) .WithLevel(levelToInclude) .Build(); config.SetLogWriterSettings(settings); }, builder => { // // processing pipeline stage // builder.Add <CallbackPipelineStage>( "Callback", stage => { // set the processing stage test callback stage.ProcessingCallback = msg => { Assert.Equal(TestMessage, msg.Text); Assert.Equal(levelToInclude, msg.LogLevel.Name); Assert.Equal(levelToInclude, msg.LogLevelName); callbackInvokedCount++; return(true); }; }); }); // write a message using all log messages var writer = Log.GetWriter(sLogWriterName); foreach (var level in LogLevel.PredefinedLogLevels) { writer.Write(level, TestMessage); } // the callback should have been invoked exactly once Assert.Equal(1, callbackInvokedCount); }
public void Log_Configuration_Should_Let_Messages_Below_BaseLevel_Pass(string baseLevel) { // convert log level name to LogLevel object var threshold = LogLevel.GetAspect(baseLevel); // initialize the logging subsystem int callbackInvokedCount = 0; Log.Initialize <VolatileLogConfiguration>( config => { // // configuration // // set configuration to block the excluded log level only var settings = new LogWriterConfiguration[1]; settings[0] = LogWriterConfigurationBuilder .New .MatchingWildcardPattern("*") .WithBaseLevel(baseLevel) .Build(); config.SetLogWriterSettings(settings); }, builder => { // // processing pipeline stage // builder.Add <CallbackPipelineStage>( "Callback", stage => { // set the processing stage test callback stage.ProcessingCallback = msg => { Assert.True(msg.LogLevel.Id <= threshold.Id); callbackInvokedCount++; return(true); }; }); }); // write a message using all predefined log levels var writer = Log.GetWriter(sLogWriterName); foreach (var level in LogLevel.PredefinedLogLevels) { writer.Write(level, TestMessage); } // check whether the callback has been invoked as often as expected Assert.Equal(threshold.Id + 1, callbackInvokedCount); }
/// <summary> /// Builds the configured log writer configuration. /// </summary> /// <returns>The built log writer configuration.</returns> public LogWriterConfiguration Build() { var copy = new LogWriterConfiguration(mConfiguration); // add default pattern matching all log writer names, if no pattern was specified explicitly if (!copy.mNamePatterns.Any()) { copy.mNamePatterns.Add(LogWriterConfiguration.DefaultPattern); } // tags are only considered, if specified => no need to add a default pattern for them return(copy); }
public void Getting_Active_Log_Level_Mask_For_Specific_BaseLevel(string level, uint expectedMask) { using (var configuration = new TConfiguration()) { var settings = new LogWriterConfiguration[1]; settings[0] = LogWriterConfigurationBuilder .New .MatchingWildcardPattern("*") .WithBaseLevel(level) .Build(); configuration.SetLogWriterSettings(settings); var writer = Log.GetWriter("UnitTest"); var mask = configuration.GetActiveLogLevelMask(writer); uint[] bitArray = mask.AsArray(); Assert.Single(bitArray); Assert.Equal(expectedMask, bitArray[0]); } }