Search code examples
c#csvhelper

How can I display my created error list using csvhelper


Apologies as this may seem a dumb question but I am missing something and not sure what to do.

As part of an application I'm making I am using CSVHelper for creating a file which contains details of any errors that may arise when using the application, such as a failure to connect to the database, values missing etc. I had been following this tutorial here: https://www.youtube.com/watch?v=fRaSeLYYrcQ and edited it more to suit my needs. I have an error logging file like so:

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

            }
        };

    }
}

So here I have made it so the column headers in the csv file will be ErrorType, Relating and Message

I also have an export log file:

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);

            }
        }

    }
}

In my main file program.cs I then use these by doing something like:

        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");

        }

And I now need to create the csv file with the list of errors by doing something like:

 ExportLog.Log(config.ExportFolder, exportFile, "error logging list here");

However I'm not sure how to actually display these lists that I am creating if that makes sense?

As I can not do:

ExportLog.Log(config.ExportFolder, exportFile, ErrorLogging.GetErrors());

since it will be looking for parameters to be passed in. I know I am missing something very obvious here but can't figure out what, any help would be appreciated


Solution

  • So I ended up solving my issue, I had been going about it the wrong way, I did not need to have:

    public static List<ErrorLogging> GetErrors(string type, string relating, string message)
    {
    
        return new List<ErrorLogging>
        {
            new ErrorLogging
            {
                ErrorType = type,
                Relation = relating,
                Message = message
    
            }
        };
    
    }
    

    in ErrorLogging.cs, it simply just needed to be:

    public class ErrorLogging
    {
        public string ErrorType { get; set; }
        public string Relation { get; set; }
        public string Message { get; set; }
    
    }
    

    Then in program.cs I can declare a new list like so:

      List<ErrorLogging> ErrorList = new List<ErrorLogging>();
    

    and then when there is a point I would need to add to the list I can simply do something along the lines of:

                ErrorList.Add(new ErrorLogging()
                {
                    ErrorType = "Warning",
                    Relation = "Database",
                    Message = "Not all properties found in the database"
                });
    

    And then when it comes to creating the csv with the list of errors, I can check whether the list is empty or not, if not empty then create:

           if (ErrorList.Any())
           {
                ExportLog.Log(config.LogFolder, errorLog, ErrorList);
    
            }