最近我发现了惊人的 AOP 框架PostSharp
,它似乎可以直接开箱即用。一切都很好,我还使用 log4net 实现了自定义日志记录方面,直到我决定将我的应用程序上传到共享主机上。
它不适用于我的共享主机。在使用自定义方面的页面上,我收到以下错误:
Sorry, an error occurred while processing your request.
我想知道是否需要调整一些东西才能让 PostSharp 在共享主机上工作?
另外,我怎样才能在失败的页面上抛出有用的异常消息,因为我收到的消息根本没有帮助?
我的方面如下:
[Serializable]
public class LoggingAspect : OnMethodBoundaryAspect
{
//Here is the once-per-class call to initialize the log object
//[NonSerialized]
//private static readonly ILog log = LogManager.GetLogger("Logger");
private string methodName;
private string className;
private Type declaringType;
/// <summary>
/// Method executed at build time. Initializes the aspect instance. After the execution
/// of <see cref="CompileTimeInitialize"/>, the aspect is serialized as a managed
/// resource inside the transformed assembly, and deserialized at runtime.
/// </summary>
/// <param name="method">Method to which the current aspect instance
/// has been applied.</param>
/// <param name="aspectInfo">Unused.</param>
public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
{
this.methodName = method.Name;
this.className = method.DeclaringType.FullName;
this.declaringType = method.DeclaringType;
}
/// <summary>
/// Method invoked before the execution of the method to which the current
/// aspect is applied.
/// </summary>
/// <param name="args">Information about the method being executed.</param>
public override void OnEntry(MethodExecutionArgs args)
{
//log.Debug(this.className + "." + this.methodName + ": Enter");
}
/// <summary>
/// Method invoked after successfull execution of the method to which the current
/// aspect is applied.
/// </summary>
/// <param name="args">Information about the method being executed.</param>
public override void OnSuccess(MethodExecutionArgs args)
{
//log.Debug(this.className + "." + this.methodName + ": Success");
}
/// <summary>
/// Method invoked after failure of the method to which the current
/// aspect is applied.
/// </summary>
/// <param name="args">Information about the method being executed.</param>
public override void OnException(MethodExecutionArgs args)
{
//log.Error(this.className + "." + this.methodName + ": Exception " + args.Exception.Message);
}
}
如您所见,我已经使用 log4net 注释掉了所有行,只是为了证明问题不是由于 log4net 引起的。好吧,如果我包含 log4net,我会在我可以让 PostSharp 工作后解决的其他问题中运行。因此,即使只有空方法 PostSharp 也无法正常工作。有人可以指出这里缺少什么吗?
我想补充一点,PostSharp 和 log4net 在调试模式下在 localhost 上工作得很好。我没有收到任何错误,并且问题仅发生在共享主机上。
更新
我再次尝试了相同的代码,并将customErrors
mode
属性设置为off
in web.config
。我现在得到以下异常:
Exception Details: System.Security.SecurityException: Request failed.
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
Exception Details: System.Security.SecurityException: Request failed.
Stack Trace:
[SecurityException: Request failed.]
PostSharp.Aspects.Serialization.BinaryAspectSerializer..cctor() +0
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18055
这是否意味着我需要联系共享主机提供商来更改Trust
我的应用程序级别?我希望有解决这个问题的方法?