DEV Community

Matthew Cullen
Matthew Cullen

Posted on

1 1

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)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay