如何在txt文件中保存异常? [英] how to save exception in txt file ?

查看:102
本文介绍了如何在txt文件中保存异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 (例外情况)
{
string dir = @ C:\ Error.txt; // 文件夹位置
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
File.AppendAllText(Server.MapPath( 〜/ Error.txt),< span class =code-string> 消息: + ex.Message + < br /> + Environment.NewLine + StackTrace: + ex.StackTrace +
+ Environment .NewLine + 日期: + DateTime.Now.ToString());
string 新= Environment.NewLine + -------------------------------------------------- --------------------------- + Environment.NewLine;
File.AppendAllText(Server.MapPath( 〜/ Error.txt),新增功能);
}
}
}



这里,我想在C:\中保存一个例外。 。我正在尝试在DAL ...如何保存例外在

C盘Error.txt

解决方案

首先,不要'尝试在任何驱动器的根文件夹中保存 任何 ,特别是不在启动驱动器的根目录中:从Vista开始,需要特殊权限才能避免病毒篡改,并且它不会变得更容易。



其次,如果这是一个真正的ASP.NET应用程序,你几乎肯定不会从你的网站主机获得访问权限真正的根目录,因为如果获得许可,这可能会导致其他网站出现巨大问题。



完全摆脱这个:

< pre lang =cs> string dir = @ C:\Error.txt; // 文件夹位置
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);





And其余的代码应该可以工作(但我只是为了保持整洁而使用远离我的网站根目录的文件夹)


最明显的是C:\ Error.txt是不是目录,而是文件。所以你的catch块应该更像:

  catch (exception ex){
< span class =code-keyword> string path = @ C:\ Error.txt ; // 文件路径
使用(StreamWriter sw = new StreamWriter(path, true )){ // 如果文件存在,将附加文本;否则将创建一个新文件
sw.Write( string .Format( 消息:{0}< br /> {1} StackTrace:{2} {1}日期:{3} {1} -------- -------------------------------------------------- ------------------- {1},ex.Message,Environment.NewLine,ex.StackTrace,DateTime.Now.ToString()));
}
}





小心!您正在尝试写入系统驱动器的根目录;它可能会导致问题,尤其是在IIS应用程序池标识下执行此代码时。最好选择web根目录中的目录来写入。



祝你好运。


你应该已经提到过错误信息& ;其相关细节。可能是与许可相关的问题。



但是不建议将这些东西存放在ROOT驱动器(特别是C:\)。



我的另一个建议是使用一些开源库,如 NLog [ ^ ],这将减轻您的头痛

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

解决方案

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:

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)


The most obvious is that C:\Error.txt is not a directory, but a file. So your catch block should be more like:

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.


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.


这篇关于如何在txt文件中保存异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