I am developing an application in C# and my current logging creates a StreamWriter and a FileStream at the beginning of the program like so:
//Activate Logging
try
{
streamer = new FileStream("filepath and name", FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter(streamer);
}
There are a few events happening prior to creating this log file, as I use some parameters that need to be parsed from the arguments to create the file name, and so I am running into two issues
If an error occurs before this file is created, I create another file called "crashlog", which logs the error itself. However, this file is sometimes empty.
If an unhandled error occurs, even if the original logfile is created, that file is empty.
This leads me to believe that the log gets dumped in it's entirety once the program exits and the stream/writer are closed. Is there a method or parameter I am overlooking that would allow to write to a file as things happen? This would help me figure out exactly where things crashed.
I thought of a way to do it, but I feel it is extremely inefficient, this is the process I have in mind
- Create a file with a randomly generated name and store it as a variable
- Once variables have been parsed, rename the file appropriately
- Every time I want to log something, open the file, add the new line, close the file
- If a crash happens, rename the file appropriately