2014-03-25 100 views
0

我現在通過以下帖子添加控制器日誌:Using Autofac to inject log4net into controller如何在autofac中設置log4net的日誌文件位置?

這樣做後,我可以讓我的應用程序正常運行。以下是詳細內容:

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這裏:

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(「測試「),我可以得到日誌實例。但問題是,日誌文件的位置在哪裏?是否有集成的log4net的配置,我可以決定在哪裏放置日誌文件?

回答

0

這個問題與Autofac無關。 Log4net是一個獨立的庫。您可以在其homepage上閱讀有關log4net的更多信息,以及如何配置它here

相關問題