コード例 #1
0
            private void PerformRoll(DateTime rollDateTime)
            {
                string actualFileName = ((FileStream)((StreamWriter)_owner.Writer).BaseStream).Name;

                string archiveFileName = ComputeArchiveFileName(actualFileName, rollDateTime);

                _owner.Writer.Close();
                SafeMove(actualFileName, archiveFileName, rollDateTime);
                PurgeArchivedFiles(actualFileName);

                // update writer - let TWTL open the file as needed to keep consistency
                _owner.Writer     = null;
                _tallyWriter      = null;
                _nextRollDateTime = null;
                UpdateRollingInformationIfNecessary();
            }
コード例 #2
0
            private bool UpdateRollingInformationIfNecessary()
            {
                // replace writer with the tally keeping version if necessary for size rolling
                if (_owner._rollSize > 0 && _tallyWriter == null)
                {
                    var currentWriter = _owner.Writer as StreamWriter;
                    if (currentWriter == null)
                    {
                        // couldn't acquire the writer - abort
                        return(false);
                    }
                    String actualFileName = ((FileStream)currentWriter.BaseStream).Name;

                    currentWriter.Close();

                    try {
                        FileStream fileStream = File.Open(actualFileName, FileMode.Append, FileAccess.Write, FileShare.Read);
                        _tallyWriter = new TallyStreamWriter(fileStream);
                    }
                    catch (Exception) {
                        // there's a slight chance of error here - abort if this occurs and just let TWTL handle it without attempting to roll
                        return(false);
                    }

                    _owner.Writer = _tallyWriter;
                }

                // compute the next roll date if necessary
                if (_owner._rollInterval != RollInterval.None && _nextRollDateTime == null)
                {
                    try {
                        // casting should be safe at this point - only file stream writers can be the writers for the owner trace listener.
                        // it should also happen rarely
                        _nextRollDateTime = CalculateNextRollDate(
                            File.GetCreationTime(((FileStream)((StreamWriter)_owner.Writer).BaseStream).Name));
                    }
                    catch (Exception) {
                        _nextRollDateTime = DateTime.MaxValue; // disable rolling if not date could be retrieved.
                        // there's a slight chance of error here - abort if this occurs and just let TWTL handle it without attempting to roll
                        return(false);
                    }
                }

                return(true);
            }