c# - CSVHelper:在抛出 BadDataException 时记录错误
问题描述
我正在尝试配置 BadDataException 消息以包含更多信息,例如出于调试目的而发生异常的位置。我已经编写了下面的代码,但似乎抛出的异常正在打印一般错误消息,而不是我配置的错误消息。我想我在这里遗漏了一个逻辑错误,但我不确定。任何帮助表示赞赏!
public void HeaderColumnParser(string PathToFile) {
try {
using (TextReader fileReader = File.OpenText(PathToFile)) {
var csv = new CsvReader(fileReader, CultureInfo.InvariantCulture);
CsvConfiguration csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture) {
BadDataFound = context => {
throw new BadDataException(context, string.Format("BadDataFound: Bad entry found at field {0}, \n row {1}: {2}", context.Field, context.RawRow, context.RawRecord.Replace("\"", "'")));
}
};
// csv.Configuration.AllowComments = true;
csv.Read();
csv.ReadHeader();
//while condition returns true till last row
while (csv.Read()) {
string RefDes = "";
//Index 0 gets the ReferenceDesignator if header column exists
if (ColumnIndex[0] > 0) {
if (csv.TryGetField(ColumnIndex[0], out string value)) {
RefDes = value;
}
}
//gets MPN
if (ColumnIndex[1] > 0) {
if (csv.TryGetField(ColumnIndex[1], out string value)) {
MPN.Add(new ManufacturerPartNumber(RefDes, value));
}
}
//Gets Value
if (ColumnIndex[2] > 0) {
if (csv.TryGetField(ColumnIndex[2], out string value)) {
Values.Add(new ComponentValue(RefDes, value));
}
}
//Gets Short description
if (ColumnIndex[3] > 0) {
if (csv.TryGetField(ColumnIndex[3], out string value)) {
DescriptionShort.Add(new ShortDescription(RefDes, value));
}
}
//Gets Long description
if (ColumnIndex[4] > 0) {
if (csv.TryGetField(ColumnIndex[4], out string value)) {
DescriptionLong.Add(new LongDescription(RefDes, value));
}
}
//Gets Manufacturer
if (ColumnIndex[5] > 0) {
if (csv.TryGetField(ColumnIndex[5], out string value)) {
Manufacturer.Add(new Manufacturer(RefDes, value));
}
}
//Gets DNI components
if (ColumnIndex[6] > 0) {
if (csv.TryGetField(ColumnIndex[6], out string value)) {
DNI.Add(new DNI(RefDes, value));
}
}
//Gets the datasheet
if (ColumnIndex[7] > 0) {
if (csv.TryGetField(ColumnIndex[7], out string value)) {
DataSheet.Add(new DataSheet(RefDes, value));
}
}
}
}
}
catch (BadDataException ex) {
throw;
}
}
解决方案
您创建了CsvConfiguration
,但从未使用过它。您可以在创建CsvReader
.
CsvConfiguration csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture)
{
BadDataFound = context => {
throw new BadDataException(context, string.Format("BadDataFound: Bad entry found at field {0}, \n row {1}: {2}", context.Field, context.RawRow, context.RawRecord.Replace("\"", "'")));
}
};
var csv = new CsvReader(fileReader, csvConfig);
或者您可以BadDataFound
在创建CsvReader
.
编辑:自版本 20.0.0 起,您无法再编辑CsvReader.Configuration
。正如Snympi 所提到的,它现在是只读的。
var csv = new CsvReader(fileReader, CultureInfo.InvariantCulture);
csv.Configuration.BadDataFound = context =>
{
throw new BadDataException(context, string.Format("BadDataFound: Bad entry found at field {0}, \n row {1}: {2}", context.Field, context.RawRow, context.RawRecord.Replace("\"", "'")));
};
推荐阅读
- python - 不可追踪的对象属性
- python - 如何在 Wagtail Admin 中获取 CharField 的下拉列表?
- regex - 将长线包裹*在*一个表格单元格内
- c# - 在 C# MVC 中使用 Stripe api 兑换促销代码
- pine-script - 为多头和空头条目创建单独的策略警报
- xml - 每张图像具有多个类/名称的 Pascal VOC XML 数据集注释不显示
- docker - 无法使用 localhost 从 PC 浏览器访问在 docker 容器中运行的 selenium hub
- javascript - 是否可以自定义 Firebase 云消息通知?
- reactjs - React Native - 当状态没有改变时,函数为相同的状态返回不同的值
- python - 从 s3 存储桶加载保存的模型时出错