DEV Community

Matthew Cullen
Matthew Cullen

Posted on

Read/Write to Files in C# with errorLogging example

https://github.com/3Mustard/cSharpCliErrorLogger

Example of how to use ErrorLogger to write one or multiple errors to a file and display them in the console

string filePath = System.IO.Path.GetFullPath(@"..\logs.txt");
ErrorLogger eLogger = new ErrorLogger(filePath);

Error err = new Error();
Error err2 = new Error();
Error err3 = new Error();
err.Message = "new error message 1";
err.Date = DateTime.UtcNow.ToString();
err2.Message = "new error message 2";
err2.Date = DateTime.UtcNow.ToString();
err3.Message = "new error message 3";
err3.Date = DateTime.UtcNow.ToString();

List<Error> errors = new List<Error>();
errors.Add(err2);
errors.Add(err3);

eLogger.LogNewError(err);
eLogger.LogAllNewErrors(errors);
eLogger.DisplayErrorsToConsole();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)