2015-09-28 78 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