Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
C#
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
Posted

First off, don't try to save anything in the root folder of any drive, and particularly not in the root of your boot drive: From Vista onwards it takes special permission to avoid virus tampering, and it will not get any easier.

Second, if this is really a ASP.NET application, you will almost certainly never get permission from your web host to access the true root directory, as this could cause huge problems with other sites if permission was given.

So get rid of this completely:
C#
string dir = @"C:\Error.txt";  // folder location
if (!Directory.Exists(dir))
{
    Directory.CreateDirectory(dir);


And the rest of your code should work (but I'd use a folder away from my website root just to keep things tidy)
 
Share this answer
 
The most obvious is that C:\Error.txt is not a directory, but a file. So your catch block should be more like:
C#
catch (Exception ex) {
   string path = @"C:\Error.txt";  // file path
   using (StreamWriter sw = new StreamWriter(path, true)) { // If file exists, text will be appended ; otherwise a new file will be created
      sw.Write(string.Format("Message: {0}<br />{1}StackTrace :{2}{1}Date :{3}{1}-----------------------------------------------------------------------------{1}", ex.Message, Environment.NewLine, ex.StackTrace, DateTime.Now.ToString()));
   }
}


Be careful! You are trying to write at the root of the system drive ; it may cause problems, especially if this code is executed under IIS application pool identity. Better choose a directory inside your web root to write to.

Good luck.
 
Share this answer
 
You should have mentioned the error message & its related details. Possibly permission related issue.

But storing such things at ROOT drive(particularly C:\) is not a recommended way.

And my another suggestion is go with some opensource library like NLog[^], that'll reduce your headaches.
 
Share this answer
 
use this function in your business layer class file:-
public static void ErrorLog(Exception errorDtl, string subDtl)
{
	string err = null;
	if (errorDtl.Message != "Thread was being aborted.") {
		DateTime fileExistDate = default(DateTime);
		fileExistDate = DateTime.Today;
		if (System.IO.File.Exists("C:\\Users\\Logs\\Exception_" + Strings.Format(fileExistDate, "yyyyMMdd") + ".txt") == true) {
			err = Constants.vbCrLf + "Page Name :-" + HttpContext.Current.Request.Url.LocalPath;
			err = err + Constants.vbCrLf + "LogIn User :-" + HttpContext.Current.Session("Login");
			err = err + Constants.vbCrLf + "Error Log Date Time :-" + DateAndTime.Now;
			err = err + Constants.vbCrLf + "Error Message :-" + errorDtl.Message;
			err = err + Constants.vbCrLf + "Source :-" + errorDtl.Source;
			err = err + Constants.vbCrLf + "Trace :-" + errorDtl.StackTrace;

			string FILE_NAME = "C:\\Users\\Logs\\Exception_" + Strings.Format(fileExistDate, "yyyyMMdd") + ".txt";
			if (System.IO.File.Exists(FILE_NAME) == true) {
				System.IO.StreamWriter objWriter = null;

				objWriter = File.AppendText(FILE_NAME);
				objWriter.WriteLine("=====================================================================");
				objWriter.WriteLine(err);
				objWriter.Flush();
				objWriter.Close();
			}
		} else {
			System.IO.FileStream fileCreate = null;
			DateTime fileDate = default(DateTime);
			fileDate = DateTime.Today;
			if (System.IO.Directory.Exists("C:\\Users\\CRMLogs")) {
				fileCreate = System.IO.File.Create("C:\\Users\\Logs\\Exception_" + Strings.Format(fileDate, "yyyyMMdd") + ".txt ");
				fileCreate.Close();
			} else {
				System.IO.Directory.CreateDirectory("C:\\Users\\Logs");
				fileCreate = System.IO.File.Create("C:\\Users\\Logs\\Exception_" + Strings.Format(fileDate, "yyyyMMdd") + ".txt ");
				fileCreate.Close();
			}

			err = Constants.vbCrLf + "Page Name :-" + HttpContext.Current.Request.Url.LocalPath;
			err = err + Constants.vbCrLf + "LogIn User :-" + HttpContext.Current.Session("Login");
			err = err + Constants.vbCrLf + "Error Log Date Time :-" + DateAndTime.Now;
			err = err + Constants.vbCrLf + "Error Message :-" + errorDtl.Message;
			err = err + Constants.vbCrLf + "Source :-" + errorDtl.Source;
			err = err + Constants.vbCrLf + "Trace :-" + errorDtl.StackTrace;

			string FILE_NAME = "C:\\Users\\Logs\\Exception_" + Strings.Format(fileDate, "yyyyMMdd") + ".txt";
			if (System.IO.File.Exists(FILE_NAME) == true) {
				System.IO.StreamWriter objWriter = null;

				objWriter = File.AppendText(FILE_NAME);
				objWriter.WriteLine("=====================================================================");
				objWriter.WriteLine(err);
				objWriter.Flush();
				objWriter.Close();
			}
		}
	}
}



and add (Bl Class file name) StaticFunctions.ErrorLog(ex, "") in catch like:-
C#
catch (Exception ex) {
    StaticFunctions.ErrorLog(ex, "");
}
 
Share this answer
 
try this.. :)

C#
public static void WriteLogFile(string fileName, string methodName, string message) 

{ 

try
{ 

if (!string.IsNullOrEmpty(message)) 

{ 

using (FileStream file = new FileStream(Application.StartupPath + "\\Log.txt", FileMode.OpenOrCreate, FileAccess.Write)) 

{ 

StreamWriter streamWriter = new StreamWriter(file); 

streamWriter.WriteLine((((System.DateTime.Now + " - ") + fileName + " - ") + methodName + " - ") + message); 

streamWriter.Close(); 

} 

} 

} 

catch

{ 

} 

} 


reference.. :)
Log Error or Exception in a Text File[^]
 
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