Logging with Castle.Facilities.Logging and log4net

前端 未结 6 1665
轻奢々
轻奢々 2020-12-06 11:49

I\'m trying to get log4net integration for Castle Windsor working. I wrote my class with an public property of type ILogger and took the configuration in my app

6条回答
  •  春和景丽
    2020-12-06 12:37

    You can use App.config for your log4net configuration without creating a custom logging factory. Simply provide the App.config file as an argument to the LoggingFacility constructor:

    container
        .AddFacility("logging",
            new LoggingFacility(LoggerImplementation.Log4net,
                System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
            )
    

    Unlike Mauricio Scheffer's answer, the default factory does ConfigureAndWatch. This works just fine for me with the App.config file, though I'm not running on IIS or anything else that restricts permissions.

    I'm doing this in code because you can't reliably use the Windsor Xml configuration to load log4net configuration from App.config. This is because the location of App.config can be modified when creating a new AppDomain.

    Using my solution means the log file configuration will be compiled into your code. But you can mitigate this by using a Windsor Installer to configure logging, and specify the installer (or installer assembly) from the App.config file:

    public class LoggingInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container
                .AddFacility("logging",
                    new LoggingFacility(LoggerImplementation.Log4net,
                        System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
                    );
        }
    }
    

    ...

    
        
            
        
    
    

    If in the future (maybe in your test cases) you have to load the logging configuration from a different file, and can't or don't want to recompile, simply change the Xml to point to Windsor Installers in a different assembly:

    
        
            
        
    
    

提交回复
热议问题