如何在.NET中将单个TextWriter拆分为多个输出



我从我的C#代码中调用了一个API,它有一个带有以下头的方法

public void ExternalFactory.SetOut(TextWriter outputStream)  

我通常把这种方法称为

ExternalFactory.SetOut(Console.Out) 

让API将其所有信息写入控制台。但是,除了写入控制台之外,我还想将这些信息存储在一个更永久的地方,例如文本文件。

我的第一个猜测是,我需要创建某种客户TextWriter来分割流,并将一个发送到控制台,另一个发送给StreamWriter。这里的正确方法是什么?

除了编译和首次使用之外,没有进行过测试,但可能会节省一些打字时间。令人烦恼的是,它还不存在。

/// <summary>
/// Spreads data out to multiple text writers.
/// </summary>
class TextWriterMulti : System.IO.TextWriter
{
    private System.Collections.Generic.List<System.IO.TextWriter> writers = new System.Collections.Generic.List<System.IO.TextWriter>();
    private System.IFormatProvider formatProvider = null;
    private System.Text.Encoding encoding = null;
    #region TextWriter Properties
    public override System.IFormatProvider FormatProvider
    {
        get
        {
            System.IFormatProvider formatProvider = this.formatProvider;
            if (formatProvider == null)
            {
                formatProvider = base.FormatProvider;
            }
            return formatProvider;
        }
    }
    public override string NewLine
    {
        get { return base.NewLine; }
        set
        {
            foreach (System.IO.TextWriter writer in this.writers)
            {
                writer.NewLine = value;
            }
            base.NewLine = value;
        }
    }

    public override System.Text.Encoding Encoding
    {
        get
        {
            System.Text.Encoding encoding = this.encoding;
            if (encoding == null)
            {
                encoding = System.Text.Encoding.Default;
            }
            return encoding;
        }
    }
    #region TextWriter Property Setters
    TextWriterMulti SetFormatProvider(System.IFormatProvider value)
    {
        this.formatProvider = value;
        return this;
    }
    TextWriterMulti SetEncoding(System.Text.Encoding value)
    {
        this.encoding = value;
        return this;
    }
    #endregion // TextWriter Property Setters
    #endregion // TextWriter Properties

    #region Construction/Destruction
    public TextWriterMulti(System.Collections.Generic.IEnumerable<System.IO.TextWriter> writers)
    {
        this.Clear();
        this.AddWriters(writers);
    }
    #endregion // Construction/Destruction
    #region Public interface
    public TextWriterMulti Clear()
    {
        this.writers.Clear();
        return this;
    }
    public TextWriterMulti AddWriter(System.IO.TextWriter writer)
    {
        this.writers.Add(writer);
        return this;
    }
    public TextWriterMulti AddWriters(System.Collections.Generic.IEnumerable<System.IO.TextWriter> writers)
    {
        this.writers.AddRange(writers);
        return this;
    }
    #endregion // Public interface
    #region TextWriter methods
    public override void Close()
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Close();
        }
        base.Close();
    }
    protected override void Dispose(bool disposing)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            if (disposing)
            {
                writer.Dispose();
            }
        }
        base.Dispose(disposing);
    }
    public override void Flush()
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Flush();
        }
        base.Flush();
    }
    //foreach (System.IO.TextWriter writer in this.writers)
    //{
    //    writer;
    //}
    public override void Write(bool value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(value);
        }
    }
    public override void Write(char value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(value);
        }
    }
    public override void Write(char[] buffer)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(buffer);
        }
    }
    public override void Write(decimal value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(value);
        }
    }
    public override void Write(double value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(value);
        }
    }
    public override void Write(float value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(value);
        }
    }
    public override void Write(int value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(value);
        }
    }
    public override void Write(long value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(value);
        }
    }
    public override void Write(object value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(value);
        }
    }
    public override void Write(string value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(value);
        }
    }
    public override void Write(uint value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(value);
        }
    }
    public override void Write(ulong value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(value);
        }
    }
    public override void Write(string format, object arg0)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(format, arg0);
        }
    }
    public override void Write(string format, params object[] arg)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(format, arg);
        }
    }
    public override void Write(char[] buffer, int index, int count)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(buffer, index, count);
        }
    }
    public override void Write(string format, object arg0, object arg1)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(format, arg0, arg1);
        }
    }
    public override void Write(string format, object arg0, object arg1, object arg2)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.Write(format, arg0, arg1, arg2);
        }
    }
    public override void WriteLine()
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine();
        }
    }
    public override void WriteLine(bool value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(value);
        }
    }
    public override void WriteLine(char value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(value);
        }
    }
    public override void WriteLine(char[] buffer)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(buffer);
        }
    }
    public override void WriteLine(decimal value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(value);
        }
    }
    public override void WriteLine(double value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(value);
        }
    }
    public override void WriteLine(float value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
           writer.WriteLine(value);
        }
    }
    public override void WriteLine(int value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(value);
        }
    }
    public override void WriteLine(long value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(value);
        }
    }
    public override void WriteLine(object value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(value);
        }
    }
    public override void WriteLine(string value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(value);
        }
    }
    public override void WriteLine(uint value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(value);
        }
    }
    public override void WriteLine(ulong value)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(value);
        }
    }
    public override void WriteLine(string format, object arg0)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(format, arg0);
        }
    }
    public override void WriteLine(string format, params object[] arg)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(format, arg);
        }
    }
    public override void WriteLine(char[] buffer, int index, int count)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(buffer, index, count);
        }
    }
    public override void WriteLine(string format, object arg0, object arg1)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(format, arg0, arg1);
        }
    }
    public override void WriteLine(string format, object arg0, object arg1, object arg2)
    {
        foreach (System.IO.TextWriter writer in this.writers)
        {
            writer.WriteLine(format, arg0, arg1, arg2);
        }
    }
    #endregion // TextWriter methods
}

