Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LogUtility"/> class.
        /// </summary>
        /// <param name="productName">Name of the product.</param>
        /// <param name="outputFolder">The output folder. Pass empty string or null to log to executable directory</param>
        /// <param name="extension">The extension for the log file - leading dot is optional (e.g 'log', '.errors' or '.trc').
        /// Pass empty string or null for default extension</param>
        /// <param name="level">The MINIMUM level of logging to be included in the output.</param>
        /// <param name="mode">The log writing mode.</param>
        /// <exception cref="ArgumentNullException">Thrown if the product name is null or empty</exception>
        /// <exception cref="PathTooLongException">Thrown if the log file path will exceed the maximum path length</exception>
        public static LogUtility CreateLogger(string productName, string outputFolder, string extension, LogLevel level, LogMode mode)
        {
            #region Implementation

            LogUtility logger = null;

            lock (_settings)
            {
                // Get the location of the executable, set that to our output Directory
                if (String.IsNullOrEmpty(outputFolder))
                {
                    outputFolder = Application.StartupPath;
                }

                //default to the custom folder
                string tempRoot = string.IsNullOrEmpty(outputFolder) ? Environment.CurrentDirectory : outputFolder;
                tempRoot = Path.Combine(tempRoot, LOG_ROOT);
                string source = Path.Combine(tempRoot, CustomFolder);

                Exception       settingsError   = null;
                bool            noCustomSetting = false;
                LoggingSettings settings        = null;

                //if there is no folder, use the application folder
                if (!Directory.Exists(source))
                {
                    source          = Application.StartupPath;
                    noCustomSetting = true;
                }

                string settingsFile = (Path.Combine(source, SETTINGS_FILE));

                //load existing settings
                if (File.Exists(settingsFile))
                {
                    try
                    {
                        //update the mode & level from the saved settings
                        settings = LoggingSettings.DeserializeFromFile(settingsFile);

                        if (noCustomSetting)
                        {
                            //if there is no customised setting, overwrite the application
                            //default with the settings requested by the caller
                            settings.LoggingLevel = level;
                            settings.LoggingMode  = mode;
                        }
                        else
                        {
                            //if there are customised settings, overwrite the caller's settings
                            //with those from the customised settings file
                            level = settings.LoggingLevel;
                            mode  = settings.LoggingMode;
                        }
                    }
                    catch (System.Runtime.Serialization.SerializationException error) { settingsError = error; }
                }

                //read in the max log size from the settings file or use the default if not available
                //int maxSize = settings != null ? settings.MaxLogSize : _maxSize;

                //create the logger
                logger = new LogUtility(productName, outputFolder, extension, _maxSize, level, mode);

                //if settings was found but could not be loaded, inform user
                if (settingsError != null)
                {
                    logger.AddException(string.Format("Error reading logging settings from {0}", source), settingsError);
                }

                //save the current settings for the user
                source = Path.Combine(tempRoot, CustomFolder);

                //only write the settings if the user had permissions to create a custom folder
                if (Directory.Exists(source) && (settings == null || noCustomSetting))
                {
                    settingsFile = (Path.Combine(source, SETTINGS_FILE));

                    settings = new LoggingSettings
                    {
                        LoggingLevel = level,
                        LoggingMode  = mode
                    };

                    settings.SerializeToFile(settingsFile);
                }
            }

            return(logger);

            #endregion
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LogUtility"/> class.
        /// </summary>
        /// <param name="productName">Name of the product.</param>
        /// <param name="outputFolder">The output folder. Pass empty string or null to log to executable directory</param>
        /// <param name="extension">The extension for the log file - leading dot is optional (e.g 'log', '.errors' or '.trc'). 
        /// Pass empty string or null for default extension</param>
        /// <param name="level">The MINIMUM level of logging to be included in the output.</param>
        /// <param name="mode">The log writing mode.</param>
        /// <exception cref="ArgumentNullException">Thrown if the product name is null or empty</exception>
        /// <exception cref="PathTooLongException">Thrown if the log file path will exceed the maximum path length</exception>
        public static LogUtility CreateLogger(string productName, string outputFolder, string extension, LogLevel level, LogMode mode)
        {
            #region Implementation

            LogUtility logger = null;

            lock (_settings)
            {
                // Get the location of the executable, set that to our output Directory
                if (String.IsNullOrEmpty(outputFolder))
                {
                    outputFolder = Application.StartupPath;
                }

                //default to the custom folder
                string tempRoot = string.IsNullOrEmpty(outputFolder) ? Environment.CurrentDirectory : outputFolder;
                tempRoot = Path.Combine(tempRoot, LOG_ROOT);
                string source = Path.Combine(tempRoot, CustomFolder);

                Exception settingsError = null;
                bool noCustomSetting = false;
                LoggingSettings settings = null;

                //if there is no folder, use the application folder
                if (!Directory.Exists(source))
                {
                    source = Application.StartupPath;
                    noCustomSetting = true;
                }

                string settingsFile = (Path.Combine(source, SETTINGS_FILE));

                //load existing settings
                if (File.Exists(settingsFile))
                {
                    try
                    {
                        //update the mode & level from the saved settings
                        settings = LoggingSettings.DeserializeFromFile(settingsFile);

                        if (noCustomSetting)
                        {
                            //if there is no customised setting, overwrite the application
                            //default with the settings requested by the caller
                            settings.LoggingLevel = level;
                            settings.LoggingMode = mode;
                        }
                        else
                        {
                            //if there are customised settings, overwrite the caller's settings
                            //with those from the customised settings file
                            level = settings.LoggingLevel;
                            mode = settings.LoggingMode;
                        }
                    }
                    catch (System.Runtime.Serialization.SerializationException error) { settingsError = error; }
                }

                //read in the max log size from the settings file or use the default if not available
                //int maxSize = settings != null ? settings.MaxLogSize : _maxSize;

                //create the logger
                logger = new LogUtility(productName, outputFolder, extension, _maxSize, level, mode);

                //if settings was found but could not be loaded, inform user
                if (settingsError != null)
                {
                    logger.AddException(string.Format("Error reading logging settings from {0}", source), settingsError);
                }

                //save the current settings for the user
                source = Path.Combine(tempRoot, CustomFolder);

                //only write the settings if the user had permissions to create a custom folder
                if (Directory.Exists(source) && (settings == null || noCustomSetting))
                {
                    settingsFile = (Path.Combine(source, SETTINGS_FILE));

                    settings = new LoggingSettings
                    {
                        LoggingLevel = level,
                        LoggingMode = mode
                    };

                    settings.SerializeToFile(settingsFile);
                }
            }

            return logger; 

            #endregion
        }