#region Apache License // // Licensed to the Apache Software Foundation (ASF) under one or more // contributor license agreements. See the NOTICE file distributed with // this work for additional information regarding copyright ownership. // The ASF licenses this file to you 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 log4net.Core; namespace log4net.Util { /// /// Implements log4net's default error handling policy which consists /// of emitting a message for the first error in an appender and /// ignoring all subsequent errors. /// /// /// /// The error message is processed using the LogLog sub-system by default. /// /// /// This policy aims at protecting an otherwise working application /// from being flooded with error messages when logging fails. /// /// /// Nicko Cadell /// Gert Driesen /// Ron Grabowski public class OnlyOnceErrorHandler : IErrorHandler { #region Public Instance Constructors /// /// Default Constructor /// /// /// /// Initializes a new instance of the class. /// /// public OnlyOnceErrorHandler() { m_prefix = ""; } /// /// Constructor /// /// The prefix to use for each message. /// /// /// Initializes a new instance of the class /// with the specified prefix. /// /// public OnlyOnceErrorHandler(string prefix) { m_prefix = prefix; } #endregion Public Instance Constructors #region Public Instance Methods /// /// Reset the error handler back to its initial disabled state. /// public void Reset() { m_enabledDate = DateTime.MinValue; m_errorCode = ErrorCode.GenericFailure; m_exception = null; m_message = null; m_firstTime = true; } #region Implementation of IErrorHandler /// /// Log an Error /// /// The error message. /// The exception. /// The internal error code. /// /// /// Invokes if and only if this is the first error or the first error after has been called. /// /// public void Error(string message, Exception e, ErrorCode errorCode) { if (m_firstTime) { FirstError(message, e, errorCode); } } /// /// Log the very first error /// /// The error message. /// The exception. /// The internal error code. /// /// /// Sends the error information to 's Error method. /// /// public virtual void FirstError(string message, Exception e, ErrorCode errorCode) { m_enabledDate = DateTime.Now; m_errorCode = errorCode; m_exception = e; m_message = message; m_firstTime = false; if (LogLog.InternalDebugging && !LogLog.QuietMode) { LogLog.Error(declaringType, "[" + m_prefix + "] ErrorCode: " + errorCode.ToString() + ". " + message, e); } } /// /// Log an Error /// /// The error message. /// The exception. /// /// /// Invokes if and only if this is the first error or the first error after has been called. /// /// public void Error(string message, Exception e) { Error(message, e, ErrorCode.GenericFailure); } /// /// Log an error /// /// The error message. /// /// /// Invokes if and only if this is the first error or the first error after has been called. /// /// public void Error(string message) { Error(message, null, ErrorCode.GenericFailure); } #endregion Implementation of IErrorHandler #endregion #region Public Instance Properties /// /// Is error logging enabled /// /// /// /// Is error logging enabled. Logging is only enabled for the /// first error delivered to the . /// /// public bool IsEnabled { get { return m_firstTime; } } /// /// The date the first error that trigged this error handler occured. /// public DateTime EnabledDate { get { return m_enabledDate; } } /// /// The message from the first error that trigged this error handler. /// public string ErrorMessage { get { return m_message; } } /// /// The exception from the first error that trigged this error handler. /// /// /// May be . /// public Exception Exception { get { return m_exception; } } /// /// The error code from the first error that trigged this error handler. /// /// /// Defaults to /// public ErrorCode ErrorCode { get { return m_errorCode; } } #endregion #region Private Instance Fields /// /// The date the error was recorded. /// private DateTime m_enabledDate; /// /// Flag to indicate if it is the first error /// private bool m_firstTime = true; /// /// The message recorded during the first error. /// private string m_message = null; /// /// The exception recorded during the first error. /// private Exception m_exception = null; /// /// The error code recorded during the first error. /// private ErrorCode m_errorCode = ErrorCode.GenericFailure; /// /// String to prefix each message with /// private readonly string m_prefix; #endregion Private Instance Fields #region Private Static Fields /// /// The fully qualified type of the OnlyOnceErrorHandler class. /// /// /// Used by the internal logger to record the Type of the /// log message. /// private readonly static Type declaringType = typeof(OnlyOnceErrorHandler); #endregion } }