Hey guys I have a web application and some times when I publish it to the server it drives me crazy cause it's not loading. It returns an error of System.NullReferenceException: Object reference not set to an instance of an object
and it hits on Global.asax Application_BeginRequest
(I tried Application_AuthenticateRequest
- same result).
Global.asax Application_BeginRequest
code:
try
{
HttpRequest request = HttpContext.Current.Request;
string header = request.Headers["X-MicrosoftAjax"];
if(header != "Delta=true")
{
string path = "~/Logs/TrafficAnalytics.txt";
string txt = "Visit from: " + ClientInfo.GetClientIP() + " at: " + ClientInfo.GetClientLastRoute + ". Traceback below:\r\nIP Address: " + ClientInfo.GetClientIP() + "\r\nHostname: " + ClientInfo.GetClientHostname + "\r\nCoordinates: " + ClientInfo.GetClientCoordinates() + "\r\nLocation: " + ClientInfo.GetClientLocation() + "\r\nOS & Browser Info: " + ClientInfo.GetClientBrowserOs + "\r\nBrowser Info: " + ClientInfo.GetClientBrowserInfo() + "\r\nOperating System: " + ClientInfo.GetClientOsInfo() + "\r\nISP: " + ClientInfo.GetClientIsp();
Logger.Create(path, txt);
}
}
catch (Exception ex)
{
HttpContext con = HttpContext.Current;
HttpException code = ex as HttpException;
string bos = Request.UserAgent;
string path = "~/Logs/ErrorReports.txt";
string txt = "URL: " + con.Request.Url.ToString() + "\r\nError Message: " + ex.Message + "\r\nAnalyzed Message: " + ex.ToString() + "\r\nError Code: " + code.GetHttpCode().ToString() + "\r\nIpAddressTraceResult: " + ClientInfo.GetClientIP() + "\r\nOS & Browser Info: " + bos;
Logger.Create(path, txt);
}
Logger.cs
source:
using System;
using System.IO;
using System.Web;
public class Logger
{
public static void Create(string p, string t)
{
FileInfo path = new FileInfo(HttpContext.Current.Server.MapPath(p));
string txt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff") + " :::::::::: " + t + "\r\n------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
path.IsReadOnly = false;
StreamWriter sw = new StreamWriter(path.ToString(), true);
sw.WriteLine(txt);
sw.Flush();
sw.Close();
}
}
The error is happening only sometimes it usualy works so I don't know what am I doing wrong. Thanks for all responses...