简体   繁体   English

MVC Web解决方案上的Log4Net

[英]Log4Net on a MVC web solution

I'm creating a solution that contains two projects: the web project ant it's test project. 我正在创建一个包含两个项目的解决方案:Web项目和测试项目。 I'm using Log4net for logging purposes, CastleWindsor for dependency injection and moq for the tests. 我使用Log4net进行日志记录,使用CastleWindsor进行依赖项注入,并使用moq进行测试。

The problem I have is the configuration of the test. 我的问题是测试的配置。 I want to test the HomeController that is Logging to a file, but when I run the test I don't want to Log, I think is absurd. 我想测试正在记录到文件的HomeController,但是当我运行测试时,我不想记录日志,这是荒谬的。

Is there a way to skip Logging in the test project? 有没有一种方法可以跳过登录测试项目?

The HomeController class: HomeController类:

public class HomeController : Controller
{
    // this is Castle.Core.Logging.ILogger, not log4net.Core.ILogger
    public ILogger Logger { get; set; }

    private IRowan _rowan;

    public HomeController(IRowan rowan)
    {
        _rowan = rowan;

    }

    public ActionResult Index()
    {
        //In the [tests] Logger fails
        Logger.Debug("GET Request traced");
        Logger.Error("Example of Error");


        String test = _rowan.DoSomething();
        ViewBag.Title = test;
        return View();
    }
}

} }

It's test: 测试:

[TestClass]
public class HomeControllerTest
{
    private static WindsorContainer _container;

    [ClassInitialize()]
    public static void InstallWindsor()
    {
        _container = new WindsorContainer();
        // configure other classes
    }

    [ClassCleanup()]
    public static void DisposeContainer()
    {
        _container.Dispose();
    }

    [TestMethod]
    public void Index()
    {
        // Disponer
        var mock = new Mock<IRowan>();

        mock.Setup(m => m.DoSomething()).Returns("Home Page");

        HomeController controller = new HomeController(mock.Object);

        // Actuar
        ViewResult result = controller.Index() as ViewResult;

        // Declarar
        Assert.IsNotNull(result);
        Assert.AreEqual("Home Page", result.ViewBag.Title);
    }
}

If the answer is no, which is the best way to implement log4net for testing? 如果答案是否定的,那么哪种是实施log4net进行测试的最佳方法?

PD: When I run the code, It works fine. PD:运行代码时,它可以正常工作。 When I run the test, Logger throws an exception because is null. 运行测试时,Logger会引发异常,因为它为null。

PD: I'm starting using the Microsoft Technology. PD:我开始使用Microsoft技术。

You could mock your Logger and set it on your controller like so: 您可以模拟Logger并在控制器上进行设置,如下所示:

Mock<ILogger> mockLogger = new Mock<ILogger>();
controller.Logger = mockLogger.Object;

Note that you shouldn't really need to use CastleWindsor in your tests. 请注意,您实际上不需要在测试中使用CastleWindsor。 That's useful for controlling the lifecycle of objects in your application but in your tests you should be injecting things by hand. 这对于控制应用程序中对象的生命周期很有用,但是在测试中,您应该手动注入东西。 You are actually doing this in the example above. 您实际上是在上面的示例中执行此操作。 From the code you've posted you can remove the ClassInitialize and ClassCleanup functions and it will run the same. 从发布的代码中,您可以删除ClassInitialize和ClassCleanup函数,它们将运行相同的函数。

You can use an other config for your test project. 您可以为测试项目使用其他配置。 Read more: https://logging.apache.org/log4net/release/manual/configuration.html 了解更多: https : //logging.apache.org/log4net/release/manual/configuration.html

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

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