iText7 无法设置日志记录
iText7 unable to setup logging
我正在尝试将 PdfCleanUpTool
与 iText7 一起使用。
但是我的最终 PDF 已损坏(大小只有 15B)。
当我从 VS 启动我的 console 应用程序时,我在输出中得到了这个:
no configuration section found - suppressing logging output
我正在尝试设置日志记录以获取错误消息,但没有成功。
我已经安装了这个包:
<?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();
}
}
}
关于原始日志相关问题
一个选项是从代码激活控制台记录器,在程序开始时输入:
// 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,显示如下:
我正在尝试将 PdfCleanUpTool
与 iText7 一起使用。
但是我的最终 PDF 已损坏(大小只有 15B)。
当我从 VS 启动我的 console 应用程序时,我在输出中得到了这个:
no configuration section found - suppressing logging output
我正在尝试设置日志记录以获取错误消息,但没有成功。
我已经安装了这个包:
<?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();
}
}
}
关于原始日志相关问题
一个选项是从代码激活控制台记录器,在程序开始时输入:
// 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,显示如下: