1

知道如何做标题所说的吗?我发现的唯一东西是在原始的 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 的文档真的很乱。

4

2 回答 2

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);
于 2011-09-02T11:32:09.203 回答
1

Implement NVelocity.Runtime.Log.ILogSystem (you could write a simple implementation that bridges to log4net) and set this impl type in the property RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS

How I got this information:

  1. Get the code.
  2. Search for "log" in the codebase
  3. Discover the classes in NVelocity.Runtime.Log.
  4. Read those classes' source, they're very simple and thoroughly documented.
于 2011-09-01T13:20:54.600 回答