我使用log4net而不是标准的I/O设施来做这类事情。我将log4net配置为同时登录到控制台和日志文件,使用ConsoleAppender和FileAppnder或RollingFileAppender。

好的方面是,您可以设置日志消息模板来捕获除了日志消息之外的各种有用信息(一天中的时间、线程/进程id、机器名称等)。

您还可以登录到SQL Server、事件日志或远程处理接收器。

轻松!

下面是一个示例TextWriter实现,它通过log4net:路由所有内容

using System;
using System.IO;
using System.Text;
using log4net ;
namespace ConsoleApplication22
{
    public class Log4NetTextWriter : TextWriter, IDisposable
    {
        private static ILog log = log4net.LogManager.GetLogger( typeof(Log4NetTextWriter) ) ;
        #region properties
        private StringBuilder buffer { get ; set ; }
        public override Encoding Encoding
        {
            get
            {
                // since this TextWrite is writing to log4net, we have no idea what the final encoding might be.
                // It all depends on the log4net configuration: tthe appender or appenders that wind up handling the logged message
                // determine the final encoding.
                //
                // Might make more sense to return Encoding.UTF8 though, just to return something.
                throw new NotImplementedException() ;
            }
        }
        #endregion properties ;
        public override void Flush()
        {
            if ( this.buffer != null && this.buffer.Length > 0 )
            {
                this.WriteLine() ;
            }
            return ;
        }
        public override void Close()
        {
            base.Close();
        }
        protected override void Dispose( bool disposing )
        {
            this.Flush() ;
            base.Dispose( disposing );
        }
        #region public constructors
        public Log4NetTextWriter() : this( null )
        {
            return ;
        }
        public Log4NetTextWriter( IFormatProvider formatProvider ) : base( formatProvider )
        {
            this.buffer = new StringBuilder() ;
        }
        #endregion public constructors
        #region public Write() overloads
        public override void Write( bool value )
        {
            this.buffer.Append( value ) ;
            return ;
        }
        public override void Write( char value )
        {
            this.buffer.Append( value ) ;
            return ;
        }
        public override void Write( char[] buffer )
        {
            this.buffer.Append( buffer ) ;
            return ;
        }
        public override void Write( char[] buffer , int index , int count )
        {
            this.buffer.Append( buffer , index , count ) ;
            return ;
        }
        public override void Write( decimal value )
        {
            this.buffer.Append( value ) ;
            return ;
        }
        public override void Write( double value )
        {
            this.buffer.Append( value ) ;
            return ;
        }
        public override void Write( float value )
        {
            this.buffer.Append( value ) ;
            return ;
        }
        public override void Write( int value )
        {
            this.buffer.Append( value ) ;
            return ;
        }
        public override void Write( long value )
        {
            this.buffer.Append( value ) ;
            return ;
        }
        public override void Write(object value)
        {
            this.buffer.Append( value ) ;
            return ;
        }
        public override void Write( string format , object arg0 )
        {
            this.buffer.AppendFormat( this.FormatProvider , format , arg0 ) ;
            return ;
        }
        public override void Write( string format , object arg0 , object arg1 )
        {
            this.buffer.AppendFormat( this.FormatProvider , format , arg0 , arg1 ) ;
            return ;
        }
        public override void Write( string format , object arg0 , object arg1 , object arg2 )
        {
            this.buffer.AppendFormat( this.FormatProvider , format , arg0 , arg1 , arg2 );
            return ;
        }
        public override void Write( string format , params object[] arg )
        {
            this.buffer.AppendFormat( this.FormatProvider , format , arg ) ;
            return ;
        }
        public override void Write( string value )
        {
             this.buffer.Append( value );
            return ;
        }
        public override void Write( uint value )
        {
            this.buffer.Append( value );
            return ;
        }
        public override void Write( ulong value )
        {
            this.buffer.Append( value );
            return ;
        }
        public override void WriteLine()
        {
            string logMessage = this.buffer.ToString() ;
            this.buffer.Length = 0 ;
            log.Info( logMessage ) ;
            return ;
        }
        #endregion public Write() overloads
        #region public WriteLine() overloads
        public override void WriteLine( bool value )
        {
            this.Write( value ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( char value )
        {
            this.Write( value ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( char[] buffer )
        {
            this.Write( buffer ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( char[] buffer , int index , int count )
        {
            this.Write( buffer , index , count ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( decimal value )
        {
            this.Write( value ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( double value )
        {
            this.Write( value ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( float value )
        {
            this.Write( value ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( int value )
        {
            this.Write( value ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( long value )
        {
            this.Write( value ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( object value )
        {
            this.Write( value ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( string format , object arg0 )
        {
            this.Write( format , arg0 ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( string format , object arg0 , object arg1 )
        {
            this.Write( format , arg0 , arg1 ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( string format , object arg0 , object arg1 , object arg2 )
        {
            this.Write( format , arg0 , arg1 , arg2 ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( string format , params object[] arg )
        {
            this.Write( format , arg ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( string value )
        {
            this.Write( value ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( uint value )
        {
            this.Write( value ) ;
            this.WriteLine() ;
            return ;
        }
        public override void WriteLine( ulong value )
        {
            this.Write( value ) ;
            this.WriteLine() ;
            return ;
        }
        #endregion public WriteLine() overloads
    }
}

下面是一个示例log4net配置文件。这一个同时登录到控制台和日志文件,并根据大小自动滚动(也可以根据日期/时间或每次执行滚动)。

<log4net>
  <!-- Log to the console -->
  <appender name="Console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <!-- Pattern to output the caller's file name and line number -->
      <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
    </layout>
  </appender>
  <!-- Log to a log file. This particular setup should log to a static file name 'log.txt' -->
  <!-- When it hits 100KB in size, it rolls, keeping up to 10 archived files. The archived -->
  <!-- files are named 'log.text.1', 'log.txt.2', ... , 'log.txt.10' -->
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="log.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="100KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="Console" />
    <appender-ref ref="RollingFile" />
  </root>
</log4net>

要配置log4net,最简单的方法是在AssemblyInfo.cs中放入XmlConfigurator属性,因此:

// Configure log4net using the .log4net file
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.log4net in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.

当程序运行时,您可以打开或关闭日志记录,或者只需编辑和保存配置文件即可更改其配置。log4net监视配置文件的更改并动态重新配置自己。

您的第一个猜测是正确的。您可以对其进行泛化,并编写一个"分布式"文本编写器,它接受一个文本编写器列表作为构建时间参数,并将接收到的所有输入分配给每个文本编写器。

您只需向流(文本文件)和显示(控制台)发送一份数据副本。

相关内容

  • 没有找到相关文章

最新更新