【发布时间】:2021-12-28 06:24:20
【问题描述】:
道歉,因为这似乎是一个愚蠢的问题,但我错过了一些东西,不知道该怎么做。
作为我正在制作的应用程序的一部分,我正在使用 CSVHelper 创建一个文件,其中包含使用该应用程序时可能出现的任何错误的详细信息,例如无法连接到数据库、缺少值等。我有一直在关注本教程:https://www.youtube.com/watch?v=fRaSeLYYrcQ 并对其进行了更多编辑以满足我的需要。我有一个像这样的错误日志文件:
public class ErrorLogging
{
public string ErrorType { get; set; }
public string Relation { get; set; }
public string Message { get; set; }
public static List<ErrorLogging> GetErrors(string type, string relating, string message)
{
return new List<ErrorLogging>
{
new ErrorLogging
{
ErrorType = type,
Relation = relating,
Message = message
}
};
}
}
所以我在这里做了,所以 csv 文件中的列标题将是 ErrorType、Relating 和 Message
我还有一个导出日志文件:
class ExportLog
{
public static void Log<T>(string path, string file, List<T>report)
{
var csvPath = Path.Combine(path + "\\", file);
using (var streamWriter = new StreamWriter(csvPath))
{
using (var csvWriter = new CsvWriter(streamWriter, CultureInfo.InvariantCulture))
{
csvWriter.WriteRecords(report);
}
}
}
}
在我的主文件 program.cs 中,然后我通过执行以下操作来使用它们:
if (validateRoleTitles is not false)
{
log.Information("Roles found");
}
else
{
log.Warning("Roles could not be found");
ErrorLogging.GetErrors("Warning", "Database", "Not all roles found in the database");
}
我现在需要通过执行以下操作来创建包含错误列表的 csv 文件:
ExportLog.Log(config.ExportFolder, exportFile, "error logging list here");
但是,如果有意义,我不确定如何实际显示我正在创建的这些列表?
我做不到:
ExportLog.Log(config.ExportFolder, exportFile, ErrorLogging.GetErrors());
因为它将寻找要传入的参数。我知道我在这里遗漏了一些非常明显的东西,但无法弄清楚是什么,任何帮助将不胜感激
【问题讨论】:
-
这不能回答您的问题 - 您是否考虑过使用日志库?类似logging.apache.org/log4net
-
有人特别要求我使用 csvhelper 执行此操作,但我会考虑自己使用,谢谢
-
旁注:
Path.Combine(path, file),而不是Path.Combine(path + "\\", file) -
错误列表 - 但您的列表只包含一个错误?当您调用 GetErrors 时,您不会存储返回值?
-
也许使用一些现有的日志记录规定并为其编写一个新的输出接收器(如果不存在的话)创建一个 CSV 将是有益的