#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 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 printed on the standard error output stream.
///
///
/// This policy aims at protecting an otherwise working application
/// from being flooded with error messages when logging fails.
///
///
/// Nicko Cadell
/// Gert Driesen
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 Implementation of IErrorHandler
///
/// Log an Error
///
/// The error message.
/// The exception.
/// The internal error code.
///
///
/// Prints the message and the stack trace of the exception on the standard
/// error output stream.
///
///
public void Error(string message, Exception e, ErrorCode errorCode)
{
if (IsEnabled)
{
LogLog.Error("[" + m_prefix + "] " + message, e);
}
}
///
/// Log an Error
///
/// The error message.
/// The exception.
///
///
/// Prints the message and the stack trace of the exception on the standard
/// error output stream.
///
///
public void Error(string message, Exception e)
{
if (IsEnabled)
{
LogLog.Error("[" + m_prefix + "] " + message, e);
}
}
///
/// Log an error
///
/// The error message.
///
///
/// Print a the error message passed as parameter on the standard
/// error output stream.
///
///
public void Error(string message)
{
if (IsEnabled)
{
LogLog.Error("[" + m_prefix + "] " + message);
}
}
#endregion Implementation of IErrorHandler
///
/// Is error logging enabled
///
///
///
/// Is error logging enabled. Logging is only enabled for the
/// first error delivered to the .
///
///
private bool IsEnabled
{
get
{
// Allow first error message to be logged
if (m_firstTime)
{
m_firstTime = false;
return true;
}
// Check if InternalDebugging is enabled
if (LogLog.InternalDebugging && !LogLog.QuietMode)
{
return true;
}
return false;
}
}
#region Private Instance Fields
///
/// Flag to indicate if it is the first error
///
private bool m_firstTime = true;
///
/// String to prefix each message with
///
private readonly string m_prefix;
#endregion Private Instance Fields
}
}