Base class for optimized file appenders.
Inheritance: IDisposable
Exemplo n.º 1
0
        private void CloseAppender(BaseFileAppender appender)
        {
            appender.Close();

#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
            externalFileArchivingWatcher.StopWatching();
#endif
        }
Exemplo n.º 2
0
        private BaseFileAppender GetAppender(string fileName)
        {
            for (int i = 0; i < this.appenders.Length; ++i)
            {
                BaseFileAppender appender = this.appenders[i];
                if (appender == null)
                {
                    break;
                }

                if (string.Equals(appender.FileName, fileName, StringComparison.OrdinalIgnoreCase))
                {
                    return(appender);
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        private void CloseAppender(BaseFileAppender appender, string reason, bool lastAppender)
        {
            InternalLogger.Debug("FileAppender Closing {0} - {1}", reason, appender.FileName);

            if (lastAppender)
            {
                // No active appenders, deactivate background tasks
                autoClosingTimer.Change(Timeout.Infinite, Timeout.Infinite);

#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
                externalFileArchivingWatcher.StopWatching();
                logFileWasArchived = false;
            }
            else
            {
                externalFileArchivingWatcher.StopWatching(appender.FileName);
#endif
            }

            appender.Close();
        }
Exemplo n.º 4
0
        /// <summary>
        /// It allocates the first slot in the list when the file name does not already in the list and clean up any
        /// unused slots.
        /// </summary>
        /// <param name="fileName">File name associated with a single appender.</param>
        /// <returns>The allocated appender.</returns>
        /// <exception cref="NullReferenceException">
        /// Thrown when <see cref="M:AllocateAppender"/> is called on an <c>Empty</c><see cref="FileAppenderCache"/> instance.
        /// </exception>
        public BaseFileAppender AllocateAppender(string fileName)
        {
            //
            // BaseFileAppender.Write is the most expensive operation here
            // so the in-memory data structure doesn't have to be
            // very sophisticated. It's a table-based LRU, where we move
            // the used element to become the first one.
            // The number of items is usually very limited so the
            // performance should be equivalent to the one of the hashtable.
            //

            BaseFileAppender appenderToWrite = null;
            int freeSpot = appenders.Length - 1;

            for (int i = 0; i < appenders.Length; ++i)
            {
                // Use empty slot in recent appender list, if there is one.
                if (appenders[i] == null)
                {
                    freeSpot = i;
                    break;
                }

                if (appenders[i].FileName == fileName)
                {
                    // found it, move it to the first place on the list
                    // (MRU)

                    // file open has a chance of failure
                    // if it fails in the constructor, we won't modify any data structures
                    BaseFileAppender app = appenders[i];
                    for (int j = i; j > 0; --j)
                    {
                        appenders[j] = appenders[j - 1];
                    }

                    appenders[0]    = app;
                    appenderToWrite = app;
                    break;
                }
            }

            if (appenderToWrite == null)
            {
                BaseFileAppender newAppender = Factory.Open(fileName, CreateFileParameters);

                if (appenders[freeSpot] != null)
                {
                    CloseAppender(appenders[freeSpot]);
                    appenders[freeSpot] = null;
                }

                for (int j = freeSpot; j > 0; --j)
                {
                    appenders[j] = appenders[j - 1];
                }

                appenders[0]    = newAppender;
                appenderToWrite = newAppender;

#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
                if (!string.IsNullOrEmpty(archiveFilePatternToWatch))
                {
                    var archiveFilePatternToWatchPath = GetFullPathForPattern(archiveFilePatternToWatch);

                    string directoryPath = Path.GetDirectoryName(archiveFilePatternToWatchPath);
                    if (!Directory.Exists(directoryPath))
                    {
                        Directory.CreateDirectory(directoryPath);
                    }

                    externalFileArchivingWatcher.Watch(archiveFilePatternToWatchPath);
                }
#endif
            }

            return(appenderToWrite);
        }
Exemplo n.º 5
0
        /// <summary>
        /// It allocates the first slot in the list when the file name does not already in the list and clean up any
        /// unused slots.
        /// </summary>
        /// <param name="fileName">File name associated with a single appender.</param>
        /// <returns>The allocated appender.</returns>
        /// <exception cref="NullReferenceException">
        /// Thrown when <see cref="M:AllocateAppender"/> is called on an <c>Empty</c><see cref="FileAppenderCache"/> instance.
        /// </exception>
        public BaseFileAppender AllocateAppender(string fileName)
        {
            //
            // BaseFileAppender.Write is the most expensive operation here
            // so the in-memory data structure doesn't have to be
            // very sophisticated. It's a table-based LRU, where we move
            // the used element to become the first one.
            // The number of items is usually very limited so the
            // performance should be equivalent to the one of the hashtable.
            //

            BaseFileAppender appenderToWrite = null;
            int freeSpot = appenders.Length - 1;

            for (int i = 0; i < appenders.Length; ++i)
            {
                // Use empty slot in recent appender list, if there is one.
                if (appenders[i] == null)
                {
                    freeSpot = i;
                    break;
                }

                if (appenders[i].FileName == fileName)
                {
                    // found it, move it to the first place on the list
                    // (MRU)

                    // file open has a chance of failure
                    // if it fails in the constructor, we won't modify any data structures
                    BaseFileAppender app = appenders[i];
                    for (int j = i; j > 0; --j)
                    {
                        appenders[j] = appenders[j - 1];
                    }

                    appenders[0]    = app;
                    appenderToWrite = app;
                    break;
                }
            }

            if (appenderToWrite == null)
            {
                BaseFileAppender newAppender = Factory.Open(fileName, CreateFileParameters);

                if (appenders[freeSpot] != null)
                {
                    appenders[freeSpot].Close();
                    appenders[freeSpot] = null;
                }

                for (int j = freeSpot; j > 0; --j)
                {
                    appenders[j] = appenders[j - 1];
                }

                appenders[0]    = newAppender;
                appenderToWrite = newAppender;
            }

            return(appenderToWrite);
        }
Exemplo n.º 6
0
        private void CloseAppender(BaseFileAppender appender)
        {
            appender.Close();

#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
            externalFileArchivingWatcher.StopWatching();
#endif
        }
Exemplo n.º 7
0
        /// <summary>
        /// It allocates the first slot in the list when the file name does not already in the list and clean up any
        /// unused slots.
        /// </summary>
        /// <param name="fileName">File name associated with a single appender.</param>
        /// <returns>The allocated appender.</returns>
        /// <exception cref="NullReferenceException">
        /// Thrown when <see cref="M:AllocateAppender"/> is called on an <c>Empty</c><see cref="FileAppenderCache"/> instance.
        /// </exception>
        public BaseFileAppender AllocateAppender(string fileName)
        {
            //
            // BaseFileAppender.Write is the most expensive operation here
            // so the in-memory data structure doesn't have to be
            // very sophisticated. It's a table-based LRU, where we move
            // the used element to become the first one.
            // The number of items is usually very limited so the
            // performance should be equivalent to the one of the hashtable.
            //

            BaseFileAppender appenderToWrite = null;
            int freeSpot = appenders.Length - 1;

            for (int i = 0; i < appenders.Length; ++i)
            {
                // Use empty slot in recent appender list, if there is one.
                if (appenders[i] == null)
                {
                    freeSpot = i;
                    break;
                }

                if (string.Equals(appenders[i].FileName, fileName, StringComparison.OrdinalIgnoreCase))
                {
                    // found it, move it to the first place on the list
                    // (MRU)
                    BaseFileAppender app = appenders[i];
                    if (i > 0)
                    {
                        // file open has a chance of failure
                        // if it fails in the constructor, we won't modify any data structures
                        for (int j = i; j > 0; --j)
                        {
                            appenders[j] = appenders[j - 1];
                        }

                        appenders[0] = app;
                    }
                    appenderToWrite = app;
                    break;
                }
            }

            if (appenderToWrite == null)
            {
                try
                {
                    InternalLogger.Debug("Creating file appender: {0}", fileName);
                    BaseFileAppender newAppender = Factory.Open(fileName, CreateFileParameters);

                    if (appenders[freeSpot] != null)
                    {
                        CloseAppender(appenders[freeSpot], "Stale", false);
                        appenders[freeSpot] = null;
                    }

                    for (int j = freeSpot; j > 0; --j)
                    {
                        appenders[j] = appenders[j - 1];
                    }

                    appenders[0]    = newAppender;
                    appenderToWrite = newAppender;

                    if (CheckCloseAppenders != null)
                    {
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__
                        if (freeSpot == 0)
                        {
                            logFileWasArchived = false;
                        }
                        if (!string.IsNullOrEmpty(archiveFilePatternToWatch))
                        {
                            string directoryPath = Path.GetDirectoryName(archiveFilePatternToWatch);
                            if (!Directory.Exists(directoryPath))
                            {
                                Directory.CreateDirectory(directoryPath);
                            }

                            externalFileArchivingWatcher.Watch(archiveFilePatternToWatch); // Always monitor the archive-folder
                        }
                        externalFileArchivingWatcher.Watch(appenderToWrite.FileName);      // Monitor the active file-appender
#endif
                    }
                }
                catch (Exception ex)
                {
                    InternalLogger.Warn(ex, "Failed to create file appender: {0}", fileName);
                    throw;
                }
            }

            return(appenderToWrite);
        }