예제 #1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="LogTag" /> class. Use this version if you want to nest other tags
        ///     underneath this tag.
        ///     <para></para>
        ///     <para>
        ///         If a parent is active, all nested tags are also considered active.
        ///     </para>
        ///     <para>
        ///         NOTE: LogTags are clever enough not to loop forever when checking if parent tags are active but there's no
        ///         other checking for how you manage your nesting, so be sensible.
        ///     </para>
        /// </summary>
        /// <param name="name">The tag name.</param>
        /// <param name="childLogTags">The child log tags, each of which will be updated to make this tag their parent.</param>
        /// <param name="parentLogTag">The parent log tag.</param>
        public LogTag(string name, IEnumerable <LogTag> childLogTags, LogTag parentLogTag = null)
        {
            Name = name;
            EncounteredLogTags.Add(this);

            foreach (LogTag childLogTag in childLogTags)
            {
                childLogTag.Parent = this;
            }

            Parent = parentLogTag;
        }
예제 #2
0
        /// <summary>
        ///     Creates a new tag that is scoped within the current thread. This results in all non tagged logs within this scope
        ///     being automatically tagged. This is a way of controlling ALL logging that might occur within scope without needing
        ///     to add tags to each statement.
        /// </summary>
        /// <returns></returns>
        public Scoped Scope()
        {
            LogTag currentScope = Scoped.CurrentValue;
            LogTag newScope     = new LogTag(string.Empty, this);
            Scoped retVal       = new Scoped(newScope);

            if (currentScope != null)
            {
                newScope.ScopeParent = currentScope;
            }

            return(retVal);
        }
        /// <summary>
        ///  Constructs a <see cref="ScopeContext"/> for logging. Messages logged within this scope will either be indented or have the scope name prefixed. See also <seealso cref="Logger.UseContext"/>
        /// </summary>
        /// <param name="_scopeName">A name for the scope we are entering. It might be a method name, it might be something else to identify the scope your logging</param>
        /// <param name="logTag">The log tag to apply to this scope. Defaults to null, i.e. no log tag applied</param>
        public ScopeContext(string _scopeName, LogTag logTag = null) : base(
                $"{(string.IsNullOrEmpty(CurrentValue) ? string.Empty : CurrentValue)}{_scopeName}{spacer}")
        {
            scopeName = _scopeName ?? throw new ArgumentNullException(nameof(_scopeName), "There must be a value for context!");

            if (logTag != null)
            {
                logTagScope = new LogTag.Scoped(logTag);
            }

            // If we aren't putting the context in front of each message then we log the entry and exit points and increment and decrement the indent every time we enter and leave scope
            if (!Logger.UseContext)
            {
                Logger.Debug($"{scopeName} - Entry");
                Logger._Indent.Value++;
            }
        }
        /// <summary>
        ///     Logs the appropriate level of message.
        /// </summary>
        /// <param name="level">The level.</param>
        /// <param name="context">The context - if <see cref="Logger.UseContext" /> is false, this will be empty.</param>
        /// <param name="threadId">The thread identifier - if <see cref="Logger.UseThreadId"/> is false, this will be empty.</param>
        /// <param name="logTag">The log tag.</param>
        /// <param name="msg">The message.</param>
        public void LogThis(LoggingLevel level, string context, string threadId, LogTag logTag, string msg)
        {
            ILogger logger = loggerFactory.CreateLogger("Moonrise");

            if (!string.IsNullOrWhiteSpace(context))
            {
                msg = $"{context}: {msg}";
            }

            switch (level)
            {
            case LoggingLevel.Debug:
                logger.LogDebug(msg);
                break;

            case LoggingLevel.Information:
                logger.LogInformation(msg);
                break;

            case LoggingLevel.Warning:
                logger.LogWarning(msg);
                break;

            case LoggingLevel.Error:
                logger.LogError(msg);
                break;

            case LoggingLevel.Fatal:
                logger.LogError($"***** FATAL ***** - {msg}");
                break;

            case LoggingLevel.Audit:
                logger.LogInformation($"***** AUDIT ***** - {msg}");
                break;
            }
        }
예제 #5
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="LogTag" /> class. Use this version if you want to nest this tag under
 ///     another existing tag.
 ///     <para>
 ///         If a parent is active, all nested tags are also considered active.
 ///     </para>
 ///     <para>
 ///         NOTE: LogTags are clever enough not to loop forever when checking if parent tags are active but there's no
 ///         other checking for how you manage your nesting, so be sensible.
 ///     </para>
 /// </summary>
 /// <param name="name">The tag name.</param>
 /// <param name="parentLogTag">A parent log tag so that you can group and nest tags.</param>
 public LogTag(string name, LogTag parentLogTag)
 {
     Name   = name;
     Parent = parentLogTag;
     EncounteredLogTags.Add(this);
 }
예제 #6
0
 /// <summary>
 ///     Deactivates the log tag by removing it from the list of those already active.
 /// </summary>
 /// <param name="tag">The tag to deactivate</param>
 public static void DeactivateLogTag(LogTag tag)
 {
     ActiveLogTags.Remove(tag.Name);
 }
예제 #7
0
 /// <summary>
 ///     Activates the log tag by adding it to the list of those already active.
 /// </summary>
 /// <param name="tag">LogTag to activate</param>
 public static void ActivateLogTag(LogTag tag)
 {
     ActiveLogTags.Add(tag.Name);
 }