Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Writing to An Error File

Posted on 2004-08-26
4
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

688 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