C
C#14mo ago
MrScautHD

❔ Why is the Ecxepction not written in the Out TextWriter?

I try to write the Console to a File:
private static void SetupConsoleOutput() {
if (LogPath != null) {

StreamWriter fileWriter = new StreamWriter(LogPath, true);
fileWriter.AutoFlush = true;
SyncTextWriter syncWriter = new SyncTextWriter(Console.Out, fileWriter);

Console.SetOut(syncWriter);
Console.SetError(syncWriter);
}
}
private static void SetupConsoleOutput() {
if (LogPath != null) {

StreamWriter fileWriter = new StreamWriter(LogPath, true);
fileWriter.AutoFlush = true;
SyncTextWriter syncWriter = new SyncTextWriter(Console.Out, fileWriter);

Console.SetOut(syncWriter);
Console.SetError(syncWriter);
}
}
Thats the SyncTextWriter:
public class SyncTextWriter : TextWriter {

private readonly TextWriter _firstWriter;
private readonly TextWriter _secondWriter;

public override Encoding Encoding => Encoding.UTF8;

public SyncTextWriter(TextWriter firstWriter, TextWriter secondWriter) {
this._firstWriter = firstWriter;
this._secondWriter = secondWriter;
}

public override void Write(char value) {
this._firstWriter.Write(value);
this._secondWriter.Write(value);
}

public override void WriteLine(string? value) {
this._firstWriter.WriteLine(value);
this._secondWriter.WriteLine(value);
}

public override void Flush() {
this._firstWriter.Flush();
this._secondWriter.Flush();
}

public override void Close() {
this._firstWriter.Close();
this._secondWriter.Close();
}
}
public class SyncTextWriter : TextWriter {

private readonly TextWriter _firstWriter;
private readonly TextWriter _secondWriter;

public override Encoding Encoding => Encoding.UTF8;

public SyncTextWriter(TextWriter firstWriter, TextWriter secondWriter) {
this._firstWriter = firstWriter;
this._secondWriter = secondWriter;
}

public override void Write(char value) {
this._firstWriter.Write(value);
this._secondWriter.Write(value);
}

public override void WriteLine(string? value) {
this._firstWriter.WriteLine(value);
this._secondWriter.WriteLine(value);
}

public override void Flush() {
this._firstWriter.Flush();
this._secondWriter.Flush();
}

public override void Close() {
this._firstWriter.Close();
this._secondWriter.Close();
}
}
82 Replies
MrScautHD
MrScautHDOP14mo ago
Does aynone know how i can write the error text too? i tried already Console.Error but it does not looks like it
JakenVeina
JakenVeina14mo ago
what exception are you expecting to be written?
MrScautHD
MrScautHDOP14mo ago
any that happans i thought the Console.Error control that but maybe im wrong
JakenVeina
JakenVeina14mo ago
the Console.Error stream only captures things that get written to it
MrScautHD
MrScautHDOP14mo ago
Yea So how is the console knowing there is a Problem ?
JakenVeina
JakenVeina14mo ago
what do you mean?
MrScautHD
MrScautHDOP14mo ago
Is the console not Something to like a file
JakenVeina
JakenVeina14mo ago
uhm kind of? what is your question?
MrScautHD
MrScautHDOP14mo ago
So where does throw ecxeption... write into it
JakenVeina
JakenVeina14mo ago
wherever you want
MrScautHD
MrScautHDOP14mo ago
Nah i mean Has it a own writer... Because i wanna get it to write it in my file too
JakenVeina
JakenVeina14mo ago
what?
Has it a own writer
what is "it"?
MrScautHD
MrScautHDOP14mo ago
Is it possible to get the text of the console
JakenVeina
JakenVeina14mo ago
yes, you're already doing it
MrScautHD
MrScautHDOP14mo ago
Textwriter But that is without the ecxeption things
JakenVeina
JakenVeina14mo ago
that is with everything that there is you are capturing everything that is being written to the Console
MrScautHD
MrScautHDOP14mo ago
No throw error not
JakenVeina
JakenVeina14mo ago
correct
MrScautHD
MrScautHDOP14mo ago
But wjy
JakenVeina
JakenVeina14mo ago
because you're not writing anything to Console.Error
MrScautHD
MrScautHDOP14mo ago
That means?
JakenVeina
JakenVeina14mo ago
thrown Exceptions do not write themselves to the console if you don't write anything to Console.Error then nothing will be written to Console.Error and nothing will show up in your file, from Console.Error
MrScautHD
MrScautHDOP14mo ago
Oh So i need to impliment a method that throw a ecxeption and a log to error?
JakenVeina
JakenVeina14mo ago
implement whatever logic you like to get whatever data you like into the error stream you can write before you throw your own exceptions you can write all exceptions that you catch you can do both of those things you can write only exceptions that bubble up to kill the whole application you can write exceptions that you catch and solve
MrScautHD
MrScautHDOP14mo ago
But it would not possible to get any ecxeption like even if any api doing it or c# (.net)
JakenVeina
JakenVeina14mo ago
why wouldn't it?
MrScautHD
MrScautHDOP14mo ago
How would it?
JakenVeina
JakenVeina14mo ago
what exceptions do you want to write?
MrScautHD
MrScautHDOP14mo ago
All
JakenVeina
JakenVeina14mo ago
define "All"
MrScautHD
MrScautHDOP14mo ago
All that extends from excaptions
JakenVeina
JakenVeina14mo ago
okay, but which ones? which instances? you have described types of exceptions
MrScautHD
MrScautHDOP14mo ago
Ok just for example a nullpointexcaption
JakenVeina
JakenVeina14mo ago
great, which ones?
MrScautHD
MrScautHDOP14mo ago
Which one?
JakenVeina
JakenVeina14mo ago
yes, which NullReferenceExceptions? which ones do you want to log?
MrScautHD
MrScautHDOP14mo ago
All
JakenVeina
JakenVeina14mo ago
all that get created and thrown?
MrScautHD
MrScautHDOP14mo ago
Yea
JakenVeina
JakenVeina14mo ago
thrown from where?
MrScautHD
MrScautHDOP14mo ago
Anywhere
JakenVeina
JakenVeina14mo ago
no, you cannot do that
MrScautHD
MrScautHDOP14mo ago
Ok
JakenVeina
JakenVeina14mo ago
you cannot log things that happen in code that isn't yours you have control over YOUR code
MrScautHD
MrScautHDOP14mo ago
Oh ok
JakenVeina
JakenVeina14mo ago
you can log wherever you might throw your own exceptions
MrScautHD
MrScautHDOP14mo ago
Sad
JakenVeina
JakenVeina14mo ago
not really
MrScautHD
MrScautHDOP14mo ago
Well But thx anyways
JakenVeina
JakenVeina14mo ago
and you can log exceptions that bubble into your code
MrScautHD
MrScautHDOP14mo ago
I impliment then a fatal method in my logger class
JakenVeina
JakenVeina14mo ago
not really, you wouldn't want this anyway
MrScautHD
MrScautHDOP14mo ago
Well but in can not write the same error as in the console
JakenVeina
JakenVeina14mo ago
what? what same error as what console?
MrScautHD
MrScautHDOP14mo ago
I want to get the throw ecxeptions that get written in the console too
JakenVeina
JakenVeina14mo ago
okay
MrScautHD
MrScautHDOP14mo ago
Because that not saved in the console.out writer
JakenVeina
JakenVeina14mo ago
so, when I asked you "what exception are you expecting to be written?" the correct answer was not "any" the correct answer was "this specific exception that I'm seeing in the console" and then show it
MrScautHD
MrScautHDOP14mo ago
Well yea Sry for that
JakenVeina
JakenVeina14mo ago
show me
MrScautHD
MrScautHDOP14mo ago
Im on the phone I can not show yu But the ecxeption does just get written in the console and not in my file
JakenVeina
JakenVeina14mo ago
does this happen right before the app crashes?
MrScautHD
MrScautHDOP14mo ago
I crash it my self to see if it works Or what yu mean?
JakenVeina
JakenVeina14mo ago
I mean is this an exception that is occuring as your app crashes?
MrScautHD
MrScautHDOP14mo ago
Yea
JakenVeina
JakenVeina14mo ago
it is not being written to the console that's the framework writing the exception to console that killed your app your app has already died, and the Console has been deconstructed
MrScautHD
MrScautHDOP14mo ago
So
JakenVeina
JakenVeina14mo ago
I.E. just because you see it in the "Console" window, doesn't mean that it was written through the Console class it was written directly, after your app was already spun down
MrScautHD
MrScautHDOP14mo ago
Oh ok
JakenVeina
JakenVeina14mo ago
if you want to catch exceptions that are killing your app, and log them, then do that
MrScautHD
MrScautHDOP14mo ago
Then do what?
JakenVeina
JakenVeina14mo ago
catch exceptions that are killing your app and log them
MrScautHD
MrScautHDOP14mo ago
Yea but thats not possible for any excaptions or?
JakenVeina
JakenVeina14mo ago
you don't want any exceptions you want the exceptions that are killing your app you want exceptions that bubble up through the entire app
MrScautHD
MrScautHDOP14mo ago
But how can i catch it then
JakenVeina
JakenVeina14mo ago
the same way you catch any exception
MrScautHD
MrScautHDOP14mo ago
try { // Block of code to try } catch (Exception e) { // Block of code to handle errors } Yu mean Something like that
JakenVeina
JakenVeina14mo ago
I mean exactly that
MrScautHD
MrScautHDOP14mo ago
But how can k catch if it crash the program
JakenVeina
JakenVeina14mo ago
by putting the try around "the program"
MrScautHD
MrScautHDOP14mo ago
Ohh Yu smart
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server