Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#ASP.NET
How to write an exception in a text file?
 
I have a code as below,
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 5-Nov-12 2:04am
Edited 5-Nov-12 8:56am
v2
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
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
  Permalink  
Comments
Member 9353131 at 6-Nov-12 1:03am
   
This doesn't worked.
Milind Thakkar at 6-Nov-12 1:10am
   
Would you please elaborate? What is not working? What is the error?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can use log4Net .
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

FileStream fs = new FileStream(@"c:\ErrorLog.txt", FileMode.CreateNew, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(file + ex);
sw.Close();
fs.Close();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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();
 }
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 OriginalGriff 503
2 Manas Bhardwaj 393
3 Tadit Dash 310
4 Damith Weerasinghe 290
0 Sergey Alexandrovich Kryukov 9,575
1 OriginalGriff 5,856
2 Peter Leow 4,405
3 Maciej Los 3,540
4 Abhinav S 3,513


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid