コード例 #1
0
        /// <summary>
        /// Write to the log.
        /// </summary>
        /// <param name="log">The class instance to log.</param>
        /// <param name="timestamp">The timestamp associated with the log.</param>
        /// <returns>true, if the logs have been flushed.</returns>
        public bool WriteLog(TLogClass log, DateTime timestamp)
        {
            DateTime logDate = timestamp.Date;

            if (_archiveLog == null)
            {
                _archiveLog = new ImageServerLogFile <TLogClass>(timestamp, _logDirectory, LogFileSize, _logType);
                Platform.Log(LogLevel.Info, "Starting archival of {0} logs for {1}", _logType, _archiveLog.FirstTimestamp.ToLongDateString());
            }

            if (logDate.Equals(_archiveLog.Date))
            {
                _archiveLog.Write(log, timestamp);
                if (_archiveLog.Stream.Length > LogFileSize)
                {
                    FlushLog();
                    return(true);
                }
                return(false);
            }

            // Flush the current log
            FlushLog();

            // Simple recursive call to rewrite, since the log has been flushed, will only go 1 deep
            // on the recursion because FlushLog set _archiveLog = null.
            WriteLog(log, timestamp);

            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Routine for flushing the log file to the correct zip file.
        /// </summary>
        public void FlushLog()
        {
            if (_archiveLog == null)
            {
                return;
            }

            Platform.Log(LogLevel.Info, "Flushing log of {0} for {1}", _logType, _archiveLog.FirstTimestamp.ToLongDateString());

            if (!Directory.Exists(_logDirectory))
            {
                Directory.CreateDirectory(_logDirectory);
            }
            if (!Directory.Exists(_archiveLog.ZipDirectory))
            {
                Directory.CreateDirectory(_archiveLog.ZipDirectory);
            }

            var zipService = Platform.GetService <IZipService>();

            using (var zipServiceWriter = zipService.OpenWrite(_archiveLog.ZipFile))
            {
                zipServiceWriter.ForceCompress = true;

                var comment =
                    String.Format("Log of {0} from {1} to {2}", _logType, _archiveLog.FirstTimestamp.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                                  _archiveLog.LastTimestamp.ToString("yyyy-MM-dd HH:mm:ss.fff"));

                zipServiceWriter.AddFileStream(_archiveLog.LogFileName, _archiveLog.Stream, comment);

                zipServiceWriter.Save();
            }

            _archiveLog.Dispose();
            _archiveLog = null;
        }