Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to write an exception in a text file?

I have a code as below,
C#
try
{
   do something
}
catch(Exception ex)
{
   MessageBox.Show(ex);
}

I need to write(or append) the errors to a text file.

Please help.

Thanks in advance.
Posted
Updated 5-Nov-12 8:56am
v2
Comments
Sergey Alexandrovich Kryukov 5-Nov-12 19:00pm    
Why do you think it's a problem? Besides, output of ex is not enough. You need to output separately: type, message, inner exceptions, recursively, stack and, sometimes, parameters. I don't see what's the problem. How to write files? :-)
Remember, with ASP.NET, you will have to write it all in server-side file, and the path should be legitimate...
--SA

C#
Using System.IO;
System.IO.File.WriteAllText(OutputPath, text);

Outpath being the directory you want to save the text file.

Text being the ex.message.tostring()...

Hope this works for you.

Storxstar
 
Share this answer
 
v2
I am not sure how you write directly ex (you can try). I think you will have to format or use ex.Message or any other property that you want to save.

To write into a file a simple code as below should do.

C#
TextWriter writer = new StreamWriter(Server.MapPath("~/"LogFileName.txt"));            
writer.WriteLine(ex.Message);


Since this code is going to run on the server, make sure you have set appropriate access to the folder where you are creating this file.

However, if it logging purposes, I would strongly recommend to use a toolset like Log4Net or any other you are comfortable with. This is because with ASP.Net and multiuser scenario, thing can get ugly very easily.

Thanks
Milind
 
Share this answer
 
Comments
Member 9353131 6-Nov-12 1:03am    
This doesn't worked.
MT_ 6-Nov-12 1:10am    
Would you please elaborate? What is not working? What is the error?
For ASP.NET, you can use ELMAH to log it, and/or email it. There is even a nice UI that will let you look at it via the browser. You can see all of the entire YSOD (yellow screen of death) complete with the stack trace, full request details, and a dump of the server variables. If you have trace on, it will even capture the trace. The great thing about ELMAH is that it's 'configuration only' to add it to your project -- no code will need to be written -- unless you wish to log exceptions that you're catching instead of just the ones you're not. To catch, log, and handle exceptions, you have to make a single call to ELMAH's API with the exception in question as a parameter. You still get the full YSOD for caught exceptions when using this method, even if none is displayed to the user. I swear by it.
 
Share this answer
 
You can use log4Net .
 
Share this answer
 
FileStream fs = new FileStream(@"c:\ErrorLog.txt", FileMode.CreateNew, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(file + ex);
sw.Close();
fs.Close();
 
Share this answer
 
try
{
 //do something 
}
catch(Exception ex) 
{
 MessageBox.Show(ex.ToString());//to see what is the matter not necessary
 System.IO.FileStream writefile;
 byte[] bytedata = null;
 bytedata = Encoding.ASCII.GetBytes(ex.ToString());
 writefile = new FileStream(Application.StartupPath+"Eror.txt",FileMode.Append);
 writefile.Write(bytedata, 0, bytedata.Length);
 writefile.Close();
 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900