{C# - Logging} Application_BeginRequest()对象引用未设置为对象的实例

时间:2016-10-23 23:57:28

标签: c# asp.net logging global-asax

嘿伙计我有一个Web应用程序,有时当我将它发布到服务器时它会让我发疯,因为它没有加载。它返回System.NullReferenceException: Object reference not set to an instance of an object的错误,并在Global.asax Application_BeginRequest上点击(我尝试了Application_AuthenticateRequest - 同样的结果)。

Global.asax Application_BeginRequest代码:

    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来源:

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();
    }
}

错误正在发生,有时它通常会起作用所以我不知道我做错了什么。感谢所有回复...

1 个答案:

答案 0 :(得分:0)

嗯,最好的答案是调试或收集完整的堆栈跟踪,以找出正在抛出的特定行。

一个明显的问题 - 如果您捕获的异常不是HttpException会怎么样?例如,如果您的日志记录代码在StreamWriter的构造函数中有异常怎么办?它可以抛出七个不同的异常,其中没有一个是HttpExceptions。

你最终进入了catch,code的值将为null,因为异常无法转换为HttpException,而code.GetHttpCode()将抛出NullReferenceException。