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

发布于 2024-12-02 05:58:41 字数 329 浏览 1 评论 0原文

知道如何按照标题所说的去做吗?我唯一发现的是在原始 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 的文档确实很混乱。

Any idea how to do what the title says? Only thing I found was on the original Velocity site, and I don't think

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

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

will work wonderfully well on .NET. I am using log4net, which should make it quite easy, but the documentation on NVelocity is really a mess.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡紫姑娘! 2024-12-09 05:58:41

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

我如何获取此信息:

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

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.
不美如何 2024-12-09 05:58:41

更新:

目前,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);

Update:

Currently, NVelocity does not support logging. The initializeLogger() and Log() methods in RuntimeInstance Class are commented out.

If you need to log, uncomment the two methods, add a private ILogSystem logSystem; property

Here's our on-the-fly implementation:

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);
    }
    ...
}

Then, we implemented ILogSystem for log4net

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;
            }
        }
    }
}

Then, when creating the engine:

var engine = new VelocityEngine();
var props = new ExtendedProperties();    
props.SetProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM,
new Log4NetILogSystem(LogManager.GetLogger(typeof(NVelocityEngine))));
engine.Init(props);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文