【问题标题】:iText7 - How to set up LoggerFactory?iText7 - 如何设置 LoggerFactory?
【发布时间】:2017-08-26 02:59:14
【问题描述】:

我今天刚开始使用 iText,所以我首先运行教程 #1 中的示例。不幸的是,我收到了这些错误消息:

Exception thrown: 'System.NullReferenceException' in itext.io.dll
iText.IO.Log.LoggerFactory: No logger factory was bound. Defaulting to no-operation logger implementation.
iText.IO.Log.LoggerFactory: In order to bind a logger factory use     iText.IO.Log.LoggerFactory.BindFactory().

显然我还没有设置一个叫做 LoggerFactory 的东西。我该怎么做?

【问题讨论】:

  • 在您提到您发现文档不连贯的答案下方的评论中。我向您询问了具体的反馈,但很遗憾我还没有收到您的回复。

标签: .net vb.net itext


【解决方案1】:

我们在一个 C# iText7 项目中遇到了类似的情况,其中

iText.IO.Log.LoggerFactory: No logger factory was bound. Defaulting to no-operation logger implementation.
iText.IO.Log.LoggerFactory: In order to bind a logger factory use     iText.IO.Log.LoggerFactory.BindFactory().

...我们每次尝试运行该进程时都会记录下来,只要我们尝试访问任何 iText 类。

我们通过在程序开头添加以下代码来解决这个问题:

iText.IO.Log.NoOpLoggerFactory iTextLoggerFactory = new iText.IO.Log.NoOpLoggerFactory();
iText.IO.Log.LoggerFactory.BindFactory(iTextLoggerFactory);

希望这会有所帮助!

【讨论】:

  • 我终于回到了我项目的这一部分。是的,这解决了问题,同时注意到必须导入 iText.IO.Log 以允许该代码工作。
  • 感谢您指出这一点!更新为包含 iText.IO.Log....,因此不需要导入,尽管导入肯定会起作用。
【解决方案2】:

在 iText 的 java 版本中,log4j 用于处理错误消息记录。
在 .net 版本中使用 log4net。
通常,如果您使用 NuGet 安装 iText,则会自动处理依赖关系解析。

【讨论】:

  • 我确实使用 NuGet 进行了安装,但显然没有完全安装。老实说,在使用它几天后,我已经放弃了。文档不连贯。
  • @SezMe 您能否就文档提供一些更具体的反馈,以便改进?你到底错过了什么?
  • 1. C# 中的示例非常缺乏。 2.根本没有明显的组织。 3. 根本找不到初学者教程。 4. 有很多 sn-ps(这很好),但很少有完整的示例。
  • 1. iText 代码库从 Java 移植到 .Net。努力确保所有类具有相同的名称、相同的方法和相同的签名。您应该能够获取任何 Java 示例并将它们作为 .net 运行(调整方法名称的大写/小写,不包括与系统相关的 IO)。 2. iText 几乎涵盖了整个 pdf 规范。很难将所有内容整齐地组织成不相干的类别。话虽如此,我们网站上的搜索功能还可以改进。
  • 3. iText7 有一个 Jumpstart 教程。应该可以完美运行。 4. 大多数(如果不是全部)代码示例在 Jenkins 上作为测试运行(这就是随附文件有时以“cmp_”为前缀的原因)。这些示例应该是独立的。
【解决方案3】:

对于那些正在寻找示例实现的人,这是我们为控制台日志记录所做的:

public class ConsoleLoggerFactory : iText.IO.Log.ILoggerFactory
{
    private readonly iText.IO.Log.ILogger _logger = new ConsoleLogger();

    public iText.IO.Log.ILogger GetLogger( Type type )
    {
        return _logger;
    }

    public iText.IO.Log.ILogger GetLogger( string name )
    {
        return _logger;
    }
}

这是上面工厂使用的记录器:

public sealed class ConsoleLogger : iText.IO.Log.ILogger
{
    public void Warn( string message )
    {
        Console.WriteLine( message );
    }

    public bool IsWarnEnabled()
    {
        return true;
    }

    public void Trace( string message )
    {
        Console.WriteLine( message );
    }

    public bool IsTraceEnabled()
    {
        return true;
    }

    public void Debug( string message )
    {
        Console.WriteLine( message );
    }

    public bool IsDebugEnabled()
    {
        return true;
    }

    public void Info( string message )
    {
        Console.WriteLine( message );
    }

    public bool IsInfoEnabled()
    {
        return true;
    }

    public void Error( string message, Exception exception )
    {
        Console.WriteLine( message );
        Console.WriteLine( exception );
    }

    public bool IsErrorEnabled()
    {
        return true;
    }

    public void Error( string message )
    {
        Console.WriteLine( message );
    }
}

然后您可以在启动时将其与以下代码绑定:

var iTextLoggerFactory = new ConsoleLoggerFactory();
iText.IO.Log.LoggerFactory.BindFactory(iTextLoggerFactory);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 2020-10-11
    • 2021-09-13
    • 2020-10-20
    相关资源
    最近更新 更多