This project is read-only.
1
Vote

LogMessageBuilder HttpContext Exception

description

In LogMessageBuilder class function HttpContext() I kept getting an exception of "object reference not set to an instance of an object". The issue arrises when you have a non null Request.QueryString,
var queryString = request.QueryString;

            if (queryString != null)
            {
                _queryString = new NameValueCollection();
                for (int x = 0; x < queryString.Count; x++)
                {
                    string key = queryString.AllKeys[x];
                    string value = queryString.Get(x);
                    ___serverVariables__.Add(string.Format("HttpRequest.QueryString[{0}]", key), value);
                }
            }
_serverVariables is used before it is set which occurs below this piece of code. Unless I have missed something I don't see any dependency injection happening that sets this before hand. I simply moved the following code,
var serverVariables = System.Web.HttpContext.Current.Request.ServerVariables;
            if (serverVariables != null && serverVariables.Count > 0)
            {
                _serverVariables = new NameValueCollection();
                for (int x = 0; x < serverVariables.Count; x++)
                {
                    string key = serverVariables.AllKeys[x];
                    if (killWords.Any(w => string.Compare(w, key) == 0))
                        continue;
                    string value = serverVariables.Get(x);
                    _serverVariables.Add(string.Format("HttpRequest.ServerVariable[{0}]", key), value);
                }
            }
above the query string test. I also added the if (serverVariables != null && serverVariables.Count > 0)

comments

HashtagDotNet wrote Oct 9, 2014 at 2:16 AM

Thank-you for your report and for your kind fix. I'll add that to the next release (which I am hoping to be in the next few weeks).