Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# iText7无法设置日志记录_C#_.net_Itext_Itext7_Common.logging - Fatal编程技术网

C# iText7无法设置日志记录

C# iText7无法设置日志记录,c#,.net,itext,itext7,common.logging,C#,.net,Itext,Itext7,Common.logging,我正在尝试将PDFCleanuptol与iText7一起使用。 然而,我的最终PDF已损坏,它的大小只有15B 当我从VS启动我的控制台应用程序时,我在输出中得到以下信息: 未找到配置节-正在抑制日志记录输出 我正在尝试设置日志以获取错误消息,但运气不好 我已安装以下软件包: <?xml version="1.0" encoding="utf-8"?> <packages> <package id="Common.Logging" version="3.4.1"

我正在尝试将PDFCleanuptol与iText7一起使用。 然而,我的最终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>
可能我需要设置一个许可证密钥,但我希望得到一条错误消息,说明我必须这样做

我的问题是:如何使用Common.Logging正确设置NLog以从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[错误]iText.Kernel.Pdf.PdfReader-测试错误日志输出 ====================================================================================最内部的异常=== 1.系统例外 ================================================================================ 方法: 类型: 大会: 程序集路径: 资料来源: 螺纹:15“非平行加工” 帮助链接: 信息: 异常消息 堆栈跟踪: ================================================================================ 在实际问题上 在您将代码添加到问题后,问题变得很清楚:您忘记关闭PdfDocument PdfDocument。因此,一切正常,没有任何记录,只是更改没有写入文件,因为PDF文档对象没有关闭。只需在cleaner.CleanUp调用后将其关闭:

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


您是否尝试将信息记录到文件而不是控制台?@LenglBoy是的,没有luckAnd,您的配置是完整的,就像@LenglBoy NLog不是问题一样,我以前使用过很多次。我无法将Common.Logging和NLog配置为一起工作。我需要这个来从iText 7获取消息。你能分享有问题的pdf吗?我已经尝试过你的解决方案,但什么也没发生。我添加了完整的示例代码。同样的代码在iTextSharp 5.5.13中编写时运行良好,在重写到iText 7.1.2后,我无法将1KB PDF作为输出。据我所知,pdfSweep是一个需要许可证的插件,当未设置许可证时,它会抛出错误,但不会发生任何事情。如果您运行上面的测试CreateLogOutput,您会得到我得到的输出吗?是的,我在控制台中看到输出,与我的问题中的nlog配置相同,但不知何故,我没有看到任何错误iText7@Misiu对我在控制台中看到了输出,与我的问题中的nlog config相同,但不知何故,我没有看到iText7中的任何错误-这意味着日志记录配置是正确的,但iText7在运行代码时根本不记录任何内容。
[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"));
}
PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDocument, cleanUpLocations);
cleaner.CleanUp();
pdfDocument.Close();