如何在autofac中设置log4net的日志文件位置?

时间:2014-03-25 12:31:55

标签: log4net

我现在通过以下帖子添加控制器日志:Using Autofac to inject log4net into controller

完成后,我可以正确运行我的应用程序。以下是详细信息:

LogInjectionModule:

public class LogInjectionModule:Module
{
    protected override void AttachToComponentRegistration(Autofac.Core.IComponentRegistry componentRegistry, Autofac.Core.IComponentRegistration registration)
    {
        registration.Preparing += OnComponentPreparing;
    }

    static void OnComponentPreparing(object sender, PreparingEventArgs e)
    {
        var t = e.Component.Activator.LimitType;
        e.Parameters = e.Parameters.Union(new[] 
        { 
            new ResolvedParameter((p, i) => p.ParameterType == typeof(ILog), (p, i) => LogManager.GetLogger(t)) 
        });
    }
}

DependencyRegister here:

private void RegisterDependency()
    {
        var builder = new ContainerBuilder();

        builder.RegisterControllers(Assembly.GetExecutingAssembly());

        builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();

        builder.RegisterType<BookContext>().As<IDbContext>().SingleInstance().PreserveExistingDefaults();
        builder.RegisterType<ManagerRepository>().As<IManager>().InstancePerHttpRequest();
        builder.RegisterType<BookLendRepository>().As<IBookLend>().InstancePerHttpRequest();
        builder.RegisterType<BookPlaceRepository>().As<IBookPlace>().InstancePerHttpRequest();
        builder.RegisterType<BookRepository>().As<IBook>().InstancePerHttpRequest();
        builder.RegisterType<BookTypeRepository>().As<IBookType>().InstancePerHttpRequest();
        builder.RegisterType<StudentRepository>().As<IStudent>().InstancePerHttpRequest();

        builder.RegisterType<ManagerService>().As<IManagerService>().InstancePerHttpRequest();

        builder.RegisterModule(new LogInjectionModule());

        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

    }

MyController在这里:

public HomeController(
         IManagerService managerService
        ,ILog logger
        )
    {
        this.managerService = managerService;
        this.logger = logger;
    }

    private readonly IManagerService managerService;
    private readonly ILog logger;

    public ActionResult Index(Manager manager)
    {
        logger.Info("test");
        return View();
    }

当我调试logger.Info(“test”)时,我可以获取日志实例。但问题是,日志文件的位置在哪里?是否有集成log4net的配置,我可以决定在哪里放置日志文件?

1 个答案:

答案 0 :(得分:0)

这个问题与Autofac无关。 Log4net是一个独立的库。您可以在homepage上了解有关log4net的更多信息,以及如何对其进行配置here