?? logger.cs
字號:
/// </value>
/// <remarks>
/// <para>
/// The name of this logger
/// </para>
/// </remarks>
virtual public string Name
{
get { return m_name; }
}
/// <summary>
/// This generic form is intended to be used by wrappers.
/// </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="level">The level of the message to be logged.</param>
/// <param name="message">The message object to log.</param>
/// <param name="exception">The exception to log, including its stack trace.</param>
/// <remarks>
/// <para>
/// Generate a logging event for the specified <paramref name="level"/> using
/// the <paramref name="message"/> and <paramref name="exception"/>.
/// </para>
/// <para>
/// This method must not throw any exception to the caller.
/// </para>
/// </remarks>
virtual public void Log(Type callerStackBoundaryDeclaringType, Level level, object message, Exception exception)
{
try
{
if (IsEnabledFor(level))
{
ForcedLog((callerStackBoundaryDeclaringType != null) ? callerStackBoundaryDeclaringType : ThisDeclaringType, level, message, exception);
}
}
catch (Exception ex)
{
log4net.Util.LogLog.Error("Log: Exception while logging", ex);
}
catch
{
log4net.Util.LogLog.Error("Log: Exception while logging");
}
}
/// <summary>
/// This is the most generic printing method that is intended to be used
/// by wrappers.
/// </summary>
/// <param name="logEvent">The event being logged.</param>
/// <remarks>
/// <para>
/// Logs the specified logging event through this logger.
/// </para>
/// <para>
/// This method must not throw any exception to the caller.
/// </para>
/// </remarks>
virtual public void Log(LoggingEvent logEvent)
{
try
{
if (logEvent != null)
{
if (IsEnabledFor(logEvent.Level))
{
ForcedLog(logEvent);
}
}
}
catch (Exception ex)
{
log4net.Util.LogLog.Error("Log: Exception while logging", ex);
}
catch
{
log4net.Util.LogLog.Error("Log: Exception while logging");
}
}
/// <summary>
/// Checks if this logger is enabled for a given <see cref="Level"/> passed as parameter.
/// </summary>
/// <param name="level">The level to check.</param>
/// <returns>
/// <c>true</c> if this logger is enabled for <c>level</c>, otherwise <c>false</c>.
/// </returns>
/// <remarks>
/// <para>
/// Test if this logger is going to log events of the specified <paramref name="level"/>.
/// </para>
/// <para>
/// This method must not throw any exception to the caller.
/// </para>
/// </remarks>
virtual public bool IsEnabledFor(Level level)
{
try
{
if (level != null)
{
if (m_hierarchy.IsDisabled(level))
{
return false;
}
return level >= this.EffectiveLevel;
}
}
catch (Exception ex)
{
log4net.Util.LogLog.Error("Log: Exception while logging", ex);
}
catch
{
log4net.Util.LogLog.Error("Log: Exception while logging");
}
return false;
}
/// <summary>
/// Gets the <see cref="ILoggerRepository"/> where this
/// <c>Logger</c> instance is attached to.
/// </summary>
/// <value>
/// The <see cref="ILoggerRepository" /> that this logger belongs to.
/// </value>
/// <remarks>
/// <para>
/// Gets the <see cref="ILoggerRepository"/> where this
/// <c>Logger</c> instance is attached to.
/// </para>
/// </remarks>
public ILoggerRepository Repository
{
get { return m_hierarchy; }
}
#endregion Implementation of ILogger
/// <summary>
/// Deliver the <see cref="LoggingEvent"/> to the attached appenders.
/// </summary>
/// <param name="loggingEvent">The event to log.</param>
/// <remarks>
/// <para>
/// Call the appenders in the hierarchy starting at
/// <c>this</c>. If no appenders could be found, emit a
/// warning.
/// </para>
/// <para>
/// This method calls all the appenders inherited from the
/// hierarchy circumventing any evaluation of whether to log or not
/// to log the particular log request.
/// </para>
/// </remarks>
virtual protected void CallAppenders(LoggingEvent loggingEvent)
{
if (loggingEvent == null)
{
throw new ArgumentNullException("loggingEvent");
}
int writes = 0;
for(Logger c=this; c != null; c=c.m_parent)
{
if (c.m_appenderAttachedImpl != null)
{
// Protected against simultaneous call to addAppender, removeAppender,...
c.m_appenderLock.AcquireReaderLock();
try
{
if (c.m_appenderAttachedImpl != null)
{
writes += c.m_appenderAttachedImpl.AppendLoopOnAppenders(loggingEvent);
}
}
finally
{
c.m_appenderLock.ReleaseReaderLock();
}
}
if (!c.m_additive)
{
break;
}
}
// No appenders in hierarchy, warn user only once.
//
// Note that by including the AppDomain values for the currently running
// thread, it becomes much easier to see which application the warning
// is from, which is especially helpful in a multi-AppDomain environment
// (like IIS with multiple VDIRS). Without this, it can be difficult
// or impossible to determine which .config file is missing appender
// definitions.
//
if (!m_hierarchy.EmittedNoAppenderWarning && writes == 0)
{
LogLog.Debug("Logger: No appenders could be found for logger [" + Name + "] repository [" + Repository.Name + "]");
LogLog.Debug("Logger: Please initialize the log4net system properly.");
try
{
LogLog.Debug("Logger: Current AppDomain context information: ");
LogLog.Debug("Logger: BaseDirectory : " + SystemInfo.ApplicationBaseDirectory);
#if !NETCF
LogLog.Debug("Logger: FriendlyName : " + AppDomain.CurrentDomain.FriendlyName);
LogLog.Debug("Logger: DynamicDirectory: " + AppDomain.CurrentDomain.DynamicDirectory);
#endif
}
catch(System.Security.SecurityException)
{
// Insufficient permissions to display info from the AppDomain
}
m_hierarchy.EmittedNoAppenderWarning = true;
}
}
/// <summary>
/// Closes all attached appenders implementing the <see cref="IAppenderAttachable"/> interface.
/// </summary>
/// <remarks>
/// <para>
/// Used to ensure that the appenders are correctly shutdown.
/// </para>
/// </remarks>
virtual public void CloseNestedAppenders()
{
m_appenderLock.AcquireWriterLock();
try
{
if (m_appenderAttachedImpl != null)
{
AppenderCollection appenders = m_appenderAttachedImpl.Appenders;
foreach(IAppender appender in appenders)
{
if (appender is IAppenderAttachable)
{
appender.Close();
}
}
}
}
finally
{
m_appenderLock.ReleaseWriterLock();
}
}
/// <summary>
/// This is the most generic printing method. This generic form is intended to be used by wrappers
/// </summary>
/// <param name="level">The level of the message to be logged.</param>
/// <param name="message">The message object to log.</param>
/// <param name="exception">The exception to log, including its stack trace.</param>
/// <remarks>
/// <para>
/// Generate a logging event for the specified <paramref name="level"/> using
/// the <paramref name="message"/>.
/// </para>
/// </remarks>
virtual public void Log(Level level, object message, Exception exception)
{
if (IsEnabledFor(level))
{
ForcedLog(ThisDeclaringType, level, message, exception);
}
}
/// <summary>
/// Creates a new logging event and logs the event without further checks.
/// </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="level">The level of the message to be logged.</param>
/// <param name="message">The message object to log.</param>
/// <param name="exception">The exception to log, including its stack trace.</param>
/// <remarks>
/// <para>
/// Generates a logging event and delivers it to the attached
/// appenders.
/// </para>
/// </remarks>
virtual protected void ForcedLog(Type callerStackBoundaryDeclaringType, Level level, object message, Exception exception)
{
CallAppenders(new LoggingEvent(callerStackBoundaryDeclaringType, this.Hierarchy, this.Name, level, message, exception));
}
/// <summary>
/// Creates a new logging event and logs the event without further checks.
/// </summary>
/// <param name="logEvent">The event being logged.</param>
/// <remarks>
/// <para>
/// Delivers the logging event to the attached appenders.
/// </para>
/// </remarks>
virtual protected void ForcedLog(LoggingEvent logEvent)
{
// The logging event may not have been created by this logger
// the Repository may not be correctly set on the event. This
// is required for the appenders to correctly lookup renderers etc...
logEvent.EnsureRepository(this.Hierarchy);
CallAppenders(logEvent);
}
#region Private Static Fields
/// <summary>
/// The fully qualified type of the Logger class.
/// </summary>
private readonly static Type ThisDeclaringType = typeof(Logger);
#endregion Private Static Fields
#region Private Instance Fields
/// <summary>
/// The name of this logger.
/// </summary>
private readonly string m_name;
/// <summary>
/// The assigned level of this logger.
/// </summary>
/// <remarks>
/// <para>
/// The <c>level</c> variable need not be
/// assigned a value in which case it is inherited
/// form the hierarchy.
/// </para>
/// </remarks>
private Level m_level;
/// <summary>
/// The parent of this logger.
/// </summary>
/// <remarks>
/// <para>
/// The parent of this logger.
/// All loggers have at least one ancestor which is the root logger.
/// </para>
/// </remarks>
private Logger m_parent;
/// <summary>
/// Loggers need to know what Hierarchy they are in.
/// </summary>
/// <remarks>
/// <para>
/// Loggers need to know what Hierarchy they are in.
/// The hierarchy that this logger is a member of is stored
/// here.
/// </para>
/// </remarks>
private Hierarchy m_hierarchy;
/// <summary>
/// Helper implementation of the <see cref="IAppenderAttachable"/> interface
/// </summary>
private log4net.Util.AppenderAttachedImpl m_appenderAttachedImpl;
/// <summary>
/// Flag indicating if child loggers inherit their parents appenders
/// </summary>
/// <remarks>
/// <para>
/// Additivity is set to true by default, that is children inherit
/// the appenders of their ancestors by default. If this variable is
/// set to <c>false</c> then the appenders found in the
/// ancestors of this logger are not used. However, the children
/// of this logger will inherit its appenders, unless the children
/// have their additivity flag set to <c>false</c> too. See
/// the user manual for more details.
/// </para>
/// </remarks>
private bool m_additive = true;
/// <summary>
/// Lock to protect AppenderAttachedImpl variable m_appenderAttachedImpl
/// </summary>
private readonly ReaderWriterLock m_appenderLock = new ReaderWriterLock();
#endregion
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -