andrew4582

MulticastTextWriter

Aug 4th, 2010
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1.  using System;
  2.  using System.Collections.Generic;
  3.  using System.IO;
  4.  using System.Text;
  5.  
  6.  namespace  BA.Core.IO {
  7.      /// <summary>
  8.      /// Text Writer that writes to one or move text writers; used in Tracing;
  9.      /// </summary>
  10.      public  class  MulticastTextWriter :NotificationTextWriter  {
  11.  
  12.          readonly  List <TextWriter > _textWriters = null ;
  13.          /// <summary>
  14.          /// Gets or Sets wether to flush writers after each write
  15.          /// </summary>
  16.          public  bool  AutoFlush { get ; set ; }
  17.  
  18.          public  MulticastTextWriter() {
  19.              _textWriters = new  List <TextWriter >();
  20.          }
  21.  
  22.          public  MulticastTextWriter(params  TextWriter [] textWriters) {
  23.              _textWriters = new  List <TextWriter >(textWriters);
  24.          }
  25.          public  MulticastTextWriter(TextNotificationHandler  notificationFunc,params  TextWriter [] textWriters)
  26.              : this (textWriters) {
  27.              TextNotification += notificationFunc;
  28.          }
  29.          public  TextWriter [] GetWriters() {
  30.              lock (_textWriters) {
  31.                  return  _textWriters.ToArray();
  32.              }
  33.          }
  34.          protected  override  void  Dispose(bool  disposing) {
  35.              lock (_textWriters) {
  36.                  foreach (TextWriter  writer in  _textWriters)
  37.                      writer.Dispose();
  38.              }
  39.              base .Dispose(disposing);
  40.          }
  41.  
  42.          public  void  Add(TextWriter  textWriter) {
  43.              lock (_textWriters) {
  44.                  _textWriters.Add(textWriter);
  45.              }
  46.          }
  47.  
  48.          public  bool  Remove(TextWriter  textWriter) {
  49.              lock (_textWriters) {
  50.                  return  _textWriters.Remove(textWriter);
  51.              }
  52.          }
  53.          public  override  void  Write(char [] buffer,int  index,int  count) {
  54.              TextWriter [] writers = GetWriters();
  55.              foreach (TextWriter  tw in  writers) {
  56.                  tw.Write(buffer,index,count);
  57.              }
  58.  
  59.              base .Write(buffer,index,count);
  60.  
  61.              if (AutoFlush)
  62.                  Flush();
  63.          }
  64.          public  override  void  Flush() {
  65.              TextWriter [] writers = GetWriters();
  66.              foreach (TextWriter  tw in  writers) {
  67.                  tw.Flush();
  68.              }
  69.          }
  70.          public  override  void  Close() {
  71.              TextWriter [] writers = GetWriters();
  72.              foreach (TextWriter  tw in  writers) {
  73.                  tw.Close();
  74.              }
  75.          }
  76.          public  override  Encoding  Encoding {
  77.              get  {
  78.                  return  base .Encoding;
  79.              }
  80.          }
  81.      }
  82.  }
Advertisement
Add Comment
Please, Sign In to add comment