📄 loggingevent.cs
字号:
/// <c>false</c>.
/// </para>
/// <para>
/// See <see cref="FixVolatileData(bool)"/> for more
/// information.
/// </para>
/// </remarks>
[Obsolete("Use Fix property")]
public void FixVolatileData()
{
Fix = FixFlags.All;
}
/// <summary>
/// Fixes instance fields that hold volatile data.
/// </summary>
/// <param name="fastButLoose">Set to <c>true</c> to not fix data that takes a long time to fix.</param>
/// <remarks>
/// <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>
/// <para>
/// The <paramref name="fastButLoose"/> param controls the data that
/// is fixed. Some of the data that can be fixed takes a long time to
/// generate, therefore if you do not require those settings to be fixed
/// they can be ignored by setting the <paramref name="fastButLoose"/> param
/// to <c>true</c>. This setting will ignore the <see cref="LocationInformation"/>
/// and <see cref="UserName"/> settings.
/// </para>
/// <para>
/// Set <paramref name="fastButLoose"/> to <c>false</c> to ensure that all
/// settings are fixed.
/// </para>
/// </remarks>
[Obsolete("Use Fix property")]
public void FixVolatileData(bool fastButLoose)
{
if (fastButLoose)
{
Fix = FixFlags.Partial;
}
else
{
Fix = FixFlags.All;
}
}
/// <summary>
/// Fix the fields specified by the <see cref="FixFlags"/> parameter
/// </summary>
/// <param name="flags">the fields to fix</param>
/// <remarks>
/// <para>
/// Only fields specified in the <paramref name="flags"/> will be fixed.
/// Fields will not be fixed if they have previously been fixed.
/// It is not possible to 'unfix' a field.
/// </para>
/// </remarks>
protected void FixVolatileData(FixFlags flags)
{
object forceCreation = null;
//Unlock the cache so that new values can be stored
//This may not be ideal if we are no longer in the correct context
//and someone calls fix.
m_cacheUpdatable=true;
// determine the flags that we are actually fixing
FixFlags updateFlags = (FixFlags)((flags ^ m_fixFlags) & flags);
if (updateFlags > 0)
{
if ((updateFlags & FixFlags.Message) != 0)
{
// Force the message to be rendered
forceCreation = this.RenderedMessage;
m_fixFlags |= FixFlags.Message;
}
if ((updateFlags & FixFlags.ThreadName) != 0)
{
// Grab the thread name
forceCreation = this.ThreadName;
m_fixFlags |= FixFlags.ThreadName;
}
if ((updateFlags & FixFlags.LocationInfo) != 0)
{
// Force the location information to be loaded
forceCreation = this.LocationInformation;
m_fixFlags |= FixFlags.LocationInfo;
}
if ((updateFlags & FixFlags.UserName) != 0)
{
// Grab the user name
forceCreation = this.UserName;
m_fixFlags |= FixFlags.UserName;
}
if ((updateFlags & FixFlags.Domain) != 0)
{
// Grab the domain name
forceCreation = this.Domain;
m_fixFlags |= FixFlags.Domain;
}
if ((updateFlags & FixFlags.Identity) != 0)
{
// Grab the identity
forceCreation = this.Identity;
m_fixFlags |= FixFlags.Identity;
}
if ((updateFlags & FixFlags.Exception) != 0)
{
// Force the exception text to be loaded
forceCreation = GetExceptionString();
m_fixFlags |= FixFlags.Exception;
}
if ((updateFlags & FixFlags.Properties) != 0)
{
CacheProperties();
m_fixFlags |= FixFlags.Properties;
}
}
// avoid warning CS0219
if (forceCreation != null)
{
}
//Finaly lock everything we've cached.
m_cacheUpdatable=false;
}
#endregion Public Instance Methods
#region Protected Instance Methods
private void CreateCompositeProperties()
{
m_compositeProperties = new CompositeProperties();
if (m_eventProperties != null)
{
m_compositeProperties.Add(m_eventProperties);
}
#if !NETCF
PropertiesDictionary logicalThreadProperties = LogicalThreadContext.Properties.GetProperties(false);
if (logicalThreadProperties != null)
{
m_compositeProperties.Add(logicalThreadProperties);
}
#endif
PropertiesDictionary threadProperties = ThreadContext.Properties.GetProperties(false);
if (threadProperties != null)
{
m_compositeProperties.Add(threadProperties);
}
// TODO: Add Repository Properties
m_compositeProperties.Add(GlobalContext.Properties.GetReadOnlyProperties());
}
private void CacheProperties()
{
if (m_data.Properties == null && this.m_cacheUpdatable)
{
if (m_compositeProperties == null)
{
CreateCompositeProperties();
}
PropertiesDictionary flattenedProperties = m_compositeProperties.Flatten();
PropertiesDictionary fixedProperties = new PropertiesDictionary();
// Validate properties
foreach(DictionaryEntry entry in flattenedProperties)
{
string key = entry.Key as string;
if (key != null)
{
object val = entry.Value;
// Fix any IFixingRequired objects
IFixingRequired fixingRequired = val as IFixingRequired;
if (fixingRequired != null)
{
val = fixingRequired.GetFixedObject();
}
// Strip keys with null values
if (val != null)
{
fixedProperties[key] = val;
}
}
}
m_data.Properties = fixedProperties;
}
}
/// <summary>
/// Lookup a composite property in this event
/// </summary>
/// <param name="key">the key for the property to lookup</param>
/// <returns>the value for the property</returns>
/// <remarks>
/// <para>
/// This event has composite properties that combine together properties from
/// several different contexts in the following order:
/// <list type="definition">
/// <item>
/// <term>this events properties</term>
/// <description>
/// This event has <see cref="Properties"/> that can be set. These
/// properties are specific to this event only.
/// </description>
/// </item>
/// <item>
/// <term>the thread properties</term>
/// <description>
/// The <see cref="ThreadContext.Properties"/> that are set on the current
/// thread. These properties are shared by all events logged on this thread.
/// </description>
/// </item>
/// <item>
/// <term>the global properties</term>
/// <description>
/// The <see cref="GlobalContext.Properties"/> that are set globally. These
/// properties are shared by all the threads in the AppDomain.
/// </description>
/// </item>
/// </list>
/// </para>
/// </remarks>
public object LookupProperty(string key)
{
if (m_data.Properties != null)
{
return m_data.Properties[key];
}
if (m_compositeProperties == null)
{
CreateCompositeProperties();
}
return m_compositeProperties[key];
}
/// <summary>
/// Get all the composite properties in this event
/// </summary>
/// <returns>the <see cref="PropertiesDictionary"/> containing all the properties</returns>
/// <remarks>
/// <para>
/// See <see cref="LookupProperty"/> for details of the composite properties
/// stored by the event.
/// </para>
/// <para>
/// This method returns a single <see cref="PropertiesDictionary"/> containing all the
/// properties defined for this event.
/// </para>
/// </remarks>
public PropertiesDictionary GetProperties()
{
if (m_data.Properties != null)
{
return m_data.Properties;
}
if (m_compositeProperties == null)
{
CreateCompositeProperties();
}
return m_compositeProperties.Flatten();
}
#endregion Public Instance Methods
#region Private Instance Fields
/// <summary>
/// The internal logging event data.
/// </summary>
private LoggingEventData m_data;
/// <summary>
/// The internal logging event data.
/// </summary>
private CompositeProperties m_compositeProperties;
/// <summary>
/// The internal logging event data.
/// </summary>
private PropertiesDictionary m_eventProperties;
/// <summary>
/// The fully qualified Type of the calling
/// logger class in the stack frame (i.e. the declaring type of the method).
/// </summary>
private readonly Type m_callerStackBoundaryDeclaringType;
/// <summary>
/// The application supplied message of logging event.
/// </summary>
private readonly object m_message;
/// <summary>
/// The exception that was thrown.
/// </summary>
/// <remarks>
/// This is not serialized. The string representation
/// is serialized instead.
/// </remarks>
private readonly Exception m_thrownException;
/// <summary>
/// The repository that generated the logging event
/// </summary>
/// <remarks>
/// This is not serialized.
/// </remarks>
private ILoggerRepository m_repository = null;
/// <summary>
/// The fix state for this event
/// </summary>
/// <remarks>
/// These flags indicate which fields have been fixed.
/// Not serialized.
/// </remarks>
private FixFlags m_fixFlags = FixFlags.None;
/// <summary>
/// Indicated that the internal cache is updateable (ie not fixed)
/// </summary>
/// <remarks>
/// This is a seperate flag to m_fixFlags as it allows incrementel fixing and simpler
/// changes in the caching strategy.
/// </remarks>
private bool m_cacheUpdatable = true;
#endregion Private Instance Fields
#region Constants
/// <summary>
/// The key into the Properties map for the host name value.
/// </summary>
public const string HostNameProperty = "log4net:HostName";
/// <summary>
/// The key into the Properties map for the thread identity value.
/// </summary>
public const string IdentityProperty = "log4net:Identity";
/// <summary>
/// The key into the Properties map for the user name value.
/// </summary>
public const string UserNameProperty = "log4net:UserName";
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -