网络上的日志异常错误

时间:2015-09-28 11:15:42

标签: c# asp.net visual-studio-2012 global-asax

我想在网络出现错误的情况下记录Exception。我在global.asax文件中进行了更改并生成了日志文件。代码在localhost上正常工作,但是当我在另一台服务器上上传global.asax文件的dll时,根据类似于localhost的网络凭据更改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");
                }

1 个答案:

答案 0 :(得分:0)

这是因为您的应用程序在NETWORK SERVICE帐户下运行(默认情况下),并且没有权限在 ExceptionLogFiles 文件夹中写入。在文件夹属性中添加用户所有人并授予他该权限