0

I have this ajax call:

$.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);
                                            }
                                        });

which calls this:

[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");

        }

Now this all works on my localhost, but when I put it on a server and try to upload a file, I get a 500 error. What I am trying to do now is add error logging on the .NET side to see what exactly the problem is, so my question is how would I adjust my .NET method to show me the error.

I tried to do a try and catch like so:

[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");

        }

but how do I display the error?

Thanks,

user979331
  • 10,209
  • 56
  • 186
  • 339

2 Answers2

1

Without doing major changes the simplest thing to do would be to write to a text file:

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

Make sure the site can write to the folder tho.

There are more options on How to Write to a Text File on MSDN.

(But you should look at building proper error handling and reporting into the app, e.g. ELMAH below.)


If possible you could rethrow the exception, so that a YSOD would display.

catch (Exception e)
{
    throw;
}

Elmah is an option for the error logging.

E.g.

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

See also, how to use ELMAH to manually log errors.

Community
  • 1
  • 1
NikolaiDante
  • 17,673
  • 14
  • 75
  • 112
0

You can write a separate class for error logging, here is skeleton of code:

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)
        {
        }
    }
}

When you use Errorlog class, you can write something like this:

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

OR

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

The file path can either be an absolute path like "c:\logs\log.txt" or a path relative to the bin directory. Hope this may help you.

Void
  • 153
  • 1
  • 10