I have a console application that will execute once every day. (Using task scheduler in Windows.) It generates output to the console, which is useful during debugging, but the automated task will not save it. And while I could set the task to redirect the output to a file, I would rather prefer this logging to be written to the console and a text file. (Which would have a timestamp as name.)
Now, I could just use a separate text stream for this file, and write information to it, but that means duplicating every Write/WriteLine statement twice. That would be a bad design.
So, how could I reduce this to just one Write/WriteLine statement that would write the text to both the console and to a file?
I am considering using the ILogger interface, but this console application is about 70 lines of code, all of it straight to the point. (About four things that it processes.) Besides, the ILogger interface adds filtering based on the log level. I just need everything to be logged so ILogger is overkill for my project.
I do not want to redirect the console output! I want the console stream to be copied to a second stream.
Does .NET 7.0 has some new features that I could use for this purpose?
Just to be clear: I Do NOT need a logger! There are no log levels or anything. Normally, I would use the application and it writes what it's doing to the console. But as a scheduled task, this console becomes invisible so I need an exact carbon copy.
This would be similar to using the tee
command on the commandline, but I'm looking for a method inside the code that does not rely on any commandline parameters.
I found an answer at Mirroring console output to a file which is quite old, yet should still work. Closing this now as it's a duplicate question.