Solved

Writing to An Error File

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
which object should i use (efficiency) 5 46
C# TextBox 11 30
Vb.net dynamic formulas in runtime 11 62
C#.NET and microsoft certification. 3 34
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now