#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 System.IO;
using log4net.Core;
namespace log4net.Util
{
///
/// that does not leak exceptions
///
///
///
/// does not throw exceptions when things go wrong.
/// Instead, it delegates error handling to its .
///
///
/// Nicko Cadell
/// Gert Driesen
public class QuietTextWriter : TextWriterAdapter
{
#region Public Instance Constructors
///
/// Constructor
///
/// the writer to actually write to
/// the error handler to report error to
///
///
/// Create a new QuietTextWriter using a writer and error handler
///
///
public QuietTextWriter(TextWriter writer, IErrorHandler errorHandler) : base(writer)
{
if (errorHandler == null)
{
throw new ArgumentNullException("errorHandler");
}
ErrorHandler = errorHandler;
}
#endregion Public Instance Constructors
#region Public Instance Properties
///
/// Gets or sets the error handler that all errors are passed to.
///
///
/// The error handler that all errors are passed to.
///
///
///
/// Gets or sets the error handler that all errors are passed to.
///
///
public IErrorHandler ErrorHandler
{
get { return m_errorHandler; }
set
{
if (value == null)
{
// This is a programming error on the part of the enclosing appender.
throw new ArgumentNullException("value");
}
m_errorHandler = value;
}
}
///
/// Gets a value indicating whether this writer is closed.
///
///
/// true if this writer is closed, otherwise false.
///
///
///
/// Gets a value indicating whether this writer is closed.
///
///
public bool Closed
{
get { return m_closed; }
}
#endregion Public Instance Properties
#region Override Implementation of TextWriter
///
/// Writes a character to the underlying writer
///
/// the char to write
///
///
/// Writes a character to the underlying writer
///
///
public override void Write(char value)
{
try
{
base.Write(value);
}
catch(Exception e)
{
m_errorHandler.Error("Failed to write [" + value + "].", e, ErrorCode.WriteFailure);
}
}
///
/// Writes a buffer to the underlying writer
///
/// the buffer to write
/// the start index to write from
/// the number of characters to write
///
///
/// Writes a buffer to the underlying writer
///
///
public override void Write(char[] buffer, int index, int count)
{
try
{
base.Write(buffer, index, count);
}
catch(Exception e)
{
m_errorHandler.Error("Failed to write buffer.", e, ErrorCode.WriteFailure);
}
}
///
/// Writes a string to the output.
///
/// The string data to write to the output.
///
///
/// Writes a string to the output.
///
///
override public void Write(string value)
{
try
{
base.Write(value);
}
catch(Exception e)
{
m_errorHandler.Error("Failed to write [" + value + "].", e, ErrorCode.WriteFailure);
}
}
///
/// Closes the underlying output writer.
///
///
///
/// Closes the underlying output writer.
///
///
override public void Close()
{
m_closed = true;
base.Close();
}
#endregion Public Instance Methods
#region Private Instance Fields
///
/// The error handler instance to pass all errors to
///
private IErrorHandler m_errorHandler;
///
/// Flag to indicate if this writer is closed
///
private bool m_closed = false;
#endregion Private Instance Fields
}
}