用于log4net的Castle日志记录设施,具有流畅的log4net配置

本文关键字:log4net 配置 设施 Castle 日志 记录 用于 | 更新日期: 2023-09-27 18:05:50

我有一个流畅的log4net配置。它属于log4netConfigSetup.cs类。在我的Castle安装程序类中,当您添加如下示例所示的日志记录工具时,它会搜索app.config中的配置:

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.AddFacility<LoggingFacility>(f => f.UseLog4Net());
} 

我怎样才能使它寻找流畅的配置类,而不是配置文件或xml文件的log4net配置?

用于log4net的Castle日志记录设施,具有流畅的log4net配置

您可以通过实现ILoggerFactory来实现这一点,就像这个部分示例一样:您需要实现您想要使用的ILogger接口的所有部分。

using System;
using Castle.Core.Logging;
using Castle.Facilities.Logging;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using log4net;
namespace Castle_Log4Net
{
    public class Installer : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, 
                            IConfigurationStore store)
        {
            container.AddFacility<LoggingFacility>
                    (f => f.LogUsing<Log4NetLoggingFactory>());
        }
    }
    public class Log4NetLoggingFactory : Castle.Core.Logging.ILoggerFactory
    {
        static Log4NetLoggingFactory()
        {
            // here you configure log4net from log4netConfigSetup.cs
        }
        public ILogger Create(Type type)
        {
            return new Log4NetLogger(type);
        }
        public ILogger Create(string name)
        {
            return new Log4NetLogger(name);
        }
        public ILogger Create(Type type, LoggerLevel level)
        {
            throw new NotImplementedException();
        }
        public ILogger Create(string name, LoggerLevel level)
        {
            throw new NotImplementedException();
        }
    }
    public class Log4NetLogger : Castle.Core.Logging.ILogger
    {
        private readonly ILog logger;
        public Log4NetLogger(Type type)
        {
            this.logger = LogManager.GetLogger(type);
        }
        public Log4NetLogger(string name)
        {
            this.logger = LogManager.GetLogger(name);
        }
        public ILogger CreateChildLogger(string loggerName)
        {
            throw new NotImplementedException();
        }
        public void Debug(string message)
        {
            logger.Debug(message);
        }
        public void Debug(Func<string> messageFactory)
        {
            logger.Debug(messageFactory.Invoke());
        }
        public void Debug(string message, Exception exception)
        {
            logger.Debug(message, exception);
        }
        public void DebugFormat(string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void DebugFormat(Exception exception, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void DebugFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void Error(string message)
        {
            throw new NotImplementedException();
        }
        public void Error(Func<string> messageFactory)
        {
            throw new NotImplementedException();
        }
        public void Error(string message, Exception exception)
        {
            throw new NotImplementedException();
        }
        public void ErrorFormat(string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void ErrorFormat(Exception exception, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void ErrorFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void Fatal(string message)
        {
            throw new NotImplementedException();
        }
        public void Fatal(Func<string> messageFactory)
        {
            throw new NotImplementedException();
        }
        public void Fatal(string message, Exception exception)
        {
            throw new NotImplementedException();
        }
        public void FatalFormat(string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void FatalFormat(Exception exception, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void FatalFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void Info(string message)
        {
            throw new NotImplementedException();
        }
        public void Info(Func<string> messageFactory)
        {
            throw new NotImplementedException();
        }
        public void Info(string message, Exception exception)
        {
            throw new NotImplementedException();
        }
        public void InfoFormat(string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void InfoFormat(Exception exception, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void InfoFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void Warn(string message)
        {
            throw new NotImplementedException();
        }
        public void Warn(Func<string> messageFactory)
        {
            throw new NotImplementedException();
        }
        public void Warn(string message, Exception exception)
        {
            throw new NotImplementedException();
        }
        public void WarnFormat(string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void WarnFormat(Exception exception, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public void WarnFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
        {
            throw new NotImplementedException();
        }
        public bool IsDebugEnabled { get; private set; }
        public bool IsErrorEnabled { get; private set; }
        public bool IsFatalEnabled { get; private set; }
        public bool IsInfoEnabled { get; private set; }
        public bool IsWarnEnabled { get; private set; }
    }
}