62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Web;
using ZeroStock.Data;
using ZeroStock.API;
using ZeroStock.Config;
using ZeroStock.Security;
using ZeroStock.Common;
using System.Runtime.InteropServices;
namespace ZeroStock.Components
{
public class ZSException : ApplicationException
{
string message=string.Empty;
public ZSExceptionType ExceptionType { get; set; }
public SystemExceptionLogInfo ExceptionLogInfo { get; set; }
private const string RESOURCE_CLASS_NAME = "Exception";
public int _authrityID = 0;
public ZSException(ZSExceptionType t):base (){
this.ExceptionType = t;
Init();
}
/// <summary>
/// 访问拒绝异常
/// </summary>
/// <param name="authrityID">所需权限ID</param>
public ZSException(int authrityID):base()
{
ExceptionType = ZSExceptionType.AccessDenied;
_authrityID = authrityID;
}
/// <summary>
///
/// </summary>
/// <param name="t"></param>
/// <param name="msg"></param>
public ZSException(ZSExceptionType t, string msg) : base(msg)
{
this.ExceptionType = t;
this.message = msg;
Init();
}
public ZSException(ZSExceptionType t, string msg, Exception innerException)
: base(msg, innerException)
{
this.ExceptionType = t;
this.message = msg;
Init();
}
public override string Message
{
get
{
switch (ExceptionType)
{
case ZSExceptionType.AccessDenied:
string authInfo = Authoritys.GetName(_authrityID);
return string.Format(
HttpContext.GetGlobalResourceObject(RESOURCE_CLASS_NAME, ExceptionType.ToString()).ToString(),
authInfo);
case ZSExceptionType.ScheduleRunFailed:
return string.Format(
HttpContext.GetGlobalResourceObject(RESOURCE_CLASS_NAME, ExceptionType.ToString()).ToString(),
message);
default :
return HttpContext.GetGlobalResourceObject(RESOURCE_CLASS_NAME, ExceptionType.ToString()).ToString();
}
}
}
public void Log()
{
SystemExceptionLogs oSystemException = new SystemExceptionLogs();
try
{
ExceptionLogInfo = oSystemException.InsertOrUpdate(this.ExceptionLogInfo);
if (Utils.InternetConnecting())
{
ZSSAPI.ReportException(ConfigProvider.GetClientInfo(), ExceptionLogInfo);
}
}
catch { }
}
void Init()
{
try
{
int hash = (ExceptionType + this.ToString()).GetHashCode();
SystemExceptionLogs oSystemException = new SystemExceptionLogs();
ExceptionLogInfo = new SystemExceptionLogInfo()
{
Category = ExceptionType,
CreatedDate = DateTime.Now,
LongMessage = base.GetBaseException().ToString(),
ShortMessage = Message,
Frequency = 1,
LastOccurredDate = DateTime.Now,
ExceptionHash=hash
};
HttpContext context = HttpContext.Current;
// This was failing when trying to access the database that we didn't have permissions to. When this is
// happening, the application is first loading (LoadSiteSettings) and not all of this context information
// is present. Because of this, we were getting an exception in the Exception class which defeats
// the whole purpose of having this class. Adding some additional checks to ensure we don't throw an
// exception in our exception constructor
if (context != null &&
context.Request != null)
{
if (context.Request.UrlReferrer != null)
ExceptionLogInfo.UrlReferrer = context.Request.UrlReferrer.ToString();
if (context.Request.UserAgent != null)
ExceptionLogInfo.UserAgent = context.Request.UserAgent;
if (context.Request.UserHostAddress != null)
ExceptionLogInfo.IPAddress = context.Request.UserHostAddress;
// "forumContext.Context.Request.Url != null" check was added because
// , similarly to above, the Url property will be null when this method is called
// from the ForumsHttpModule.ScheduledWorkCallbackEmailInterval timer callback.
if (context.Request != null
&& context.Request.Url != null
&& context.Request.Url.PathAndQuery != null)
ExceptionLogInfo.HttpPathAndQuery = context.Request.Url.PathAndQuery;
// Added to have Log() working. The table columns that hold
// all exception details doesn't support null values. In certain circumstances
// adding exception details to database for thrown exception might run into an
// unhandled exception: a new exception is thrown while current exception
// processing is not finished (ForumsHttpModule.Application_OnError).
if (context.Request != null
&& context.Request.UrlReferrer != null
&& context.Request.UrlReferrer.PathAndQuery != null)
ExceptionLogInfo.UrlReferrer = context.Request.UrlReferrer.PathAndQuery;
}
}
catch { }
}
}
}
public ZSException(ZSExceptionType t):base (){
this.ExceptionType = t;
Init();
}
/// <summary>
/// 访问拒绝异常
/// </summary>
/// <param name="authrityID">所需权限ID</param>
public ZSException(int authrityID):base()
{
ExceptionType = ZSExceptionType.AccessDenied;
_authrityID = authrityID;
}
/// <summary>
///
/// </summary>
/// <param name="t"></param>
/// <param name="msg"></param>
public ZSException(ZSExceptionType t, string msg) : base(msg)
{
this.ExceptionType = t;
this.message = msg;
Init();
}
public ZSException(ZSExceptionType t, string msg, Exception innerException)
: base(msg, innerException)
{
this.ExceptionType = t;
this.message = msg;
Init();
}
public class ZSException : ApplicationException
{
public ZSException () {}
public ZSException (string message) : base(message) {}
public ZSException (string message, Exception e) : base(message, e) {}
}
public class ZSException : IHttpModule
public class ZSHttpModule : IHttpModule
{
void IHttpModule.Init(HttpApplication context)
{
context.Error += new EventHandler(this.Application_OnError);
}
public void Application_OnError(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
ZSException zsException = context.Server.GetLastError() as ZSException;
if (zsException == null)
zsException = context.Server.GetLastError().GetBaseException() as ZSException;
try
{
....//重定向到自定义的错误页面