Esempio n. 1
0
        /// <summary>
        /// Opens the log file for the current application.
        /// </summary>
        /// <param name="logFile">Log file for the current application; if null or string.Empty use the default log file.</param>
        /// <param name="loggerSetup">LoggerSetup info for the logger instance; if null use the default LoggerSetup info.</param>
        /// <returns>Logger instance.</returns>
        public static Logger Open(string logFile, LoggerSetup loggerSetup)
        {
            LogConfig logConfig = new LogConfig();

            if (!string.IsNullOrEmpty(logFile))
            {
                logConfig.LogFile = logFile;
            }

            if (loggerSetup != null)
            {
                logConfig.LoggerSetup = loggerSetup;
            }

            int key = logConfig.GetHashCode();

            if (LoggerDictionary.ContainsKey(key))
            {
                return(LoggerDictionary[key]);
            }

            lock (SyncRoot)
            {
                if (LoggerDictionary.ContainsKey(key))
                {
                    return(LoggerDictionary[key]);
                }
                else
                {
                    Logger result = new Logger(logConfig);

                    LoggerDictionary.Add(key, result);

                    return(result);
                }
            }
        }
        public static LogConfig GetLogConfig(string configFile, bool throwOnError = false)
        {
            if (string.IsNullOrEmpty(configFile) && throwOnError)
            {
                throw new ArgumentNullException("configFile", "The specified file name is null or empty.");
            }

            LogConfig result = new LogConfig();

            if (File.Exists(configFile))
            {
                try
                {
                    XmlDocument xmlDocument = new XmlDocument();

                    xmlDocument.Load(configFile);

                    XmlNode logConfigNode = xmlDocument.SelectSingleNode("descendant-or-self::LogConfig");

                    if (logConfigNode == null)
                    {
                        XmlNode loggerSetupNode = xmlDocument.SelectSingleNode("descendant-or-self::LoggerSetup");

                        if (loggerSetupNode != null)
                        {
                            XmlSerializer xmlSerializer = new XmlSerializer(typeof(LoggerSetup));

                            using (StringReader stringReader = new StringReader(loggerSetupNode.OuterXml))
                            {
                                result.LoggerSetup = (LoggerSetup)xmlSerializer.Deserialize(stringReader);
                            }
                        }

                        return(result);
                    }

                    XmlNode logFileNode = logConfigNode.SelectSingleNode("descendant::LogFile");

                    if (logFileNode != null)
                    {
                        result.LogFile = logFileNode.InnerText;
                    }

                    XmlNode logConfigLoggerSetupNode = logConfigNode.SelectSingleNode("descendant::LoggerSetup");

                    if (logConfigLoggerSetupNode != null)
                    {
                        XmlSerializer xmlSerializer = new XmlSerializer(typeof(LoggerSetup));

                        using (StringReader stringReader = new StringReader(logConfigLoggerSetupNode.OuterXml))
                        {
                            result.LoggerSetup = (LoggerSetup)xmlSerializer.Deserialize(stringReader);
                        }
                    }
                }
                catch (Exception e)
                {
                    InternalLogger.Log(e);

                    if (throwOnError)
                    {
                        throw;
                    }
                }
            }
            else
            {
                if (throwOnError)
                {
                    throw new FileNotFoundException("The specified file does not exist.", configFile);
                }
            }

            return(result);
        }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LogConfig"/> class.
 /// </summary>
 /// <param name="logConfig">The log configuration.</param>
 private LogConfig(LogConfig logConfig)
 {
     this.LogFile     = logConfig.LogFile;
     this.LoggerSetup = logConfig.LoggerSetup.Clone();
 }
Esempio n. 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Logger" /> class.
 /// </summary>
 /// <param name="logConfig">LogConfig instance.</param>
 internal Logger(LogConfig logConfig)
     : this(logConfig.LogFile, logConfig.LoggerSetup)
 {
 }
Esempio n. 5
0
 internal Logger(LogConfig logConfig, string configFile = null)
     : this(logConfig.LogFile, logConfig.LoggerSetup, configFile)
 {
 }