C# Files Create some logging code that does the following. - Create a logging cl
ID: 3575150 • Letter: C
Question
C#
Files
Create some logging code that does the following.
-
Create a logging class.
Fields
fileName
---
Constructor(string)
Sets the file name to the argument.
Creates the file if it does not exist.
Opens the file and adds a line of text indicating the date.
---
Methods
Log method that takes a string message and a log level enum (Debug, Warn, Info, Error)
This method will open the file in append mode and add the message and enum as a new line using the following format:
LogLevel (formatted as 6 characters) Time (HH:mm:ss.ffff) Message
---
Main
Create your log object.
Call the log object with a message using the warn enum value.
Call the log object with a message using the debug enum value.
Call the log object with a message using the info enum value.
Call the log object with a message using the error enum value.
i.e.
04/08/2020
Warn 22:05:02.4324 The system performance is averaging 2.44 seconds per query.
Error 22:05:03.6421 -4 is not a valid argument, unable to proceed
Explanation / Answer
class Log
{
public string message{get;set;}
public string LogLevel{get;set;}
}
Enum LogLevel
{
Debug, Warn, Info, Error
}
class program
{
string FilePath="";
public program(string FilePath)
{
this.FilePath=FilePath;
}
public void Log(Log Log)
{
if(!File.Exists(FilePath))
{
File.Create(FilePath);
}
File.WriteAllText(string.concat(Log.logLevel," ",dateTime.Now.ToString()," ",Log.message),FilePath);
}
}
public class mainClass
{
public Static void Main(string[] args)
{
program prog=new program("");
prog.log(new log(){"Message1",LogLevel.Warn});
prog.log(new log(){"Message2",LogLevel.Debug});
prog.log(new log(){"Message3",LogLevel.Info});
prog.log(new log(){"Message4",LogLevel.Error});
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.