【问题标题】:Log Exception Error on Network在网络上记录异常错误
【发布时间】:2015-09-28 11:15:41
【问题描述】:

我想记录异常以防网络出错。我对 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");
                }

【问题讨论】:

  • 可能是权限问题,iis应用池用户是否有权限在你指定的路径上写?尝试向文件夹中的每个人授予写入权限,
  • 请检查您是否拥有对该服务器的读取、写入权限...这可能只是权限问题。

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


【解决方案1】:

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

【讨论】:

  • 我做了我手动测试的文件已创建并具有所有权限。 global.asax.dll 是唯一在出错时生成的 dll 吗?
  • 但是你说没有创建日志文件。那么究竟出了什么问题呢?
  • 您是否尝试过重新启动应用程序池。如果文件正在创建,那么有什么问题?请详细说明您的实际查询。
  • 基本上,日志文件是在本地测试时在本地主机上的网络上创建的。但似乎不适用于具有相同代码和日志文件的跨域,如果域上出现错误页面,异常不会显示结果。
  • 这可能是您的 web.config 文件有问题,导致 Application_Error 无法按预期工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-16
  • 2010-10-07
  • 2010-11-19
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多