【问题标题】:iText7 unable to setup loggingiText7 无法设置日志记录
【发布时间】:2018-06-22 11:43:29
【问题描述】:

我正在尝试将PdfCleanUpTool 与 iText7 一起使用。
但是我的最终 PDF 已损坏(它只有 15B 大小)。

当我从 VS 启动我的 console 应用程序时,我在输出中得到这个:

找不到配置部分 - 禁止记录输出

我正在尝试设置日志记录以获取错误消息,但没有运气。

我已经安装了这个包:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Common.Logging" version="3.4.1" targetFramework="net47" />
  <package id="Common.Logging.Core" version="3.4.1" targetFramework="net47" />
  <package id="Common.Logging.NLog4412" version="3.4.1" targetFramework="net47" />
  <package id="itext7" version="7.1.2" targetFramework="net47" />
  <package id="itext7.pdfsweep" version="2.0.1" targetFramework="net47" />
  <package id="Microsoft.CSharp" version="4.0.1" targetFramework="net47" />
  <package id="NLog" version="4.4.12" targetFramework="net47" />
  <package id="Portable.BouncyCastle" version="1.8.1.3" targetFramework="net47" />
</packages>

这是我的app.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>

    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>

  <common>
    <logging>
      <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog4412">
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>

  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="console" xsi:type="Console" layout="${date:format=HH\:MM\:ss} ${logger} ${message}" />
    </targets>
    <rules>
      <logger name="*" minlevel="Info" writeTo="console" />
    </rules>
  </nlog>

    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
    </startup>
</configuration>

可能我需要设置许可证密钥,但我想收到错误消息说我必须这样做。

我的问题是:
我应该如何正确设置 NLog 与 Common.Logging 以从 iText7 获取错误。

这是可用于验证当前行为的完整示例:

using Common.Logging;
using Common.Logging.Configuration;
using Common.Logging.Simple;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.PdfCleanup;
using System;
using System.Collections.Generic;
using System.IO;

namespace RedactTest
{
    class Program
    {
        static void Main(string[] args)
        {
            NameValueCollection properties = new NameValueCollection
            {
                ["showDateTime"] = "true",
                ["level"] = "All"
            };

            LogManager.Adapter = new ConsoleOutLoggerFactoryAdapter(properties);

            using (Stream inputStream = new FileStream("D:\\test.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                PdfReader reader = new PdfReader(inputStream);
                using (Stream outputStream = new FileStream("D:\\test_redact.pdf", FileMode.Create))
                {
                    PdfWriter writer = new PdfWriter(outputStream);
                    PdfDocument pdfDocument = new PdfDocument(reader, writer);

                    List<PdfCleanUpLocation> cleanUpLocations = new List<PdfCleanUpLocation>
                    {
                        new PdfCleanUpLocation(1, new Rectangle(40f, 650f, 200f, 700f),ColorConstants.GRAY),
                        new PdfCleanUpLocation(1, new Rectangle(40f, 550f, 200f, 590f),ColorConstants.GRAY),
                        new PdfCleanUpLocation(1, new Rectangle(344f, 650f, 550f, 724f),ColorConstants.GRAY)
                    };

                    PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDocument, cleanUpLocations);
                    cleaner.CleanUp();
                }
            }
            Console.Write("OK");
            Console.ReadLine();
        }
    }
}

【问题讨论】:

  • 您是否尝试将信息记录到文件而不是控制台?
  • @LenglBoy 是的,没有运气
  • 你的配置已经完成,就像here in the tutorial
  • @LenglBoy NLog 没问题,我以前用过很多次。我无法配置 Common.Logging 和 NLog 一起工作。我需要这个来接收来自iText 7 的消息
  • 你能分享有问题的pdf吗?

标签: c# .net itext itext7 common.logging


【解决方案1】:

关于原来的日志记录相关问题

一种选择是在程序开始时从代码中激活控制台记录器:

// create properties
NameValueCollection properties = new NameValueCollection();
properties["showDateTime"] = "true";
properties["level"] = "All";

// set Adapter
Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter(properties);

例如用于以下测试

[Test]
public void CreateLogOutput()
{
    // create properties
    NameValueCollection properties = new NameValueCollection();
    properties["showDateTime"] = "true";
    properties["level"] = "All";

    // set Adapter
    Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter(properties);

    ILog logger = LogManager.GetLogger(typeof(iText.Kernel.Pdf.PdfReader));
    logger.Error("Testing an error log output", new Exception("The exception message"));
}

一个得到这样的输出:

27.06.2018 17:07:37 [ERROR] iText.Kernel.Pdf.PdfReader - Testing an error log output
=======================================================(inner most exception)===
 (1) System.Exception
================================================================================
Method        :  <unavailable>
Type          :  <unavailable>
Assembly      :  <unavailable>
Assembly Path :  <unavailable>
Source        :  
Thread        :  15 'NonParallelWorker'
Helplink      :  

Message:
"The exception message"

Stack Trace:

================================================================================

关于实际问题

将代码添加到问题后,问题变得清晰:您忘记关闭PdfDocument pdfDocument。因此,一切正常,没有记录任何内容,只是更改不会写入文件,因为 PDF 文档对象没有关闭。只需在cleaner.CleanUp() 调用后将其关闭:

PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDocument, cleanUpLocations);
cleaner.CleanUp();
pdfDocument.Close();

现在的结果是 34KB 大小,显示如下:

【讨论】:

  • 我已经尝试过您的解决方案,但没有任何反应。我添加了完整的示例代码。在 iTextSharp 5.5.13 中编写时,相同的代码运行良好,在重写为 iText 7.1.2 后,我无法使用 1 KB PDF 作为输出。 pdfSweep 是(据我所知)需要许可证的插件,如果未设置许可证,它应该会抛出错误,但没有任何反应。
  • 如果你在上面运行我的测试CreateLogOutput,你会得到我得到的输出吗?
  • 是的,我在控制台中看到输出,与我的问题中的 nlog 配置相同,但不知何故我没有看到 iText7 的任何错误
  • @Misiu “是的,我在控制台中看到了输出,与我的问题中的 nlog 配置相同,但不知何故我没有看到 iText7 的任何错误” - 这意味着日志记录配置是正确的,但 iText7 在运行代码时根本不会记录任何内容。
猜你喜欢
  • 2018-11-13
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
  • 2023-03-23
  • 1970-01-01
  • 2020-11-13
相关资源
最近更新 更多