/// <summary>
/// 记录日志
/// </summary>
/// <param name="ex">出错的信息</param>
public static void LogRecord(Exception ex)
{
Mongo mongoDBLog = null;
try
{
mongoDBLog = new Mongo(ConfigurationManager.AppSettings["mongoDBConfig"]);
mongoDBLog.Connect();
var dbLog = mongoDBLog.GetDatabase("USTALogs");
var collection = dbLog.GetCollection<USTALogs>(DateTime.Now.Date.ToString("yyyy-MM-dd"));
USTALogs ustaLogs = new USTALogs
{
errorTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
errorURL = HttpContext.Current.Request.Url.ToString(),
accessIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"],
errorCode = System.Runtime.InteropServices.Marshal.GetHRForException(ex).ToString(),
errorObject = ex.Source,
errorStackTrace = ex.StackTrace,
errorMessage = ex.Message,
errorHelpLink = ex.HelpLink,
errorMethod = (ex.TargetSite == null ? string.Empty : ex.TargetSite.ToString())
};
collection.Insert(ustaLogs);
}
catch (Exception mongoDBLogException)
{
string mongoDBInfoError = HttpContext.Current.Server.MapPath("/LogFiles/WriteMongoDBInfoError_" + DateTime.Now.Date.ToString("yyyy-MM-dd"));
if (!System.IO.File.Exists(mongoDBInfoError))
{
System.IO.File.Create(mongoDBInfoError);
}
}
finally
{
if (mongoDBLog != null)
{
mongoDBLog.Disconnect();
mongoDBLog.Dispose();
}
}
////错误信息
//string exceptionMessage = "\n出错页面地址为:" + HttpContext.Current.Request.Url.ToString() + "\n\n访问者IP为:" + HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] + "\n" + "\n异常码为:" + System.Runtime.InteropServices.Marshal.GetHRForException(ex) + "\n出错对象为:" + ex.Source + "\n堆栈信息为:" + ex.StackTrace + "\n出错函数为:" + ex.TargetSite + "\n出错信息为:" + ex.Message + "\n参考帮助链接为:" + ex.HelpLink + "\n\n";
////记录错误日志
//log.Error(exceptionMessage);
}