Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting from StreamWriter with smallest possible effort

I am using an external library which allows logging using StreamWriter - now I want to add some handling based on the content of the logging. As I want to avoid to loop through the log file, I would like to write a class which inherits from StreamWriter.
What is the best way to inherit from StreamWriter with as few re-implementations of methods/constructors?

like image 592
weismat Avatar asked Dec 22 '22 05:12

weismat


1 Answers

I'm not sure of what you want to do exactly, but if you only want to inspect what is being written in the stream, you can do this:

public class CustomStreamWriter : StreamWriter
{
    public CustomStreamWriter(Stream stream)
        : base(stream)
    {}

    public override void Write(string value)
    {
        //Inspect the value and do something

        base.Write(value);
    }
}
like image 87
Jeff Cyr Avatar answered Dec 31 '22 13:12

Jeff Cyr