Aop、Unity、拦截器和 ASP.NET MVC 控制器操作方法

发布于 2024-08-03 01:17:29 字数 915 浏览 1 评论 0原文

使用 log4net,我们希望记录对 ASP.NET MVC 控制器操作方法的所有调用。

日志应包含有关传递给控制器​​的任何参数的信息。

我们希望使用带有 的 AoP 方法,而不是在每个操作方法中手动编码。通过 Unity 拦截器

我们已经将其与其他一些使用接口的类一起使用(使用InterfaceInterceptor)。但是,我们不确定如何继续使用我们的控制器。我们是否应该稍微重新设计它们以使用界面,或者是否有更简单的方法?

编辑

VirtualMethodInterceptor 似乎是正确的方法,但是使用此方法会导致以下异常:

System.ArgumentNullException: Value cannot be null.
Parameter name: str
   at System.Reflection.Emit.DynamicILGenerator.Emit(OpCode opcode, String str)
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)

Using log4net we would like to log all calls to our ASP.NET MVC controller action methods.

The logs should include information about any parameters that were passed to the controller.

Rather than hand-coding this in each action method we hope to use an AoP approach with Interceptors via Unity.

We already have this working with some other classes that use interfaces (using the InterfaceInterceptor). However, we're not sure how to proceed with our controllers. Should we re-work them slightly to use an interface, or is there a simpler approach?

Edit

The VirtualMethodInterceptor seems to be the correct approach, however using this results in the following exception:

System.ArgumentNullException: Value cannot be null.
Parameter name: str
   at System.Reflection.Emit.DynamicILGenerator.Emit(OpCode opcode, String str)
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2024-08-10 01:17:29

您看过这个 [链接损坏]

以下是由 David Hayden 提供的损坏/丢失博客文章的摘录

这是一个使用企业库日志记录应用程序块<的 LogFilterAttribute /strong> 的 LogWriter 用于记录消息。

public class LogFilterAttribute : ActionFilterAttribute
{
    public LogWriter Logger { get; set; }
 
    public string Category { get; set; }
 
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        string message = string.Format(
            "{0} ActionMethod on {1}Controller executing...",
            filterContext.ActionDescriptor.ActionName,
            filterContext.ActionDescriptor.ControllerDescriptor.ControllerName);
 
        var category = Category ?? "General";
 
        Logger.Write(new LogEntry {
            Message = message,
            Categories = new[] { category }
        });
    }
}

Have you seen this [link broken]

Following is an excerpt from the broken/missing blog post courtesy of David Hayden

Here is a LogFilterAttribute that uses the Enterprise Library Logging Application Block's LogWriter for logging messages.

public class LogFilterAttribute : ActionFilterAttribute
{
    public LogWriter Logger { get; set; }
 
    public string Category { get; set; }
 
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        string message = string.Format(
            "{0} ActionMethod on {1}Controller executing...",
            filterContext.ActionDescriptor.ActionName,
            filterContext.ActionDescriptor.ControllerDescriptor.ControllerName);
 
        var category = Category ?? "General";
 
        Logger.Write(new LogEntry {
            Message = message,
            Categories = new[] { category }
        });
    }
}
祁梦 2024-08-10 01:17:29

不完全是您所要求的,但您可能需要考虑 log4postsharp。也许您还需要这个,但我不知道当然,因为到目前为止我还没有使用 ASP.Net MVC

not exactly what you are asking, but you might want to consider log4postsharp. maybe you will need this as well, but I cannot tell for sure as I did not use ASP.Net MVC so far

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文