1005 lines · 37.7 KB · C#
RawDownload
//******************************************************************************************************//  LogFile.cs - Gbtc////  Copyright © 2012, Grid Protection Alliance.  All Rights Reserved.////  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See//  the NOTICE file distributed with this work for additional information regarding copyright ownership.//  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may//  not use this file except in compliance with the License. You may obtain a copy of the License at:////      http://www.opensource.org/licenses/MIT////  Unless agreed to in writing, the subject software distributed under the License is distributed on an//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the//  License for the specific language governing permissions and limitations.////  Code Modification History://  ----------------------------------------------------------------------------------------------------//  04/09/2007 - Pinal C. Patel//       Original version of source code generated.//  11/30/2007 - Pinal C. Patel//       Modified the "design time" check in EndInit() method to use LicenseManager.UsageMode property//       instead of DesignMode property as the former is more accurate than the latter.//  09/19/2008 - J. Ritchie Carroll//       Converted to C#.//  10/21/2008 - Pinal C. Patel//       Edited code comments.//  06/18/2009 - Pinal C. Patel//       Fixed the implementation of Enabled property.//  07/02/2009 - Pinal C. Patel//       Modified state alternating properties to reopen the file when changed.//  09/14/2009 - Stephen C. Wills//       Added new header and license agreement.//  07/30/2012 - Vijay Sukhavasi//       Added user option to control time duration of rollover. //  12/14/2012 - Starlynn Danyelle Gilliam//       Modified Header.////****************************************************************************************************** using System;using System.Collections.Generic;using System.ComponentModel;using System.Configuration;using System.Drawing;using System.IO;using System.Linq;using System.Text;using System.Threading;using System.Timers;using GSF.Collections;using GSF.Configuration;using GSF.Threading;using Timer = System.Timers.Timer; namespace GSF.IO{    #region [ Enumerations ]     /// <summary>    /// Specifies the operation to be performed on the <see cref="LogFile"/> when it is full.    /// </summary>    public enum LogFileFullOperation    {        /// <summary>        /// Truncates the existing entries in the <see cref="LogFile"/> to make space for new entries.        /// </summary>        Truncate,        /// <summary>        /// Rolls over to a new <see cref="LogFile"/>, and keeps the full <see cref="LogFile"/> for reference.        /// </summary>        Rollover    }     #endregion     /// <summary>    /// Represents a file that can be used for logging messages in real-time.    /// </summary>    /// <example>    /// This example shows how to use <see cref="LogFile"/> for logging messages:    /// <code>    /// using System;    /// using GSF.IO;    ///    /// class Program    /// {    ///     static void Main(string[] args)    ///     {    ///         LogFile log = new LogFile();    ///         log.Initialize();                       // Initialize the log file.    ///         log.Open();                             // Open the log file.    ///         log.WriteTimestampedLine("Test entry"); // Write message to the log file.    ///         log.Flush();                            // Flush message to the log file.    ///         log.Close();                            // Close the log file.    ///    ///         Console.ReadLine();    ///     }    /// }    /// </code>    /// </example>    [ToolboxBitmap(typeof(LogFile))]    public class LogFile : Component, ISupportLifecycle, ISupportInitialize, IProvideStatus, IPersistSettings    {        #region [ Members ]         // Constants         /// <summary>        /// Specifies the minimum size for a <see cref="LogFile"/>.        /// </summary>        public const int MinFileSize = 1;         /// <summary>        /// Specifies the maximum size for a <see cref="LogFile"/>.        /// </summary>        public const int MaxFileSize = 10;         /// <summary>        /// Specifies the default value for the <see cref="FileName"/> property.        /// </summary>        public const string DefaultFileName = "LogFile.txt";         /// <summary>        /// Specifies the default value for the <see cref="FileSize"/> property.        /// </summary>        public const int DefaultFileSize = 3;         /// <summary>        /// Specifies the default value for the <see cref="FileFullOperation"/> property.        /// </summary>        public const LogFileFullOperation DefaultFileFullOperation = LogFileFullOperation.Truncate;         /// <summary>        /// Specifies the default value for the <see cref="PersistSettings"/> property.        /// </summary>        public const bool DefaultPersistSettings = false;         /// <summary>        /// Specifies the default value for the <see cref="LogFilesDuration"/> property.        /// </summary>        public const double DefaultLogFilesDuration = 0.0;         /// <summary>        /// Specifies the default value for the <see cref="FlushTimerInterval"/> property.        /// </summary>        public const double DefaultFlushTimerInterval = 10.0D;         /// <summary>        /// Specifies the default value for the <see cref="SettingsCategory"/> property.        /// </summary>        public const string DefaultSettingsCategory = "LogFile";         // Events         /// <summary>        /// Occurs when the <see cref="LogFile"/> is full.        /// </summary>        [Description("Occurs when the LogFile is full.")]        public event EventHandler FileFull;         /// <summary>        /// Occurs when an <see cref="Exception"/> is encountered while writing entries to the <see cref="LogFile"/>.        /// </summary>        /// <remarks>        /// <see cref="EventArgs{T}.Argument"/> is the <see cref="Exception"/> encountered during writing of log entries.        /// </remarks>        [Description("Occurs when an Exception is encountered while writing entries to the LogFile.")]        public event EventHandler<EventArgs<Exception>> LogException;         // Fields        private string m_fileName;        private int m_fileSize;        private LogFileFullOperation m_fileFullOperation;        private bool m_persistSettings;        private string m_settingsCategory;        private FileStream m_fileStream;        private readonly object m_fileStreamLock;        private ManualResetEvent m_operationWaitHandle;        private ProcessQueue<string> m_logEntryQueue;        private Timer m_flushTimer;        private Encoding m_textEncoding;        private bool m_disposed;        private bool m_initialized;        private double m_logFilesDuration;        private double m_flushTimerInterval;        private readonly Dictionary<DateTime, string> m_savedFilesWithTime;         #endregion         #region [ Constructors ]         /// <summary>        /// Initializes a new instance of the <see cref="LogFile"/> class.        /// </summary>        public LogFile()        {            m_fileName = DefaultFileName;            m_fileSize = DefaultFileSize;            m_fileFullOperation = DefaultFileFullOperation;            m_logFilesDuration = DefaultLogFilesDuration;            m_persistSettings = DefaultPersistSettings;            m_settingsCategory = DefaultSettingsCategory;            m_textEncoding = Encoding.Default;            m_operationWaitHandle = new ManualResetEvent(true);            m_savedFilesWithTime = new Dictionary<DateTime, string>();            m_logEntryQueue = ProcessQueue<string>.CreateRealTimeQueue(WriteLogEntries);            m_logEntryQueue.SynchronizedOperationType = SynchronizedOperationType.Long;            m_flushTimer = new Timer();            m_flushTimerInterval = 10.0D;            m_fileStreamLock = new object();             this.FileFull += LogFile_FileFull;            m_logEntryQueue.ProcessException += ProcessExceptionHandler;            m_flushTimer.Elapsed += FlushTimer_Elapsed;            m_flushTimer.AutoReset = false;        }         /// <summary>        /// Initializes a new instance of the <see cref="LogFile"/> class.        /// </summary>        /// <param name="container"><see cref="IContainer"/> object that contains the <see cref="LogFile"/>.</param>        public LogFile(IContainer container)            : this()        {            if ((object)container != null)                container.Add(this);        }         #endregion         #region [ Properties ]         /// <summary>        /// Gets or sets the name of the <see cref="LogFile"/>, including the file extension.        /// </summary>        /// <exception cref="ArgumentNullException">The value being assigned is null or empty string.</exception>        [Category("Settings"),        DefaultValue(DefaultFileName),        Description("Name of the LogFile, including the file extension.")]        public string FileName        {            get            {                return m_fileName;            }            set            {                if (string.IsNullOrEmpty(value))                    throw new ArgumentNullException("value");                 m_fileName = value;                ReOpen();            }        }         /// <summary>        /// Gets or sets the size of the <see cref="LogFile"/> in MB.        /// </summary>        ///<exception cref="ArgumentOutOfRangeException">The value being assigned outside the <see cref="MinFileSize"/> and <see cref="MaxFileSize"/> range.</exception>        [Category("Settings"),        DefaultValue(DefaultFileSize),        Description("Size of the LogFile in MB.")]        public int FileSize        {            get            {                return m_fileSize;            }            set            {                if (value < MinFileSize || value > MaxFileSize)                    throw new ArgumentOutOfRangeException("value", string.Format("Value must be between {0} and {1}", MinFileSize, MaxFileSize));                 m_fileSize = value;            }        }         /// <summary>        /// Gets or sets the type of operation to be performed when the <see cref="LogFile"/> is full.        /// </summary>        [Category("Behavior"),        DefaultValue(DefaultFileFullOperation),        Description("Type of operation to be performed when the LogFile is full.")]        public LogFileFullOperation FileFullOperation        {            get            {                return m_fileFullOperation;            }            set            {                m_fileFullOperation = value;            }        }         /// <summary>        /// Gets or sets the time duration, in hours, to save the <see cref="LogFile"/> .        /// </summary>        [Category("Behavior"),        DefaultValue(DefaultLogFilesDuration),        Description("Time duration in hours to save the log files.")]        public double LogFilesDuration        {            get            {                return m_logFilesDuration;            }            set            {                m_logFilesDuration = value;            }        }         /// <summary>        /// Gets or sets the number of seconds of inactivity before the <see cref="LogFile"/> automatically flushes the file stream.        /// </summary>        [Category("Behavior"),        DefaultValue(DefaultFlushTimerInterval),        Description("Number of seconds of inactivity before the log file automatically flushes the file stream.")]        public double FlushTimerInterval        {            get            {                return m_flushTimerInterval;            }            set            {                m_flushTimerInterval = value;                 if (m_flushTimerInterval > 0.0D)                    m_flushTimer.Interval = m_flushTimerInterval * 1000.0D;            }        }         /// <summary>        /// Gets or sets a boolean value that indicates whether the settings of <see cref="LogFile"/> object are         /// to be saved to the config file.        /// </summary>        [Category("Persistence"),        DefaultValue(DefaultPersistSettings),        Description("Indicates whether the settings of LogFile object are to be saved to the config file.")]        public bool PersistSettings        {            get            {                return m_persistSettings;            }            set            {                m_persistSettings = value;            }        }         /// <summary>        /// Gets or sets the category under which the settings of <see cref="LogFile"/> object are to be saved        /// to the config file if the <see cref="PersistSettings"/> property is set to true.        /// </summary>        /// <exception cref="ArgumentNullException">The value being assigned is null or empty string.</exception>        [Category("Persistence"),        DefaultValue(DefaultSettingsCategory),        Description("Category under which the settings of LogFile object are to be saved to the config file if the PersistSettings property is set to true.")]        public string SettingsCategory        {            get            {                return m_settingsCategory;            }            set            {                if (string.IsNullOrEmpty(value))                    throw new ArgumentNullException("value");                 m_settingsCategory = value;            }        }         /// <summary>        /// Gets or sets the <see cref="Encoding"/> to be used to encode the messages being logged.        /// </summary>        [Browsable(false),        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]        public virtual Encoding TextEncoding        {            get            {                return m_textEncoding;            }            set            {                if ((object)value == null)                    m_textEncoding = Encoding.Default;                else                    m_textEncoding = value;            }        }         /// <summary>        /// Gets or sets a boolean value that indicates whether the <see cref="LogFile"/> object is currently enabled.        /// </summary>        [Browsable(false),        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]        public bool Enabled        {            get            {                return IsOpen;            }            set            {                if (value && !Enabled)                    Open();                else if (!value && Enabled)                    Close();            }        }         /// <summary>        /// Gets a flag that indicates whether the object has been disposed.        /// </summary>        [Browsable(false),        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]        public bool IsDisposed        {            get            {                return m_disposed;            }        }         /// <summary>        /// Gets a boolean value that indicates whether the <see cref="LogFile"/> is open.        /// </summary>        [Browsable(false)]        public bool IsOpen        {            get            {                return ((object)m_fileStream != null);            }        }         /// <summary>        /// Gets the unique identifier of the <see cref="LogFile"/> object.        /// </summary>        [Browsable(false)]        public string Name        {            get            {                return m_settingsCategory;            }        }         /// <summary>        /// Gets the descriptive status of the <see cref="LogFile"/> object.        /// </summary>        [Browsable(false)]        public string Status        {            get            {                StringBuilder status = new StringBuilder();                 status.Append("     Configuration section: ");                status.Append(m_settingsCategory);                status.AppendLine();                status.Append("       Maximum export size: ");                status.Append(m_fileSize.ToString());                status.Append(" MB");                status.AppendLine();                status.Append("       File full operation: ");                status.Append(m_fileFullOperation);                status.AppendLine();                 if ((object)m_logEntryQueue != null)                    status.Append(m_logEntryQueue.Status);                 return status.ToString();            }        }         #endregion         #region [ Methods ]         /// <summary>        /// Initializes the <see cref="LogFile"/> object.        /// </summary>        /// <remarks>        /// <see cref="Initialize()"/> is to be called by user-code directly only if the <see cref="LogFile"/>         /// object is not consumed through the designer surface of the IDE.        /// </remarks>        public void Initialize()        {            if (!m_initialized)            {                LoadSettings();         // Load settings from the config file.                m_initialized = true;   // Initialize only once.            }

Lines 1–500 of 1005.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t