StreamWriter inheritance with minimal effort

I use an external library that allows logging using StreamWriter - now I want to add some processing based on the contents of the log. Since I want to avoid looping through a log file, I would like to write a class that inherits from StreamWriter.
What is the best way to inherit from StreamWriter with a minimal number of reimplementations of methods / constructors?

+4
source share
2 answers

I'm not sure what you want to do exactly, but if you only want to check what is 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); } } 
+8
source

Define the constructor that the external library uses and implement this (or just implement all of them), and then you just need to override the recording methods (s) used by your external library.

 public class Class1 : StreamWriter { public Class1(Stream stream) : base(stream) { } public Class1(Stream stream, Encoding encoding) : base(stream, encoding) { } public Class1(Stream stream, Encoding encoding, int bufferSize) : base(stream, encoding, bufferSize) { } public Class1(string path) : base(path) { } public Class1(string path, bool append) : base(path, append) { } public Class1(string path, bool append, Encoding encoding) : base(path, append, encoding) { } public Class1(string path, bool append, Encoding encoding, int bufferSize) : base(path, append, encoding, bufferSize) { } public override void Write(string value) { base.Write(value); } } 
+1
source

Source: https://habr.com/ru/post/1301348/


All Articles