?? rollingfileappender.cs
字號:
#region Copyright & License
//
// Copyright 2001-2006 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using log4net.Util;
using log4net.Core;
namespace log4net.Appender
{
#if CONFIRM_WIN32_FILE_SHAREMODES
// The following sounds good, and I though it was the case, but after
// further testing on Windows I have not been able to confirm it.
/// On the Windows platform if another process has a write lock on the file
/// that is to be deleted, but allows shared read access to the file then the
/// file can be moved, but cannot be deleted. If the other process also allows
/// shared delete access to the file then the file will be deleted once that
/// process closes the file. If it is necessary to open the log file or any
/// of the backup files outside of this appender for either read or
/// write access please ensure that read and delete share modes are enabled.
#endif
/// <summary>
/// Appender that rolls log files based on size or date or both.
/// </summary>
/// <remarks>
/// <para>
/// RollingFileAppender can roll log files based on size or date or both
/// depending on the setting of the <see cref="RollingStyle"/> property.
/// When set to <see cref="RollingMode.Size"/> the log file will be rolled
/// once its size exceeds the <see cref="MaximumFileSize"/>.
/// When set to <see cref="RollingMode.Date"/> the log file will be rolled
/// once the date boundary specified in the <see cref="DatePattern"/> property
/// is crossed.
/// When set to <see cref="RollingMode.Composite"/> the log file will be
/// rolled once the date boundary specified in the <see cref="DatePattern"/> property
/// is crossed, but within a date boundary the file will also be rolled
/// once its size exceeds the <see cref="MaximumFileSize"/>.
/// When set to <see cref="RollingMode.Once"/> the log file will be rolled when
/// the appender is configured. This effectively means that the log file can be
/// rolled once per program execution.
/// </para>
/// <para>
/// A of few additional optional features have been added:
/// <list type="bullet">
/// <item>Attach date pattern for current log file <see cref="StaticLogFileName"/></item>
/// <item>Backup number increments for newer files <see cref="CountDirection"/></item>
/// <item>Infinite number of backups by file size <see cref="MaxSizeRollBackups"/></item>
/// </list>
/// </para>
///
/// <note>
/// <para>
/// For large or infinite numbers of backup files a <see cref="CountDirection"/>
/// greater than zero is highly recommended, otherwise all the backup files need
/// to be renamed each time a new backup is created.
/// </para>
/// <para>
/// When Date/Time based rolling is used setting <see cref="StaticLogFileName"/>
/// to <see langword="true"/> will reduce the number of file renamings to few or none.
/// </para>
/// </note>
///
/// <note type="caution">
/// <para>
/// Changing <see cref="StaticLogFileName"/> or <see cref="CountDirection"/> without clearing
/// the log file directory of backup files will cause unexpected and unwanted side effects.
/// </para>
/// </note>
///
/// <para>
/// If Date/Time based rolling is enabled this appender will attempt to roll existing files
/// in the directory without a Date/Time tag based on the last write date of the base log file.
/// The appender only rolls the log file when a message is logged. If Date/Time based rolling
/// is enabled then the appender will not roll the log file at the Date/Time boundary but
/// at the point when the next message is logged after the boundary has been crossed.
/// </para>
///
/// <para>
/// The <see cref="RollingFileAppender"/> extends the <see cref="FileAppender"/> and
/// has the same behavior when opening the log file.
/// The appender will first try to open the file for writing when <see cref="ActivateOptions"/>
/// is called. This will typically be during configuration.
/// If the file cannot be opened for writing the appender will attempt
/// to open the file again each time a message is logged to the appender.
/// If the file cannot be opened for writing when a message is logged then
/// the message will be discarded by this appender.
/// </para>
/// <para>
/// When rolling a backup file necessitates deleting an older backup file the
/// file to be deleted is moved to a temporary name before being deleted.
/// </para>
///
/// <note type="caution">
/// <para>
/// A maximum number of backup files when rolling on date/time boundaries is not supported.
/// </para>
/// </note>
/// </remarks>
/// <author>Nicko Cadell</author>
/// <author>Gert Driesen</author>
/// <author>Aspi Havewala</author>
/// <author>Douglas de la Torre</author>
/// <author>Edward Smit</author>
public class RollingFileAppender : FileAppender
{
#region Public Enums
/// <summary>
/// Style of rolling to use
/// </summary>
/// <remarks>
/// <para>
/// Style of rolling to use
/// </para>
/// </remarks>
public enum RollingMode
{
/// <summary>
/// Roll files once per program execution
/// </summary>
/// <remarks>
/// <para>
/// Roll files once per program execution.
/// Well really once each time this appender is
/// configured.
/// </para>
/// <para>
/// Setting this option also sets <c>AppendToFile</c> to
/// <c>false</c> on the <c>RollingFileAppender</c>, otherwise
/// this appender would just be a normal file appender.
/// </para>
/// </remarks>
Once = 0,
/// <summary>
/// Roll files based only on the size of the file
/// </summary>
Size = 1,
/// <summary>
/// Roll files based only on the date
/// </summary>
Date = 2,
/// <summary>
/// Roll files based on both the size and date of the file
/// </summary>
Composite = 3
}
#endregion
#region Protected Enums
/// <summary>
/// The code assumes that the following 'time' constants are in a increasing sequence.
/// </summary>
/// <remarks>
/// <para>
/// The code assumes that the following 'time' constants are in a increasing sequence.
/// </para>
/// </remarks>
protected enum RollPoint
{
/// <summary>
/// Roll the log not based on the date
/// </summary>
InvalidRollPoint =-1,
/// <summary>
/// Roll the log for each minute
/// </summary>
TopOfMinute = 0,
/// <summary>
/// Roll the log for each hour
/// </summary>
TopOfHour = 1,
/// <summary>
/// </summary>
HalfDay = 2,
/// <summary>
/// Roll the log each day (midnight)
/// </summary>
TopOfDay = 3,
/// <summary>
/// Roll the log each week
/// </summary>
TopOfWeek = 4,
/// <summary>
/// Roll the log each month
/// </summary>
TopOfMonth = 5
}
#endregion Protected Enums
#region Public Instance Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RollingFileAppender" /> class.
/// </summary>
/// <remarks>
/// <para>
/// Default constructor.
/// </para>
/// </remarks>
public RollingFileAppender()
{
m_dateTime = new DefaultDateTime();
}
#endregion Public Instance Constructors
#region Public Instance Properties
/// <summary>
/// Gets or sets the date pattern to be used for generating file names
/// when rolling over on date.
/// </summary>
/// <value>
/// The date pattern to be used for generating file names when rolling
/// over on date.
/// </value>
/// <remarks>
/// <para>
/// Takes a string in the same format as expected by
/// <see cref="log4net.DateFormatter.SimpleDateFormatter" />.
/// </para>
/// <para>
/// This property determines the rollover schedule when rolling over
/// on date.
/// </para>
/// </remarks>
public string DatePattern
{
get { return m_datePattern; }
set { m_datePattern = value; }
}
/// <summary>
/// Gets or sets the maximum number of backup files that are kept before
/// the oldest is erased.
/// </summary>
/// <value>
/// The maximum number of backup files that are kept before the oldest is
/// erased.
/// </value>
/// <remarks>
/// <para>
/// If set to zero, then there will be no backup files and the log file
/// will be truncated when it reaches <see cref="MaxFileSize"/>.
/// </para>
/// <para>
/// If a negative number is supplied then no deletions will be made. Note
/// that this could result in very slow performance as a large number of
/// files are rolled over unless <see cref="CountDirection"/> is used.
/// </para>
/// <para>
/// The maximum applies to <b>each</b> time based group of files and
/// <b>not</b> the total.
/// </para>
/// </remarks>
public int MaxSizeRollBackups
{
get { return m_maxSizeRollBackups; }
set { m_maxSizeRollBackups = value; }
}
/// <summary>
/// Gets or sets the maximum size that the output file is allowed to reach
/// before being rolled over to backup files.
/// </summary>
/// <value>
/// The maximum size in bytes that the output file is allowed to reach before being
/// rolled over to backup files.
/// </value>
/// <remarks>
/// <para>
/// This property is equivalent to <see cref="MaximumFileSize"/> except
/// that it is required for differentiating the setter taking a
/// <see cref="long"/> argument from the setter taking a <see cref="string"/>
/// argument.
/// </para>
/// <para>
/// The default maximum file size is 10MB (10*1024*1024).
/// </para>
/// </remarks>
public long MaxFileSize
{
get { return m_maxFileSize; }
set { m_maxFileSize = value; }
}
/// <summary>
/// Gets or sets the maximum size that the output file is allowed to reach
/// before being rolled over to backup files.
/// </summary>
/// <value>
/// The maximum size that the output file is allowed to reach before being
/// rolled over to backup files.
/// </value>
/// <remarks>
/// <para>
/// This property allows you to specify the maximum size with the
/// suffixes "KB", "MB" or "GB" so that the size is interpreted being
/// expressed respectively in kilobytes, megabytes or gigabytes.
/// </para>
/// <para>
/// For example, the value "10KB" will be interpreted as 10240 bytes.
/// </para>
/// <para>
/// The default maximum file size is 10MB.
/// </para>
/// <para>
/// If you have the option to set the maximum file size programmatically
/// consider using the <see cref="MaxFileSize"/> property instead as this
/// allows you to set the size in bytes as a <see cref="Int64"/>.
/// </para>
/// </remarks>
public string MaximumFileSize
{
get { return m_maxFileSize.ToString(NumberFormatInfo.InvariantInfo); }
set { m_maxFileSize = OptionConverter.ToFileSize(value, m_maxFileSize + 1); }
}
/// <summary>
/// Gets or sets the rolling file count direction.
/// </summary>
/// <value>
/// The rolling file count direction.
/// </value>
/// <remarks>
/// <para>
/// Indicates if the current file is the lowest numbered file or the
/// highest numbered file.
/// </para>
/// <para>
/// By default newer files have lower numbers (<see cref="CountDirection" /> < 0),
/// i.e. log.1 is most recent, log.5 is the 5th backup, etc...
/// </para>
/// <para>
/// <see cref="CountDirection" /> >= 0 does the opposite i.e.
/// log.1 is the first backup made, log.5 is the 5th backup made, etc.
/// For infinite backups use <see cref="CountDirection" /> >= 0 to reduce
/// rollover costs.
/// </para>
/// <para>The default file count direction is -1.</para>
/// </remarks>
public int CountDirection
{
get { return m_countDirection; }
set { m_countDirection = value; }
}
/// <summary>
/// Gets or sets the rolling style.
/// </summary>
/// <value>The rolling style.</value>
/// <remarks>
/// <para>
/// The default rolling style is <see cref="RollingMode.Composite" />.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -