简体   繁体   English

IIS中使用ASP.NET MVC进行控制器特定的iis日志记录

[英]controller specific iis logging with ASP.NET MVC in IIS

I want to log visits only for some controllers (or routes) as it was possible with classic ASP.NET pages by checking/unchecking the 'log visits' checkbox in IIS. 我只想记录某些控制器(或路由)的访问记录,因为经典的ASP.NET页可以通过选中/取消选中IIS中的“记录访问”复选框来记录访问记录。

Does anyone know if this is possible somehow? 有人知道这是否有可能吗? A solution without a custom logging component would be fantastic! 没有自定义日志记录组件的解决方案将是很棒的! Please share your knowledge, if you know how ;) 如果您知道如何,请分享您的知识;)

Thanks in advance 提前致谢

Create a BaseController which the controllers that you want to record data for inherit from. 创建一个BaseController,您要记录其数据的控制器将继承自该控制器。 Then create an ActionFilter which overrides the OnActionExecuted method and apply it to the base controller. 然后创建一个重写OnActionExecuted方法的ActionFilter并将其应用于基本控制器。 Something like this.. 像这样

public class ActionExecutedFilter : System.Web.Mvc.ActionFilterAttribute
{
    UnitOfWork unitOfWork= new UnitOfWork();

    public override void OnActionExecuted(ActionExecutedContext filter)
    {
        Transaction tran = new Transaction();
        tran.Controller = filter.ActionDescriptor.ControllerDescriptor.ControllerName;
        tran.ActionName = filter.ActionDescriptor.ActionName;
        tran.User = HttpContext.Current.User.Identity.Name;
        tran.Date = DateTime.Now;

        unitOfWork.TransactionRepository.Insert(tran);
        unitOfWork.Save();
    }
}

This will save to a database table called Transactions information for every time a action method is called on that controller, recording the user, controller and action method name. 每次在该控制器上调用一个操作方法时,它将保存到一个名为“事务信息”的数据库表中,记录用户,控制器和操作方法的名称。 Obviously I just typed in the UnitOfWork method of saving to the database, you can just plug in whichever method you like. 显然,我只是输入了保存到数据库的UnitOfWork方法,您可以插入任何您喜欢的方法。 I usually keep these methods in a filters folder, add a using statement then add it to the controller like so; 我通常将这些方法保存在过滤器文件夹中,添加一条using语句,然后将其添加到控制器中,如下所示:

[ActionExecutedFilter]
public class BaseController : Controller
{
}

Again, just make all the controller you wish to record data from inherit the BaseController. 同样,只需使您希望记录数据的所有控制器都继承BaseController即可。

You could also use something like Log4Net, but I find just doing it this way gets what I need. 您也可以使用Log4Net之类的东西,但是我发现以这种方式进行操作即可满足我的需求。 Whatever you think yourself. 随便你怎么想

http://dotnetdarren.wordpress.com/2010/07/29/logging-in-mvc-part-4-log4net/ http://dotnetdarren.wordpress.com/2010/07/29/logging-in-mvc-part-4-log4net/

A solution without a custom logging component would be fantastic! 没有自定义日志记录组件的解决方案将是很棒的!

Apart from basic IIS diagnostic logs, I cannot think of any ASP.Net MVC specific Logging features in IIS. 除了基本的IIS诊断日志外,我无法想到IIS中任何ASP.Net MVC特定的日志记录功能。

One simple solution what I want to offer is to write a HttpModule . 我要提供的一种简单解决方案是编写HttpModule In the HttpModule you can log the requests. 在HttpModule中,您可以记录请求。 Also if you want to have control on what to log and what not to, then based on Routes you can make that happen in HttpModule. 另外,如果您想控制要记录什么和不记录什么,则可以基于Routes在HttpModule中实现。 Advantage with HttpModule is it is easy to plug in and also easy to remove. HttpModule的优点是易于插入和删除。

Make HttpModule work only on certain routes 使HttpModule仅在某些路由上工作

When it comes to logging itself, you can have your own custom logic to database or to log file. 对于日志记录本身,您可以对数据库或日志文件有自己的自定义逻辑。 But you can opt for ELMAH or Log4Net or Enterprise Logging Application Block 但是您可以选择ELMAHLog4Net企业日志记录应用程序块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM