如何在C#中处理错误和调试条码操作
条形码处理流程可能会悄无声息地失败,结果为零通常会被误认为是"未检测到条形码"。然而,诸如文件损坏、受密码保护的 PDF 文件或格式不匹配等问题都可能是造成这种情况的原因。 实施适当的日志记录和结构化的错误处理可以发现这些故障并提供可操作的诊断信息。
IronBarcode在BarcodeResult属性。 这些属性包括检测到的格式、解码值、页码以及每次成功解码的坐标。
这篇指南解释了如何捕获和解释类型化异常、从失败读取中提取诊断上下文、启用结构化日志记录以及在批量操作期间隔离故障。
快速入门:处理条码错误并启用诊断将读/写调用包装在针对IronBarcode类型异常的try-catch块中,以显现可操作的错误信息,而不是静默失败。
-
1Install IronBarcode with NuGet Package Manager
-
2复制并运行这段代码。
using IronBarCode; using IronBarCode.Exceptions; try { BarcodeResults results = BarcodeReader.Read("label.pdf"); Console.WriteLine($"Found {results.Count} barcode(s)"); } catch (IronBarCodeFileException ex) { Console.Error.WriteLine($"File error: {ex.Message}"); }C# -
3部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronBarcode
如何使用IronBarcode处理条形码错误并启用诊断功能
- 从NuGet下载IronBarcode库
- 将读/写调用包装在针对特定异常类型的try-catch块中
- 在成功读取后检查
BarcodeResults中的空条目或低置信度条目 - 启用
IronSoftware.Logger以捕获内部诊断输出 - 在批量操作中使用继续出错逻辑将错误隔离到每个文件
如何捕获和解析IronBarcode异常?
从最具体到最一般,捕获IronBarcode异常。 先处理可操作的异常,例如文件错误、PDF 密码错误和编码错误,然后再处理基本类型异常。 IronBarCode.Exceptions命名空间定义了11种异常类型,每种类型对应特定的故障模式:
| 异常类型 | 触发 | 推荐修复 |
|---|---|---|
IronBarCodeFileException | 文件已损坏、被锁定或为不支持的图像格式。 | 验证文件是否为受支持的图像格式且未被锁定;同时,对于缺失的文件,单独捕获FileNotFoundException 。 |
IronBarCodePdfPasswordException | PDF受密码保护或加密 | 通过PdfBarcodeReaderOptions提供密码,或跳过文件并记录 |
IronBarCodeEncodingException | 在生成条码时发生的通用编码故障 | 验证输入数据符合目标BarcodeWriterEncoding约束 |
IronBarCodeContentTooLongEncodingException | 值超过所选符号的字符限制 | 截断数据或切换到更高容量格式(QR, DataMatrix) |
IronBarCodeFormatOnlyAcceptsNumericValuesEncodingException | 为仅数字格式(EAN, UPC)传递了非数字字符 | 清理输入或转换为字母数字格式(Code128, Code39) |
IronBarCodeUnsupportedRendererEncodingException | IronBarcode无法写入选定的BarcodeEncoding | 使用BarcodeWriterEncoding枚举而不是BarcodeEncoding |
IronBarCodeParsingException | 解析过程中结构化数据(GS1-128)验证失败 | 在解析前用Code128GS1Parser.IsValid()验证GS1结构 |
IronBarCodeNativeException | 本地互操作层错误(缺少DLL,平台不兼容) | 验证是否安装了平台特定的NuGet包(BarCode.Linux, BarCode.macOS) |
IronBarCodeConfidenceThresholdException | 无效的置信度阈值参数传递给读取器选项 | 确保ConfidenceThreshold介于0.0与1.0之间 |
IronBarCodeUnsupportedException | 当前上下文中不支持的操作 | 检查变更日志以获取您版本中功能的可用性 |
IronBarCodeException | 基础类型——捕获上述未匹配到的任何IronBarcode特定错误 | 记录完整的异常详情并加以调查 |
使用带有when的异常过滤器来路由重叠的异常类型,而无需深度嵌套。 缺失文件抛出标准IronBarCodeFileException,因此请为这种情况添加一个单独的catch块:
输入
Code128 条形码编码发票号码(成功路径)和仓库标签条形码表示缺失 PDF 的内容(失败路径)。

