Example #1
0
        /// <summary>
        /// Flush pending log messages to each registered <see cref="T:ILog"/> target.
        /// </summary>
        /// <param name="iterationLimit">Limits the number of messages which may be flushed. Zero or less for no limit.</param>
        /// <remarks>
        /// Must be called periodicall to ensure pending messages are removed.
        ///
        /// The number of iterations performed may be limited by <paramref name="iterationLimit"/>
        /// to avoid any infinite loops (e.g., logging from a call to <see cref="T:ILog"/>).
        /// The default is set high to ensure some limit which should not be practically reached.
        /// </remarks>
        public static void Flush(int iterationLimit = 1000000)
        {
            int   iterations = 0;
            Entry entry      = new Entry();

            while ((iterationLimit <= 0 || iterations < iterationLimit) && _logQueue.TryDequeue(ref entry))
            {
                for (int i = 0; i < _targets.Count; ++i)
                {
                    if (entry.Except == null)
                    {
                        _targets[i].Log(entry.Level, entry.Category, entry.Message);
                    }
                    else
                    {
                        _targets[i].Log(entry.Except);
                    }
                }
                ++iterations;
            }
        }