C
C#8mo 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
MrScautHD8mo 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
JakenVeina8mo ago
what exception are you expecting to be written?
MrScautHD
MrScautHD8mo ago
any that happans i thought the Console.Error control that but maybe im wrong
JakenVeina
JakenVeina8mo ago
the Console.Error stream only captures things that get written to it
MrScautHD
MrScautHD8mo ago
Yea So how is the console knowing there is a Problem ?
JakenVeina
JakenVeina8mo ago
what do you mean?
MrScautHD
MrScautHD8mo ago
Is the console not Something to like a file
JakenVeina
JakenVeina8mo ago
uhm kind of? what is your question?
MrScautHD
MrScautHD8mo ago
So where does throw ecxeption... write into it
JakenVeina
JakenVeina8mo ago
wherever you want
MrScautHD
MrScautHD8mo ago
Nah i mean Has it a own writer... Because i wanna get it to write it in my file too
JakenVeina
JakenVeina8mo ago
what?
Has it a own writer
what is "it"?
MrScautHD
MrScautHD8mo ago
Is it possible to get the text of the console
JakenVeina
JakenVeina8mo ago
yes, you're already doing it
MrScautHD
MrScautHD8mo ago
Textwriter But that is without the ecxeption things
JakenVeina
JakenVeina8mo ago
that is with everything that there is you are capturing everything that is being written to the Console
MrScautHD
MrScautHD8mo ago
No throw error not
JakenVeina
JakenVeina8mo ago
correct
MrScautHD
MrScautHD8mo ago
But wjy
JakenVeina
JakenVeina8mo ago
because you're not writing anything to Console.Error
MrScautHD
MrScautHD8mo ago
That means?
JakenVeina
JakenVeina8mo 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
MrScautHD8mo ago
Oh So i need to impliment a method that throw a ecxeption and a log to error?
JakenVeina
JakenVeina8mo 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
MrScautHD8mo ago
But it would not possible to get any ecxeption like even if any api doing it or c# (.net)
JakenVeina
JakenVeina8mo ago
why wouldn't it?
MrScautHD
MrScautHD8mo ago
How would it?
JakenVeina
JakenVeina8mo ago
what exceptions do you want to write?
MrScautHD
MrScautHD8mo ago
All
JakenVeina
JakenVeina8mo ago
define "All"
MrScautHD
MrScautHD8mo ago
All that extends from excaptions
JakenVeina
JakenVeina8mo ago
okay, but which ones? which instances? you have described types of exceptions
MrScautHD
MrScautHD8mo ago
Ok just for example a nullpointexcaption
JakenVeina
JakenVeina8mo ago
great, which ones?
MrScautHD
MrScautHD8mo ago
Which one?
JakenVeina
JakenVeina8mo ago
yes, which NullReferenceExceptions? which ones do you want to log?
MrScautHD
MrScautHD8mo ago
All
JakenVeina
JakenVeina8mo ago
all that get created and thrown?
MrScautHD
MrScautHD8mo ago
Yea
JakenVeina
JakenVeina8mo ago
thrown from where?
MrScautHD
MrScautHD8mo ago
Anywhere
JakenVeina
JakenVeina8mo ago
no, you cannot do that
MrScautHD
MrScautHD8mo ago
Ok
JakenVeina
JakenVeina8mo ago
you cannot log things that happen in code that isn't yours you have control over YOUR code
MrScautHD
MrScautHD8mo ago
Oh ok
JakenVeina
JakenVeina8mo ago
you can log wherever you might throw your own exceptions
MrScautHD
MrScautHD8mo ago
Sad
JakenVeina
JakenVeina8mo ago
not really
MrScautHD
MrScautHD8mo ago
Well But thx anyways
JakenVeina
JakenVeina8mo ago
and you can log exceptions that bubble into your code
MrScautHD
MrScautHD8mo ago
I impliment then a fatal method in my logger class
JakenVeina
JakenVeina8mo ago
not really, you wouldn't want this anyway
MrScautHD
MrScautHD8mo ago
Well but in can not write the same error as in the console
JakenVeina
JakenVeina8mo ago
what? what same error as what console?
MrScautHD
MrScautHD8mo ago
I want to get the throw ecxeptions that get written in the console too
JakenVeina
JakenVeina8mo ago
okay
MrScautHD
MrScautHD8mo ago
Because that not saved in the console.out writer
JakenVeina
JakenVeina8mo 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
MrScautHD8mo ago
Well yea Sry for that
JakenVeina
JakenVeina8mo ago
show me
MrScautHD
MrScautHD8mo 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
JakenVeina8mo ago
does this happen right before the app crashes?
MrScautHD
MrScautHD8mo ago
I crash it my self to see if it works Or what yu mean?
JakenVeina
JakenVeina8mo ago
I mean is this an exception that is occuring as your app crashes?
MrScautHD
MrScautHD8mo ago
Yea
JakenVeina
JakenVeina8mo 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
MrScautHD8mo ago
So
JakenVeina
JakenVeina8mo 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
MrScautHD8mo ago
Oh ok
JakenVeina
JakenVeina8mo ago
if you want to catch exceptions that are killing your app, and log them, then do that
MrScautHD
MrScautHD8mo ago
Then do what?
JakenVeina
JakenVeina8mo ago
catch exceptions that are killing your app and log them
MrScautHD
MrScautHD8mo ago
Yea but thats not possible for any excaptions or?
JakenVeina
JakenVeina8mo 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
MrScautHD8mo ago
But how can i catch it then
JakenVeina
JakenVeina8mo ago
the same way you catch any exception
MrScautHD
MrScautHD8mo ago
try { // Block of code to try } catch (Exception e) { // Block of code to handle errors } Yu mean Something like that
JakenVeina
JakenVeina8mo ago
I mean exactly that
MrScautHD
MrScautHD8mo ago
But how can k catch if it crash the program
JakenVeina
JakenVeina8mo ago
by putting the try around "the program"
MrScautHD
MrScautHD8mo ago
Ohh Yu smart
Accord
Accord8mo 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
More Posts