scanned-invoice.png(成功路径)

warehouse-labels.pdf(失败路径——磁盘上不存在该文件)
using IronBarCode;
using IronBarCode.Exceptions;
// Success path: valid file present on disk
string filePath = "scanned-invoice.png";
// Failure path: file does not exist → caught by FileNotFoundException below
// string filePath = "warehouse-labels.pdf";
try
{
BarcodeResults results = BarcodeReader.Read(filePath);
foreach (BarcodeResult result in results)
{
// Print the detected symbology and decoded value for each barcode found
Console.WriteLine($"[{result.BarcodeType}] {result.Value}");
}
}
catch (IronBarCodePdfPasswordException ex)
{
// PDF is encrypted — supply the password via PdfBarcodeReaderOptions before retrying
Console.Error.WriteLine($"PDF requires password: {filePath} — {ex.Message}");
}
catch (IronBarCodeFileException ex)
{
// File is present but corrupted, locked, or in an unsupported format
Console.Error.WriteLine($"Cannot read file: {filePath} — {ex.Message}");
}
catch (FileNotFoundException ex)
{
// Missing files throw FileNotFoundException, not IronBarCodeFileException
Console.Error.WriteLine($"File not found: {filePath} — {ex.Message}");
}
catch (IronBarCodeNativeException ex) when (ex.Message.Contains("DLL"))
{
// The when filter routes only missing-DLL errors here; other native exceptions
// fall through to the IronBarCodeException block below
Console.Error.WriteLine($"Missing native dependency: {ex.Message}");
}
catch (IronBarCodeException ex)
{
// Base catch for any IronBarcode-specific error not matched by the blocks above
Console.Error.WriteLine($"IronBarcode error: {ex.GetType().Name} — {ex.Message}");
}Imports IronBarCode
Imports IronBarCode.Exceptions
' Success path: valid file present on disk
Dim filePath As String = "scanned-invoice.png"
' Failure path: file does not exist → caught by FileNotFoundException below
' Dim filePath As String = "warehouse-labels.pdf"
Try
Dim results As BarcodeResults = BarcodeReader.Read(filePath)
For Each result As BarcodeResult In results
' Print the detected symbology and decoded value for each barcode found
Console.WriteLine($"[{result.BarcodeType}] {result.Value}")
Next
Catch ex As IronBarCodePdfPasswordException
' PDF is encrypted — supply the password via PdfBarcodeReaderOptions before retrying
Console.Error.WriteLine($"PDF requires password: {filePath} — {ex.Message}")
Catch ex As IronBarCodeFileException
' File is present but corrupted, locked, or in an unsupported format
Console.Error.WriteLine($"Cannot read file: {filePath} — {ex.Message}")
Catch ex As FileNotFoundException
' Missing files throw FileNotFoundException, not IronBarCodeFileException
Console.Error.WriteLine($"File not found: {filePath} — {ex.Message}")
Catch ex As IronBarCodeNativeException When ex.Message.Contains("DLL")
' The when filter routes only missing-DLL errors here; other native exceptions
' fall through to the IronBarCodeException block below
Console.Error.WriteLine($"Missing native dependency: {ex.Message}")
Catch ex As IronBarCodeException
' Base catch for any IronBarcode-specific error not matched by the blocks above
Console.Error.WriteLine($"IronBarcode error: {ex.GetType().Name} — {ex.Message}")
End Try输出
![控制台输出显示 Code128 解码成功:[Code128] INV-2024-7829](/static-assets/barcode/how-to/detailed-error-messages/output-exception-hierarchy-success.webp)
缺失文件触发FileNotFoundException,通过专用的catch块路由。

IronBarCodeNativeException上,指示错过依赖的错误处理到特定的处理器,而不影响其他本机异常。 这种方法在 Docker 部署中尤其有用,因为 Docker 部署中可能缺少特定于平台的软件包。
当许可证密钥无效或丢失时,IronSoftware.Exceptions.LicensingException会被单独抛出。 在应用程序启动时捕获此异常,而不是在单个读取或写入调用周围捕获。
如何从读取失败的数据中提取诊断详情?
读取操作返回零结果也不属于例外情况; 它生成一个空的BarcodeResults集合。 通过检查输入参数、配置选项和返回的任何部分结果来获取诊断上下文。
Points(角坐标)。 如果结果存在但出乎意料,首先检查PageNumber。
输入
一个编码发票号的Code128条码,通过ReadingSpeed.Detailed来进行彻底扫描。

using IronBarCode;
string filePath = "scanned-invoice.png";
// Configure the reader to narrow the search to specific symbologies and use
// a thorough scan pass — narrows false positives and improves decode accuracy
var options = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeEncoding.Code128 | BarcodeEncoding.QRCode, // limit scan to known formats
Speed = ReadingSpeed.Detailed, // slower but more thorough — use ExtremeDetail for damaged images
ExpectMultipleBarcodes = true // scan the full image rather than stopping at the first match
};
BarcodeResults results = BarcodeReader.Read(filePath, options);
// An empty result is not an exception — it means no barcode matched the configured options
if (results == null || results.Count == 0)
{
// Log the configured options alongside the warning so the cause is immediately actionable
Console.Error.WriteLine($"[WARN] No barcodes found in: {filePath}");
Console.Error.WriteLine($" ExpectedTypes: {options.ExpectBarcodeTypes}");
Console.Error.WriteLine($" Speed: {options.Speed}");
Console.Error.WriteLine($" Action: Retry with ReadingSpeed.ExtremeDetail or broaden ExpectBarcodeTypes");
}
else
{
foreach (BarcodeResult result in results)
{
// Points contains the four corner coordinates of the barcode in the image;
// use the first corner as a representative position indicator
string pos = result.Points.Length > 0 ? $"{result.Points[0].X:F0},{result.Points[0].Y:F0}" : "N/A";
Console.WriteLine($"[{result.BarcodeType}] {result.Value} "
+ $"(Page: {result.PageNumber}, Position: {pos})");
}
}Imports IronBarCode
Dim filePath As String = "scanned-invoice.png"
' Configure the reader to narrow the search to specific symbologies and use
' a thorough scan pass — narrows false positives and improves decode accuracy
Dim options As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.Code128 Or BarcodeEncoding.QRCode, ' limit scan to known formats
.Speed = ReadingSpeed.Detailed, ' slower but more thorough — use ExtremeDetail for damaged images
.ExpectMultipleBarcodes = True ' scan the full image rather than stopping at the first match
}
Dim results As BarcodeResults = BarcodeReader.Read(filePath, options)
' An empty result is not an exception — it means no barcode matched the configured options
If results Is Nothing OrElse results.Count = 0 Then
' Log the configured options alongside the warning so the cause is immediately actionable
Console.Error.WriteLine($"[WARN] No barcodes found in: {filePath}")
Console.Error.WriteLine($" ExpectedTypes: {options.ExpectBarcodeTypes}")
Console.Error.WriteLine($" Speed: {options.Speed}")
Console.Error.WriteLine($" Action: Retry with ReadingSpeed.ExtremeDetail or broaden ExpectBarcodeTypes")
Else
For Each result As BarcodeResult In results
' Points contains the four corner coordinates of the barcode in the image;
' use the first corner as a representative position indicator
Dim pos As String = If(result.Points.Length > 0, $"{result.Points(0).X:F0},{result.Points(0).Y:F0}", "N/A")
Console.WriteLine($"[{result.BarcodeType}] {result.Value} " &
$"(Page: {result.PageNumber}, Position: {pos})")
Next
End If输出
ExpectBarcodeTypes与图像中的条码匹配时,读取返回的是类型、值、页码和位置。

如果ExpectBarcodeTypes不包含实际的符号,那么读取返回一个空结果。 [WARN] 块记录配置的类型、读取速度以及建议的下一步操作。
![控制台输出显示 [警告] 未找到条形码,ExpectBarcodeTypes 设置为 Code39,但对应的是 Code128 图像](/static-assets/barcode/how-to/detailed-error-messages/output-diagnostic-logging-failure.webp)
诊断过程中会出现两种常见模式。 带有狭窄的ExpectBarcodeTypes设置的空结果通常意味着条码使用了不同的符号; 扩展到BarcodeEncoding.All可以确认这一点。 解码结果异常通常表明图像质量较差。
应用图像滤镜并以较慢的读取速度重试通常可以解决这些问题。 您还可以切换RemoveFalsePositive选项来消除来自嘈杂背景的幽灵读取。
如何启用条形码操作的详细日志记录?
IronBarcode通过IronSoftware.Logger暴露了一个内置的日志记录API。 在进行任何条形码操作之前,设置日志记录模式和文件路径,以捕获读写管道的内部诊断输出。
输入
启用详细日志记录时,使用 Code128 条形码 TIFF 图像作为读取目标。

using IronBarCode;
// Enable IronBarcode's built-in logging — set BEFORE any read/write calls
// LoggingModes.All writes both debug output and file-level diagnostics
IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.All;
IronSoftware.Logger.LogFilePath = "ironbarcode-debug.log"; // path is relative to the working directory
// All subsequent operations will write internal processing steps to the log file:
// image pre-processing stages, format detection attempts, and native interop calls
var options = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Detailed,
ExpectBarcodeTypes = BarcodeEncoding.All // scan for every supported symbology
};
BarcodeResults results = BarcodeReader.Read("problem-scan.tiff", options);
Console.WriteLine($"Read complete. Results: {results.Count}. See ironbarcode-debug.log for details.");Imports IronBarCode
' Enable IronBarcode's built-in logging — set BEFORE any read/write calls
' LoggingModes.All writes both debug output and file-level diagnostics
IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.All
IronSoftware.Logger.LogFilePath = "ironbarcode-debug.log" ' path is relative to the working directory
' All subsequent operations will write internal processing steps to the log file:
' image pre-processing stages, format detection attempts, and native interop calls
Dim options As New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Detailed,
.ExpectBarcodeTypes = BarcodeEncoding.All ' scan for every supported symbology
}
Dim results As BarcodeResults = BarcodeReader.Read("problem-scan.tiff", options)
Console.WriteLine($"Read complete. Results: {results.Count}. See ironbarcode-debug.log for details.")LoggingModes.All捕获调试输出和文件级别的日志记录。 日志文件记录内部处理步骤,例如图像预处理阶段、格式检测尝试和本地互操作调用,这些步骤无法通过公共 API 查看。
对于使用结构化日志框架(如Serilog、NLog、Microsoft.Extensions.Logging)的生产管道,将IronBarcode操作包装在中间件层中,可以在内置日志文件旁边添加结构化的JSON条目。内置的记录器写入对支持升级有用的普通文本诊断信息; 结构化包装器为可观测性堆栈提供可查询字段。
using IronBarCode;
using System.Diagnostics;
// Lightweight wrapper that adds structured JSON observability to every read call.
// Call this in place of BarcodeReader.Read wherever elapsed-time and status logging is needed.
BarcodeResults ReadWithDiagnostics(string filePath, BarcodeReaderOptions options)
{
var sw = Stopwatch.StartNew(); // start timing before the read so setup overhead is included
try
{
BarcodeResults results = BarcodeReader.Read(filePath, options);
sw.Stop();
// Emit a structured success entry to stdout — pipe to Fluentd, Datadog, or CloudWatch
Console.WriteLine($"{{\"file\":\"{filePath}\",\"status\":\"ok\","
+ $"\"count\":{results.Count},\"elapsed_ms\":{sw.ElapsedMilliseconds}}}");
return results;
}
catch (Exception ex)
{
sw.Stop();
// Emit a structured error entry to stderr with exception type, message, and elapsed time
Console.Error.WriteLine($"{{\"file\":\"{filePath}\",\"status\":\"error\","
+ $"\"exception\":\"{ex.GetType().Name}\",\"message\":\"{ex.Message}\","
+ $"\"elapsed_ms\":{sw.ElapsedMilliseconds}}}");
throw; // rethrow so the caller's catch blocks still handle the exception normally
}
}Imports IronBarCode
Imports System.Diagnostics
' Lightweight wrapper that adds structured JSON observability to every read call.
' Call this in place of BarcodeReader.Read wherever elapsed-time and status logging is needed.
Function ReadWithDiagnostics(filePath As String, options As BarcodeReaderOptions) As BarcodeResults
Dim sw As Stopwatch = Stopwatch.StartNew() ' start timing before the read so setup overhead is included
Try
Dim results As BarcodeResults = BarcodeReader.Read(filePath, options)
sw.Stop()
' Emit a structured success entry to stdout — pipe to Fluentd, Datadog, or CloudWatch
Console.WriteLine($"{{""file"":""{filePath}"",""status"":""ok"",""count"":{results.Count},""elapsed_ms"":{sw.ElapsedMilliseconds}}}")
Return results
Catch ex As Exception
sw.Stop()
' Emit a structured error entry to stderr with exception type, message, and elapsed time
Console.Error.WriteLine($"{{""file"":""{filePath}"",""status"":""error"",""exception"":""{ex.GetType().Name}"",""message"":""{ex.Message}"",""elapsed_ms"":{sw.ElapsedMilliseconds}}}")
Throw ' rethrow so the caller's catch blocks still handle the exception normally
End Try
End Function结构化输出可直接与日志聚合工具集成。 在容器化部署中,将stdout传递到Fluentd、Datadog或CloudWatch中。 经过时间字段可以突出显示性能下降情况,防止其演变为服务级别协议 (SLA) 违规。
输出

如何调试批量条形码处理?
通过将每个读取操作隔离到其自身的 try-catch 块中来处理多个文件,记录每个文件的结果,并生成汇总摘要。 即使出现故障,管道也会继续运行,而不是在遇到第一个错误时停止。
输入
来自scans/批处理目录的五张Code128条码图像中的四张。 第5个文件(scan-05-broken.png)包含无效字节以触发文件异常。

第一批 — 扫描 1

第一批 — 扫描 2

第一批 — 扫描 3

第一批 — 扫描 4
using IronBarCode;
using IronBarCode.Exceptions;
using System.Diagnostics;
// Enable built-in logging for the entire batch run so internal processing steps
// are captured in the log file alongside the per-file console output
IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.All;
IronSoftware.Logger.LogFilePath = "batch-run.log";
// Collect all files in the directory — SearchOption.TopDirectoryOnly skips subdirectories
string[] files = Directory.GetFiles("scans/", "*.*", SearchOption.TopDirectoryOnly);
var options = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Balanced, // balances throughput vs accuracy
ExpectBarcodeTypes = BarcodeEncoding.Code128 | BarcodeEncoding.QRCode, // limit to known formats
ExpectMultipleBarcodes = true // scan each file fully
};
// Three outcome counters: success (decoded), empty (read OK but no barcode found), fail (exception)
int successCount = 0;
int failCount = 0;
int emptyCount = 0;
var errors = new List<(string File, string Error)>(); // per-file error context for root cause analysis
var sw = Stopwatch.StartNew();
foreach (string file in files)
{
try
{
BarcodeResults results = BarcodeReader.Read(file, options);
// Empty result is not an exception — the file was read but contained no matching barcode
if (results == null || results.Count == 0)
{
emptyCount++;
errors.Add((file, "No barcodes detected")); // record so caller can adjust options
continue;
}
foreach (BarcodeResult result in results)
{
Console.WriteLine($"{Path.GetFileName(file)} | {result.BarcodeType} | {result.Value}");
}
successCount++;
}
catch (IronBarCodePdfPasswordException)
{
// PDF is password-protected — supply password via PdfBarcodeReaderOptions to recover
failCount++;
errors.Add((file, "Password-protected PDF"));
}
catch (IronBarCodeFileException ex)
{
// File is corrupted, locked, or in an unsupported image format
failCount++;
errors.Add((file, $"File error: {ex.Message}"));
}
catch (FileNotFoundException ex)
{
// File was in the directory listing but deleted before the read completed (race condition)
failCount++;
errors.Add((file, $"File not found: {ex.Message}"));
}
catch (IronBarCodeException ex)
{
// Catch-all for any other IronBarcode-specific errors not handled above
failCount++;
errors.Add((file, $"{ex.GetType().Name}: {ex.Message}"));
}
catch (Exception ex)
{
// Unexpected non-IronBarcode error — log the full type for investigation
failCount++;
errors.Add((file, $"Unexpected: {ex.GetType().Name}: {ex.Message}"));
}
}
sw.Stop();
// Summary report — parse failCount > 0 in CI/CD to set a non-zero exit code
Console.WriteLine("\n--- Batch Summary ---");
Console.WriteLine($"Total files: {files.Length}");
Console.WriteLine($"Success: {successCount}");
Console.WriteLine($"Empty reads: {emptyCount}");
Console.WriteLine($"Failures: {failCount}");
Console.WriteLine($"Elapsed: {sw.Elapsed.TotalSeconds:F1}s");
if (errors.Any())
{
Console.WriteLine("\n--- Error Details ---");
foreach (var (errorFile, errorMsg) in errors)
{
Console.Error.WriteLine($" {Path.GetFileName(errorFile)}: {errorMsg}");
}
}Imports IronBarCode
Imports IronBarCode.Exceptions
Imports System.Diagnostics
' Enable built-in logging for the entire batch run so internal processing steps
' are captured in the log file alongside the per-file console output
IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.All
IronSoftware.Logger.LogFilePath = "batch-run.log"
' Collect all files in the directory — SearchOption.TopDirectoryOnly skips subdirectories
Dim files As String() = Directory.GetFiles("scans/", "*.*", SearchOption.TopDirectoryOnly)
Dim options As New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Balanced, ' balances throughput vs accuracy
.ExpectBarcodeTypes = BarcodeEncoding.Code128 Or BarcodeEncoding.QRCode, ' limit to known formats
.ExpectMultipleBarcodes = True ' scan each file fully
}
' Three outcome counters: success (decoded), empty (read OK but no barcode found), fail (exception)
Dim successCount As Integer = 0
Dim failCount As Integer = 0
Dim emptyCount As Integer = 0
Dim errors As New List(Of (File As String, Error As String))() ' per-file error context for root cause analysis
Dim sw As Stopwatch = Stopwatch.StartNew()
For Each file As String In files
Try
Dim results As BarcodeResults = BarcodeReader.Read(file, options)
' Empty result is not an exception — the file was read but contained no matching barcode
If results Is Nothing OrElse results.Count = 0 Then
emptyCount += 1
errors.Add((file, "No barcodes detected")) ' record so caller can adjust options
Continue For
End If
For Each result As BarcodeResult In results
Console.WriteLine($"{Path.GetFileName(file)} | {result.BarcodeType} | {result.Value}")
Next
successCount += 1
Catch ex As IronBarCodePdfPasswordException
' PDF is password-protected — supply password via PdfBarcodeReaderOptions to recover
failCount += 1
errors.Add((file, "Password-protected PDF"))
Catch ex As IronBarCodeFileException
' File is corrupted, locked, or in an unsupported image format
failCount += 1
errors.Add((file, $"File error: {ex.Message}"))
Catch ex As FileNotFoundException
' File was in the directory listing but deleted before the read completed (race condition)
failCount += 1
errors.Add((file, $"File not found: {ex.Message}"))
Catch ex As IronBarCodeException
' Catch-all for any other IronBarcode-specific errors not handled above
failCount += 1
errors.Add((file, $"{ex.GetType().Name}: {ex.Message}"))
Catch ex As Exception
' Unexpected non-IronBarcode error — log the full type for investigation
failCount += 1
errors.Add((file, $"Unexpected: {ex.GetType().Name}: {ex.Message}"))
End Try
Next
sw.Stop()
' Summary report — parse failCount > 0 in CI/CD to set a non-zero exit code
Console.WriteLine(vbCrLf & "--- Batch Summary ---")
Console.WriteLine($"Total files: {files.Length}")
Console.WriteLine($"Success: {successCount}")
Console.WriteLine($"Empty reads: {emptyCount}")
Console.WriteLine($"Failures: {failCount}")
Console.WriteLine($"Elapsed: {sw.Elapsed.TotalSeconds:F1}s")
If errors.Any() Then
Console.WriteLine(vbCrLf & "--- Error Details ---")
For Each errorDetail In errors
Console.Error.WriteLine($" {Path.GetFileName(errorDetail.File)}: {errorDetail.Error}")
Next
End If输出

执行过程中,控制台会为每个解码的条形码输出一行信息,随后是包含文件计数、成功次数、空读取次数、失败次数和运行时间的摘要。错误信息会列出对应的文件名和失败原因。
该过程区分三种结果类别:成功(找到并解码条形码)、空(读取文件但未检测到条形码)和失败(抛出异常)。 这种区别很重要,因为空读取和读取失败需要不同的处理方式。 空读取可能需要更广泛的格式设置,而失败通常表明基础架构问题,例如缺少文件、资源被锁定或缺少本地依赖项。
错误列表维护每个文件的上下文,以支持根本原因分析。 在CI/CD管道中,解析此输出以设置退出代码(完全成功时为零,当failCount大于零时为非零)或将错误详细信息转发到警报系统。
为了更高的吞吐量,通过设置MaxParallelThreads以匹配可用的CPU核心来启用并行处理。 通过将并行迭代包装在Parallel.ForEach中并使用线程安全的集合来保持错误列表的每个文件的隔离。
进一步阅读
- IronBarcode教程:读取条形码:端到端读取指南。 -防止误报:减少噪声图像中的虚假读数。 -图像校正方法:提高读取准确率的滤镜。
- Docker 设置指南:使用正确的原生依赖项进行容器化部署。
- BarcodeReaderOptions API 参考:完整的配置文档。
- IronBarcode更新日志:版本特定的修复和功能添加。
当管道准备就绪投入生产时,请查看许可选项。
常见问题解答
如何使用 IronBarcode 在条形码操作中处理错误?
IronBarcode 提供类型化异常和内置日志功能以有效管理和处理条形码操作中的错误,确保您的应用程序顺利运行。
IronBarcode 提供哪些用于调试条形码问题的功能?
IronBarcode 包含诊断信息提取和生产就绪的批量错误隔离,帮助开发人员有效地识别和解决与条形码相关的问题。
IronBarcode 是否可以在条形码处理过程中记录错误?
是的,IronBarcode 具有内置的日志功能,允许开发人员在条形码处理过程中捕获和记录错误详细信息,便于调试。
IronBarcode 中的类型化异常是什么?
IronBarcode 中的类型化异常是特定的错误类型,提供有关条形码操作问题的详细信息,使开发人员更易于诊断和修复问题。
IronBarcode 如何帮助批量错误隔离?
IronBarcode 提供生产就绪的批量错误隔离,有助于将错误的条形码操作与成功的操作分开,从而简化批量处理中的错误管理。
有没有办法使用 IronBarcode 提取条形码操作的诊断信息?
是的,IronBarcode 提供诊断信息提取工具,帮助开发人员收集有关条形码操作的详细信息,便于排错和错误解决。
How can I catch and interpret exceptions effectively in IronBarcode?
Catch and interpret exceptions in IronBarcode by ordering your try-catch blocks from specific to general. Start with actionable exceptions like file errors or PDF password issues and end with the base IronBarCodeException to ensure comprehensive error handling.
What properties of BarcodeResult can be used for post-mortem analysis?
The BarcodeResult object in IronBarcode provides properties like BarcodeType, Value, PageNumber, and Points (coordinates) for post-mortem analysis. These properties help in understanding unexpected results by checking the actual versus expected barcode type and verifying the page number.
In IronBarcode, how can I prevent false positives during barcode reads?
To prevent false positives during barcode reads in IronBarcode, you can use image filters to enhance image quality and the RemoveFalsePositive option. Additionally, adjusting reading speed and ExpectBarcodeTypes can minimize errors from noisy backgrounds.
How does IronBarcode handle errors from encrypted PDFs?
IronBarcode handles errors from encrypted PDFs using the IronBarCodePdfPasswordException. To process such files, supply the password using PdfBarcodeReaderOptions or log and skip them for non-disruptive barcode processing.

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。