Inheriting from StreamWriter with smallest possible effort

c#, mocking, streamwriter

Solution

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);
    }
}

Problem

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?

Original source