2015-09-28 68 views
0

我想在网络出现错误时记录异常。我对global.asax文件进行了更改,并在其中生成了日志文件。该代码在本地主机上正常工作,但是当我将另一台服务器上的global.asax文件的dll上传并根据类似于本地主机的网络凭证更改Web配置时。如果出现错误,该方法似乎不起作用,也不会创建写入该异常的文件。请帮助网络上的日志异常错误

void Application_Error(object sender, EventArgs e) 
     { 
      // Code that runs when an unhandled error occurs 
      try 
      { 
       DffUtility.AddCookie("verystart", "Very start of error"); 
       Exception exc = Server.GetLastError(); 

       Uri refurl = Request.UrlReferrer; 
       DffUtility.AddCookie("star655", "getting exception start"); 
       string networkLogFolderPath = ConfigurationManager.AppSettings["Logpath"] + "\\" + DffUtility.WebSiteInfo.Folder + "\\" + DffUtility.WebSiteInfo.ThemeName + "\\"; 
       DffUtility.AddCookie("path", networkLogFolderPath.ToString()); 
       Network.connectToRemote(ConfigurationManager.AppSettings["Logpath"], 
     ConfigurationManager.AppSettings["networkusername"], 
     ConfigurationManager.AppSettings["pass"]); 
       string LogFolderPath = HttpContext.Current.Server.MapPath("~/ExceptionLogFiles/"); 
       string filePath = networkLogFolderPath; 


       string stacktracemessage, stacktrace, Errormsg, extype, exurl; 
       stacktracemessage = (exc.InnerException).Message; 

       stacktrace = exc.ToString(); 

       Errormsg = exc.GetType().Name.ToString(); 
       extype = exc.GetType().ToString(); 
       exurl = HttpContext.Current.Request.Url.ToString(); 

       if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(filePath))) 
        System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filePath)); 
       if (DffUtility.Country > 0) 
       { 
        if (DffUtility.RegionArea > 0) 
        { 
         if (DffUtility.RegionCity > 0) 
         { 
          if (DffUtility.ProdID > 0) 
          { 
           filePath = (filePath + "Product_" + DffUtility.ProdID + "_" + DffUtility.RegionCity + "_" + DffUtility.RegionArea + "_" + DffUtility.Country + ".txt"); 
          } 
          else 
          { 
           filePath = (filePath + "City_" + DffUtility.RegionCity + "_" + DffUtility.RegionArea + "_" + DffUtility.Country + ".txt"); 
          } 
         } 
         else 
         { 
          filePath = (filePath + "Area_" + DffUtility.RegionArea + "_" + DffUtility.Country + ".txt"); 
         } 
        } 
        else 
        { 
         filePath = (filePath + "Country_" + DffUtility.Country + ".txt"); 

         DffUtility.AddCookie("start8", "After generating product file"); 
        } 
       } 
       System.IO.File.Create(filePath).Dispose(); 

       using (System.IO.StreamWriter sw = System.IO.File.AppendText(filePath)) 
       { 
        string logFormat = Environment.NewLine + " " + Environment.NewLine; 

        DffUtility.AddCookie("start6", "in"); 
        string error = "Error Message:" + " " + Errormsg + logFormat + "Exception Type:" + " " + extype + logFormat + " Error Page Url:" + " " + exurl + logFormat + " StackTraceMessage:" + " " + stacktracemessage + logFormat + " StackTrace:" + " " + stacktrace + logFormat + " " + refurl + logFormat; 
        sw.WriteLine("-----------Exception Details on " + " " + DateTime.Now.ToString() + "-----------------"); 
        sw.WriteLine("-------------------------------------------------------------------------------------"); 

        sw.WriteLine(error); 

        sw.WriteLine(logFormat); 
        sw.Flush(); 
        sw.Close(); 
        DffUtility.AddCookie("start4", " after getting generated log"); 
       } 
+0

可能是其权限问题,iis应用程序池用户是否有权在您指定的路径上写入? 尝试给该文件夹上的所有人写入权限, –

+0

请检查您是否具有读取和写入该服务器的权限......可能只有权限问题。 – pedram

回答

0

这是因为应用程序在网络服务帐户下运行(默认),并没有权限在ExceptionLogFiles文件夹来写。添加用户所有人在文件夹属性中授予他该权限

+0

我做了我手动测试的文件创建并拥有所有权限。是global.asax.dll唯一的DLL在错误的情况下生成的? –

+0

但你说这个日志文件没有创建。那么究竟是哪里出了问题? – 2015-09-28 11:29:33

+0

您是否尝试过重新启动应用程序池。如果文件正在创建,那么有什么问题?请详细说明您的实际查询。 – pedram