📄 loggingevent.cs
字号:
#region Copyright & License
//
// Copyright 2001-2005 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.Runtime.Serialization;
using System.Collections;
using System.IO;
#if (!NETCF)
using System.Security.Principal;
#endif
using log4net.Util;
using log4net.Repository;
namespace log4net.Core
{
/// <summary>
/// Portable data structure used by <see cref="LoggingEvent"/>
/// </summary>
/// <remarks>
/// <para>
/// Portable data structure used by <see cref="LoggingEvent"/>
/// </para>
/// </remarks>
/// <author>Nicko Cadell</author>
public struct LoggingEventData
{
#region Public Instance Fields
/// <summary>
/// The logger name.
/// </summary>
/// <remarks>
/// <para>
/// The logger name.
/// </para>
/// </remarks>
public string LoggerName;
/// <summary>
/// Level of logging event.
/// </summary>
/// <remarks>
/// <para>
/// Level of logging event. Level cannot be Serializable
/// because it is a flyweight. Due to its special serialization it
/// cannot be declared final either.
/// </para>
/// </remarks>
public Level Level;
/// <summary>
/// The application supplied message.
/// </summary>
/// <remarks>
/// <para>
/// The application supplied message of logging event.
/// </para>
/// </remarks>
public string Message;
/// <summary>
/// The name of thread
/// </summary>
/// <remarks>
/// <para>
/// The name of thread in which this logging event was generated
/// </para>
/// </remarks>
public string ThreadName;
/// <summary>
/// The time the event was logged
/// </summary>
/// <remarks>
/// <para>
/// The TimeStamp is stored in the local time zone for this computer.
/// </para>
/// </remarks>
public DateTime TimeStamp;
/// <summary>
/// Location information for the caller.
/// </summary>
/// <remarks>
/// <para>
/// Location information for the caller.
/// </para>
/// </remarks>
public LocationInfo LocationInfo;
/// <summary>
/// String representation of the user
/// </summary>
/// <remarks>
/// <para>
/// String representation of the user's windows name,
/// like DOMAIN\username
/// </para>
/// </remarks>
public string UserName;
/// <summary>
/// String representation of the identity.
/// </summary>
/// <remarks>
/// <para>
/// String representation of the current thread's principal identity.
/// </para>
/// </remarks>
public string Identity;
/// <summary>
/// The string representation of the exception
/// </summary>
/// <remarks>
/// <para>
/// The string representation of the exception
/// </para>
/// </remarks>
public string ExceptionString;
/// <summary>
/// String representation of the AppDomain.
/// </summary>
/// <remarks>
/// <para>
/// String representation of the AppDomain.
/// </para>
/// </remarks>
public string Domain;
/// <summary>
/// Additional event specific properties
/// </summary>
/// <remarks>
/// <para>
/// A logger or an appender may attach additional
/// properties to specific events. These properties
/// have a string key and an object value.
/// </para>
/// </remarks>
public PropertiesDictionary Properties;
#endregion Public Instance Fields
}
/// <summary>
/// Flags passed to the <see cref="LoggingEvent.Fix"/> property
/// </summary>
/// <remarks>
/// <para>
/// Flags passed to the <see cref="LoggingEvent.Fix"/> property
/// </para>
/// </remarks>
/// <author>Nicko Cadell</author>
[Flags] public enum FixFlags
{
/// <summary>
/// Fix the MDC
/// </summary>
[Obsolete("Replaced by composite Properties")]
Mdc = 0x01,
/// <summary>
/// Fix the NDC
/// </summary>
Ndc = 0x02,
/// <summary>
/// Fix the rendered message
/// </summary>
Message = 0x04,
/// <summary>
/// Fix the thread name
/// </summary>
ThreadName = 0x08,
/// <summary>
/// Fix the callers location information
/// </summary>
/// <remarks>
/// CAUTION: Very slow to generate
/// </remarks>
LocationInfo = 0x10,
/// <summary>
/// Fix the callers windows user name
/// </summary>
/// <remarks>
/// CAUTION: Slow to generate
/// </remarks>
UserName = 0x20,
/// <summary>
/// Fix the domain friendly name
/// </summary>
Domain = 0x40,
/// <summary>
/// Fix the callers principal name
/// </summary>
/// <remarks>
/// CAUTION: May be slow to generate
/// </remarks>
Identity = 0x80,
/// <summary>
/// Fix the exception text
/// </summary>
Exception = 0x100,
/// <summary>
/// Fix the event properties
/// </summary>
Properties = 0x200,
/// <summary>
/// No fields fixed
/// </summary>
None = 0x0,
/// <summary>
/// All fields fixed
/// </summary>
All = 0xFFFFFFF,
/// <summary>
/// Partial fields fixed
/// </summary>
/// <remarks>
/// <para>
/// This set of partial fields gives good performance. The following fields are fixed:
/// </para>
/// <list type="bullet">
/// <item><description><see cref="Message"/></description></item>
/// <item><description><see cref="ThreadName"/></description></item>
/// <item><description><see cref="Exception"/></description></item>
/// <item><description><see cref="Domain"/></description></item>
/// <item><description><see cref="Properties"/></description></item>
/// </list>
/// </remarks>
Partial = Message | ThreadName | Exception | Domain | Properties,
}
/// <summary>
/// The internal representation of logging events.
/// </summary>
/// <remarks>
/// <para>
/// When an affirmative decision is made to log then a
/// <see cref="LoggingEvent"/> instance is created. This instance
/// is passed around to the different log4net components.
/// </para>
/// <para>
/// This class is of concern to those wishing to extend log4net.
/// </para>
/// <para>
/// Some of the values in instances of <see cref="LoggingEvent"/>
/// are considered volatile, that is the values are correct at the
/// time the event is delivered to appenders, but will not be consistent
/// at any time afterwards. If an event is to be stored and then processed
/// at a later time these volatile values must be fixed by calling
/// <see cref="FixVolatileData()"/>. There is a performance penalty
/// for incurred by calling <see cref="FixVolatileData()"/> but it
/// is essential to maintaining data consistency.
/// </para>
/// </remarks>
/// <author>Nicko Cadell</author>
/// <author>Gert Driesen</author>
/// <author>Douglas de la Torre</author>
/// <author>Daniel Cazzulino</author>
#if !NETCF
[Serializable]
#endif
public class LoggingEvent
#if !NETCF
: ISerializable
#endif
{
#region Public Instance Constructors
/// <summary>
/// Initializes a new instance of the <see cref="LoggingEvent" /> class
/// from the supplied parameters.
/// </summary>
/// <param name="callerStackBoundaryDeclaringType">The declaring type of the method that is
/// the stack boundary into the logging system for this call.</param>
/// <param name="repository">The repository this event is logged in.</param>
/// <param name="loggerName">The name of the logger of this event.</param>
/// <param name="level">The level of this event.</param>
/// <param name="message">The message of this event.</param>
/// <param name="exception">The exception for this event.</param>
/// <remarks>
/// <para>
/// Except <see cref="TimeStamp"/>, <see cref="Level"/> and <see cref="LoggerName"/>,
/// all fields of <c>LoggingEvent</c> are filled when actually needed. Call
/// <see cref="FixVolatileData()"/> to cache all data locally
/// to prevent inconsistencies.
/// </para>
/// <para>This method is called by the log4net framework
/// to create a logging event.
/// </para>
/// </remarks>
public LoggingEvent(Type callerStackBoundaryDeclaringType, log4net.Repository.ILoggerRepository repository, string loggerName, Level level, object message, Exception exception)
{
m_callerStackBoundaryDeclaringType = callerStackBoundaryDeclaringType;
m_message = message;
m_repository = repository;
m_thrownException = exception;
m_data.LoggerName = loggerName;
m_data.Level = level;
// Store the event creation time
m_data.TimeStamp = DateTime.Now;
}
/// <summary>
/// Initializes a new instance of the <see cref="LoggingEvent" /> class
/// using specific data.
/// </summary>
/// <param name="callerStackBoundaryDeclaringType">The declaring type of the method that is
/// the stack boundary into the logging system for this call.</param>
/// <param name="repository">The repository this event is logged in.</param>
/// <param name="data">Data used to initialize the logging event.</param>
/// <param name="fixedData">The fields in the <paranref name="data"/> struct that have already been fixed.</param>
/// <remarks>
/// <para>
/// This constructor is provided to allow a <see cref="LoggingEvent" />
/// to be created independently of the log4net framework. This can
/// be useful if you require a custom serialization scheme.
/// </para>
/// <para>
/// Use the <see cref="GetLoggingEventData(FixFlags)"/> method to obtain an
/// instance of the <see cref="LoggingEventData"/> class.
/// </para>
/// <para>
/// The <paramref name="fixedData"/> parameter should be used to specify which fields in the
/// <paramref name="data"/> struct have been preset. Fields not specified in the <paramref name="fixedData"/>
/// will be captured from the environment if requested or fixed.
/// </para>
/// </remarks>
public LoggingEvent(Type callerStackBoundaryDeclaringType, log4net.Repository.ILoggerRepository repository, LoggingEventData data, FixFlags fixedData)
{
m_callerStackBoundaryDeclaringType = callerStackBoundaryDeclaringType;
m_repository = repository;
m_data = data;
m_fixFlags = fixedData;
}
/// <summary>
/// Initializes a new instance of the <see cref="LoggingEvent" /> class
/// using specific data.
/// </summary>
/// <param name="callerStackBoundaryDeclaringType">The declaring type of the method that is
/// the stack boundary into the logging system for this call.</param>
/// <param name="repository">The repository this event is logged in.</param>
/// <param name="data">Data used to initialize the logging event.</param>
/// <remarks>
/// <para>
/// This constructor is provided to allow a <see cref="LoggingEvent" />
/// to be created independently of the log4net framework. This can
/// be useful if you require a custom serialization scheme.
/// </para>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -