Solved

Writing to An Error File

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

11 Experts available now in Live!

Get 1:1 Help Now