【问题标题】:Log the exception in a log file if there is conversion exception如果存在转换异常,则将异常记录在日志文件中
【发布时间】:2015-03-11 11:08:46
【问题描述】:

如果在迭代文件数据时出现任何转换异常,我需要记录异常消息,然后继续下一个数据。

例如,如果输入文件有 10 条记录,并且由于第 7 条记录而引发异常。然后我需要为 1-6 和 8-10 生成返回行,以及第 7 条记录的日志记录异常。

我正在使用以下代码使用文件数据生成行:

public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
{
    using (FileEngine file = FluentFile.For<SomeDataRecordETL>().From(FilePath))
    {   
        foreach (object obj in file)
        {
            yield return Row.FromObject(obj);
        }
    }
}

【问题讨论】:

  • FileEngine 可能会暴露一个 OnError 方法。当前的 FileHelpers 库有 3 种错误模式:ThrowExceptionSaveAndContinueIgnoreAndContinue。您或许可以使用SaveAndContinue 作为解决方案的一部分。

标签: c# rhino yield-return rhino-etl


【解决方案1】:

如果你说的是FromObject()抛出的异常,那么你可以直接使用try-catch

foreach (object obj in file)
{
    try
    {
        yield return Row.FromObject(obj);
    }
    catch (Exception ex) // ideally, this should be some specific exception type
    {
        Log(ex);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2011-10-23
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    相关资源
    最近更新 更多