I have a simple function to log errors that might happen when displaying certain pages.
I'm using the the null coalescing operator to make sure I dont have any null
's in my string.Format. Yet, I still see these errors in the event log:
Error: System.NullReferenceException: Object reference not set to an instance of an object. at ASP._core_showad_aspx.LogError(String sType, String sIP, Nullable`1 id, Nullable`1 id2)
void LogError(string sType, string sIP, int? id, int? id2)
{
string error = string.Format("'{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}'",
sType??"", sIP??"", id??-1, id2??-1,
Request.ServerVariables["HTTP_X_ORIGINAL_URL"]??"", Request.ServerVariables["URL"]??"", Request.ServerVariables["QUERY_STRING"]??"",
Request.Path??"", Request.ServerVariables["HTTP_REFERER"]??"", Request.ServerVariables["HTTP_USER_AGENT"]??"",
Request.ServerVariables["REMOTE_HOST"]??"", Request.ServerVariables["HTTP_COOKIE"].Substring(0, (Request.ServerVariables["HTTP_COOKIE"].Length>399)?399:Request.ServerVariables["HTTP_COOKIE"].Length)??"");
WriteLog(error);
}
Shouldn't the null coalescing operator prevent this from happening?