代码之家  ›  专栏  ›  技术社区  ›  Misiu

IText7无法设置日志记录

  •  0
  • Misiu  · 技术社区  · 6 年前

    我想用 PdfCleanUpTool 用ITXEX7。
    然而,我的最终pdf文件已经损坏(它的大小只有15b)。

    当我开始我的 慰问 VS的应用程序我在输出中得到这个:

    未找到配置节-正在抑制日志记录输出

    我试图设置日志以获取错误消息,但没有成功。

    我已安装此软件包:

    <?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 具有 普通日志记录 从中获取错误 ITXEX7 .

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

    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();
            }
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   mkl    6 年前

    论原始测井相关问题

    一种选择是从代码启动控制台记录器,在程序开始时:

    // 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,显示如下:

    screen shot