【问题标题】:ASP.NET MVC 4 File Upload via Ajax Error LoggingASP.NET MVC 4 通过 Ajax 错误日志上传文件
【发布时间】:2016-05-04 23:22:34
【问题描述】:

我有这个 ajax 调用:

$.ajax({
                                            type: "POST",
                                            url: '/Home/Upload?customerID=' + customerID + "&questionID=" + id + "&community=" + communitySelected + "&lot=" + lotSelected,
                                            data: formData,
                                            dataType: 'json',
                                            contentType: false,
                                            processData: false,
                                            success: function (response) {
                                                alert('success!!');
                                                $("#" + id).attr('disabled', false);
                                            },
                                            error: function (error) {
                                                alert(error);
                                                console.log(error);
                                            }
                                        });

这叫这个:

[HttpPost]
        public ActionResult Upload(int customerID, int questionID, string community, int lot)
        {

            for (int i = 0; i < Request.Files.Count; i++)
            {

                    var file = Request.Files[i];

                    string path = Path.Combine(Server.MapPath("~/UploadedFiles"),
                                                   Path.GetFileName(customerID + "-" + community + lot + "-" + questionID + "-" + file.FileName));

                    file.SaveAs(path);

            }

            return Json(new { success = true },
            "text/plain");

        }

现在这一切都在我的本地主机上运行,​​但是当我将它放在服务器上并尝试上传文件时,我收到 500 错误。我现在要做的是在 .NET 端添加错误日志以查看究竟是什么问题,所以我的问题是如何调整我的 .NET 方法来显示错误。

我试着像这样尝试并抓住:

[HttpPost]
        public ActionResult Upload(int customerID, int questionID, string community, int lot)
        {

            for (int i = 0; i < Request.Files.Count; i++)
            {
                try
                {

                    var file = Request.Files[i];

                    string path = Path.Combine(Server.MapPath("~/UploadedFiles"),
                                                   Path.GetFileName(customerID + "-" + community + lot + "-" + questionID + "-" + file.FileName));

                    file.SaveAs(path);
                }
                catch (Exception e)
                {
                    Console.Write(e);
                }

            }

            return Json(new { success = true },
            "text/plain");

        }

但是如何显示错误?

谢谢,

【问题讨论】:

    标签: c# jquery asp.net ajax asp.net-mvc


    【解决方案1】:

    如果不进行重大更改,最简单的做法就是写入文本文件:

    System.IO.File.WriteAllText(@"C:\SomeFolder\error.txt", text);
    

    确保站点可以写入文件夹。

    How to Write to a Text File on MSDN 上有更多选项。

    (但您应该考虑在应用程序中构建适当的错误处理和报告,例如下面的 ELMAH。)


    如果可能,您可以重新抛出异常,以便显示 YSOD。

    catch (Exception e)
    {
        throw;
    }
    

    Elmah 是错误记录的一个选项。

    例如

    catch(Exception e)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(e);
    }
    

    另请参阅,how to use ELMAH to manually log errors

    【讨论】:

      【解决方案2】:

      您可以编写一个单独的类来记录错误,这里是代码框架:

      public static class ErrorLog
      {
          public static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
      
          public static void LogError(System.Exception ex)
          {
              try
              {
                  log.Error("ERROR", ex);
              }
              catch (System.Exception)
              {
              }
          }
      }
      

      当你使用 Errorlog 类时,你可以这样写:

          try
          {
              //Your code here...
          }
          catch (Exception ex)
          {
              ErrorLog.LogError(ex);
              throw;
          }
      

      ErrorLog.Log.Debug("Debug message plus whatever you want to display here");
      

      文件路径可以是绝对路径,例如“c:\logs\log.txt”,也可以是相对于 bin 目录的路径。希望这对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2011-06-22
        • 2012-07-27
        • 1970-01-01
        • 2021-12-10
        • 1970-01-01
        • 2012-12-25
        • 2011-02-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多