Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Writing to An Error File

Posted on 2004-08-26
4
Medium Priority
?
208 Views
Last Modified: 2010-04-15
I was wondering what was the easiest way to write to an error file. Ideally, I first want to check to see if the file exists. If it does, I want to open it and add a line (the error message), if it doesn't, I want to create it, then add the line.

Thanks!
0
Comment
Question by:nh_capricorn
  • 2
4 Comments
 
LVL 3

Assisted Solution

by:sohilm
sohilm earned 1000 total points
ID: 11907287
There is probably different ways to do this, most of the apps I write have a database back end, so I upload the error into the database. The way I handle that is by writing an static error class with a static subroutine that displays the error and also uploads the info to the database.

Another idea would be to display the error and log it to the event log.  ex:
public class ErrorLog
{
  public static void LogError(Exception e)
  {
    System.Diagnostics.EventLog.WriteEntry(productname,@"The Following Error Occured:" + e.ToString(),System.Diagnostics.EventLogEntryType.Error,901);
    System.Windows.Forms.MessageBox.Show(@"The Following Error Occured:" + e.ToString(),"Error",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
  }
}


Hope that helps.

SohilM
0
 
LVL 14

Accepted Solution

by:
existenz2 earned 1000 total points
ID: 11908988
If you want to use a file for the error log you could use the following code:

using System.IO;

if (File.Exists("<filename>")) {
StreamWriter sw = File.AppendText("<filename>");
sw.Write("<error>");
sw.Flush();
sw.Close();
}
else {
StreamWriter sw = File.CreateText("<filename>");
sw.Write("<error>");
sw.Flush();
sw.Close();
}

0
 

Author Comment

by:nh_capricorn
ID: 11921263
Once again I have been presented with two good solutions, both of which I am likely to use, depending upon my needs. Therefore, I increased the points for this question, so that I could make an even split between the two of you (with the resulting points being closer to the original question value).

Thanks Again!
0
 
LVL 3

Expert Comment

by:sohilm
ID: 11923943
Thank you, that was very kind of you.

SohilM.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question