In an existing C# 2008 application, I want to have error messages written to a log file that can be saved and looked at later for a reference. I basically want to generate console.writeline messages and save the output. I could also use something like objUtil.LogMessage to accomplish this goal.
I would like to accomplish the following goals:
1. Do I need to 'open' some kind of a log file. If so, how would you code this?
2. I would like to save ex.messages that are located in try catch blocks. Does this require any special code?
3. I would like to open and save the log files to a certain directory path location.
4. I could possibly use objUtil.LogMessage and/or console.writeline separate or together? Is this possbile, if so how is this possible?
Thus can you tell me and or show me in code how to accomplish any and/or all of the goals I just listed above?
Loading
VulpesPosted Nov 9, 2012, 12:55 PM
1. 'logPath' can be set to anything you like though it's usually best to write log files to the same directory as the executable itself or to a sub-directory thereof, as discussed in previous threads.
2. As long as you specify the second parameter to be 'true' when creating the StreamWriter object, then new material will be appended to an existing file rather than overwriting it completely. If the file doesn't already exist, then it will be created.
You can, of course, call the StreamWriter.WriteLine method any place you want - not just in 'catch' blocks.
Venkatesh KumarPosted Nov 9, 2012, 1:31 PM
dcPosted Nov 9, 2012, 12:23 PM
1. How would I have the file to a certain directory path?
2. How would I append messages to this log file?
VulpesPosted Nov 9, 2012, 12:09 PM
I can't answer the fourth point as I don't know what objUtil.LogMessage is - something to do with Java rather than C# may be?