如何在NVelocity中启用日志记录?

时间:2011-09-01 12:46:23

标签: nvelocity

知道如何做标题所说的内容吗?我发现的只有原始的Velocity网站,我不认为

ve.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
    "org.apache.velocity.runtime.log.Log4JLogChute" );

ve.setProperty("runtime.log.logsystem.log4j.logger",
   LOGGER_NAME);

将在.NET上运行得非常好。我正在使用log4net,它应该很容易,但是关于NVelocity的文档实际上是一团糟。

2 个答案:

答案 0 :(得分:1)

实施NVelocity.Runtime.Log.ILogSystem(您可以编写一个桥接到log4net的简单实现)并在属性RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS中设置此impl类型

我如何获得这些信息:

  1. 获取code
  2. 在代码库中搜索“log”
  3. 发现classes in NVelocity.Runtime.Log
  4. 阅读这些课程的来源,它们非常简单且完整记录。

答案 1 :(得分:1)

更新

目前,NVelocity不支持日志记录。注释掉RuntimeInstance类中的initializeLogger()和Log()方法。

如果您需要记录,取消注释这两种方法,请添加private ILogSystem logSystem;属性

这是我们的即时实施:

public class RuntimeInstance : IRuntimeServices
{
    private ILogSystem logSystem;
    ...
    ...
    private void initializeLogger()
    {
    logSystem = LogManager.CreateLogSystem(this);

    }
    ...
    ...
    private void Log(LogLevel level, Object message)
    {
    String output = message.ToString();

    logSystem.LogVelocityMessage(level, output);
    }
    ...
}

然后,我们为log4net实现了ILogSystem

using log4net;
using NVelocity.Runtime;
using NVelocity.Runtime.Log;

namespace Services.Templates
{
    public class Log4NetILogSystem : ILogSystem
    {
        private readonly ILog _log;

        public Log4NetILogSystem(ILog log )
        {
            _log = log;
        }

        public void Init(IRuntimeServices rs)
        {

        }

        public void LogVelocityMessage(LogLevel level, string message)
        {
            switch (level)
            {
                case LogLevel.Debug:
                    _log.Debug(message);
                    break;
                case LogLevel.Info:
                    _log.Info(message);
                    break;
                case LogLevel.Warn:
                    _log.Warn(message);
                    break;
                case LogLevel.Error:
                    _log.Error(message);
                    break;
            }
        }
    }
}

然后,在创建引擎时:

var engine = new VelocityEngine();
var props = new ExtendedProperties();    
props.SetProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM,
new Log4NetILogSystem(LogManager.GetLogger(typeof(NVelocityEngine))));
engine.Init(props);
相关问题