Solved

Writing to An Error File

Posted on 2004-08-26
4
203 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 250 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 250 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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