Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- namespace BA.Core.IO {
- /// <summary>
- /// Text Writer that writes to one or move text writers; used in Tracing;
- /// </summary>
- public class MulticastTextWriter :NotificationTextWriter {
- readonly List <TextWriter > _textWriters = null ;
- /// <summary>
- /// Gets or Sets wether to flush writers after each write
- /// </summary>
- public bool AutoFlush { get ; set ; }
- public MulticastTextWriter() {
- _textWriters = new List <TextWriter >();
- }
- public MulticastTextWriter(params TextWriter [] textWriters) {
- _textWriters = new List <TextWriter >(textWriters);
- }
- public MulticastTextWriter(TextNotificationHandler notificationFunc,params TextWriter [] textWriters)
- : this (textWriters) {
- TextNotification += notificationFunc;
- }
- public TextWriter [] GetWriters() {
- lock (_textWriters) {
- return _textWriters.ToArray();
- }
- }
- protected override void Dispose(bool disposing) {
- lock (_textWriters) {
- foreach (TextWriter writer in _textWriters)
- writer.Dispose();
- }
- base .Dispose(disposing);
- }
- public void Add(TextWriter textWriter) {
- lock (_textWriters) {
- _textWriters.Add(textWriter);
- }
- }
- public bool Remove(TextWriter textWriter) {
- lock (_textWriters) {
- return _textWriters.Remove(textWriter);
- }
- }
- public override void Write(char [] buffer,int index,int count) {
- TextWriter [] writers = GetWriters();
- foreach (TextWriter tw in writers) {
- tw.Write(buffer,index,count);
- }
- base .Write(buffer,index,count);
- if (AutoFlush)
- Flush();
- }
- public override void Flush() {
- TextWriter [] writers = GetWriters();
- foreach (TextWriter tw in writers) {
- tw.Flush();
- }
- }
- public override void Close() {
- TextWriter [] writers = GetWriters();
- foreach (TextWriter tw in writers) {
- tw.Close();
- }
- }
- public override Encoding Encoding {
- get {
- return base .Encoding;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment