Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save exception in txt file?

public DataTable InsertItemDetails(FeedRetailPL objFeedRetPL) {     DataTable GetListID = new DataTable();     try     {         SqlParameter[] arParams = new SqlParameter[4];          arParams[0] = new SqlParameter("@Date", typeof(DateTime));         arParams[0].Value = objFeedRetPL.requestdate;      }     catch (Exception ex)     {         string dir = @"C:\Error.txt";  // folder location         if (!Directory.Exists(dir))         {             Directory.CreateDirectory(dir);             File.AppendAllText(Server.MapPath("~/Error.txt"), "Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +        "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());             string New = Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine;             File.AppendAllText(Server.MapPath("~/Error.txt"), New);         }     } } 

Here, I want to save an Exception in "C:\" ..I am trying In DAL... How to save the Exception In C drive Error.txt

like image 280
Sambasiva Avatar asked Jan 23 '14 12:01

Sambasiva


People also ask

How do I create an exception to a text file?

So let us start by creating the application. "Start" - "All Programs" - "Microsoft Visual Studio 2010". "File" - "New" - "Project..." then select "C#" - "Empty Project" (to avoid adding a master page). Provide the project a name such as "ExceptionLoggingToTextFile" or another as you wish and specify the location.

How do I create an exception to a log file?

To log a handled exceptionCreate the method that will generate the exception information. Use a Try...Catch block to catch the exception. Put the code that could generate an exception in the Try block. Uncomment the Dim and MsgBox lines to cause a NullReferenceException exception.

How do you handle exceptions in C#?

Use a try block around the statements that might throw exceptions. Once an exception occurs in the try block, the flow of control jumps to the first associated exception handler that is present anywhere in the call stack. In C#, the catch keyword is used to define an exception handler.

What is exception c3?

An exception is defined as an event that occurs during the execution of a program that is unexpected by the program code. The actions to be performed in case of occurrence of an exception is not known to the program. In such a case, we create an exception object and call the exception handler code.


1 Answers

Since you want to save the exception to C:\Error.txt, you don't need Directory.Exists, Directory.CreateDirectory, or Server.MapPath("~/Error.txt"). You can simply use StreamWriter like this:

string filePath = @"C:\Error.txt";  Exception ex = ...  using( StreamWriter writer = new StreamWriter( filePath, true ) ) {     writer.WriteLine( "-----------------------------------------------------------------------------" );     writer.WriteLine( "Date : " + DateTime.Now.ToString() );     writer.WriteLine();      while( ex != null )     {         writer.WriteLine( ex.GetType().FullName );         writer.WriteLine( "Message : " + ex.Message );         writer.WriteLine( "StackTrace : " + ex.StackTrace );          ex = ex.InnerException;     } } 

The above code will create C:\Error.txt if it doesn't exist, or append C:\Error.txt if it already exists.

like image 130
ekad Avatar answered Sep 28 '22 06:09

ekad