elmah / Elmah

Error Logging Modules & Handlers for ASP.NET
https://elmah.github.io/
Apache License 2.0
306 stars 65 forks source link

Elmah does not capture Post data of request #425

Closed ankit57 closed 6 years ago

ankit57 commented 6 years ago

Elmah currently does not support logging and displaying the Post Data of the request. With all the new paradigm change of how we look at the web application now, Rest API are used pretty heavily. Capturing Post data will give more insight to the error and help debugging and reproducing the issue.

I have made the change and implemented it successfully in one of my application and wish that same be incorporated in base code so everyone can leverage the feature. I can push the code change if you make me collaborator else high level change is in Error.cs file to read the body from context.Request.InputStream. Displaying it on the log page is simple.

atifaziz commented 6 years ago

General post data is problematic to log. See my comment in issue #164 on why.

I can push the code change if you make me collaborator

You don't need permissions to share your work. You can do it via your own fork. You can also submit a PR here but it would be best if we discuss and agree on the design of your implementation before. As I said, it's problematic so I'm curious as to how you went about solving it.

ankit57 commented 6 years ago

In Error.cs file, we can capture InputStream of Request and use StreamReader to get the string. Below is sample code

string jsonString = String.Empty;

            try
            {
                if (context.Request.InputStream != null)
                {
                    context.Request.InputStream.Position = 0;
                    using (StreamReader inputStream = new StreamReader(context.Request.InputStream))
                    {
                        jsonString = inputStream.ReadToEnd();
                    }
                }
            }
            catch (Exception)
            {
            }
            _postData = jsonString;
atifaziz commented 6 years ago

@ankit57 This assumes that InputStream is entirely committed to memory and can be rewound back (Position = 0). Unless you can point me to docs that state so, the assumption may not always hold true.

atifaziz commented 6 years ago

Closing this as by-design.