design a logger system

 Ok, please design a logger system to follow the SOLD principles in C# for the following requirement: There are three types of messages: Info, Warn and Error. We need three loggers, one for email, which only sends an email if the message is an error and nothing else. Secondly, we need a database logger that logs all three types of log messages and their information in different tables as per log type. In the third one, we need a text file logger that logs all three types of messages into three different types of files. Logger should be in such a way that we can inject any logger with minimal changes to the application's code.


// Define an interface for the logger

public interface ILogger

{

    void LogInfo(string message);

    void LogWarn(string message);

    void LogError(string message);

}


// Define a class for the email logger

public class EmailLogger : ILogger

{

    private string email; // The email address to send the error messages


    public EmailLogger(string email)

    {

        this.email = email;

    }


    public void LogInfo(string message)

    {

        // Do nothing

    }


    public void LogWarn(string message)

    {

        // Do nothing

    }


    public void LogError(string message)

    {

        // Send an email with the error message

        Console.WriteLine($"Sending an email to {email} with the error message: {message}");

    }

}


// Define a class for the database logger

public class DatabaseLogger : ILogger

{

    private string connectionString; // The connection string to the database


    public DatabaseLogger(string connectionString)

    {

        this.connectionString = connectionString;

    }


    public void LogInfo(string message)

    {

        // Insert the message into the Info table in the database

        Console.WriteLine($"Inserting the info message into the Info table in the database: {message}");

    }


    public void LogWarn(string message)

    {

        // Insert the message into the Warn table in the database

        Console.WriteLine($"Inserting the warn message into the Warn table in the database: {message}");

    }


    public void LogError(string message)

    {

        // Insert the message into the Error table in the database

        Console.WriteLine($"Inserting the error message into the Error table in the database: {message}");

    }

}


// Define a class for the text file logger

public class TextFileLogger : ILogger

{

    private string infoFilePath; // The file path to store the info messages

    private string warnFilePath; // The file path to store the warn messages

    private string errorFilePath; // The file path to store the error messages


    public TextFileLogger(string infoFilePath, string warnFilePath, string errorFilePath)

    {

        this.infoFilePath = infoFilePath;

        this.warnFilePath = warnFilePath;

        this.errorFilePath = errorFilePath;

    }


    public void LogInfo(string message)

    {

        // Append the message to the info file

        Console.WriteLine($"Appending the info message to the info file: {message}");

        File.AppendAllText(infoFilePath, message + "\n");

    }


    public void LogWarn(string message)

    {

        // Append the message to the warn file

        Console.WriteLine($"Appending the warn message to the warn file: {message}");

        File.AppendAllText(warnFilePath, message + "\n");

    }


    public void LogError(string message)

    {

        // Append the message to the error file

        Console.WriteLine($"Appending the error message to the error file: {message}");

        File.AppendAllText(errorFilePath, message + "\n");

    }

}


// Define a class for testing

public class Program

{

     static void Main(string[] args)

     {

         // Create some loggers with different configurations

         ILogger emailLogger = new EmailLogger("admin@example.com");

         ILogger databaseLogger = new DatabaseLogger("Server=localhost;Database=LogDB;User Id=sa;Password=123;");

         ILogger textFileLogger = new TextFileLogger("info.txt", "warn.txt", "error.txt");


         // Inject any logger into the application and use it

         UseLogger(emailLogger);

         UseLogger(databaseLogger);

         UseLogger(textFileLogger);

        

         Console.ReadLine();

     }


     // Define a method that uses a logger to log some messages

     static void UseLogger(ILogger logger)

     {

          logger.LogInfo("This is an info message.");

          logger.LogWarn("This is a warn message.");

          logger.LogError("This is an error message.");

     }

}


ok, based on above given example. Now I need a custom logger macanigem where We want to formate a message in our way before to log that message and we want that logger we used throught the application. other concrete logger will be injected inside this one using same C# programming language

// Define an interface for the custom logger
public interface ICustomLogger
{
    void Log(string message);
}

// Define a class for the custom logger
public class CustomLogger : ICustomLogger
{
    private ILogger logger; // The concrete logger to be injected

    public CustomLogger(ILogger logger)
    {
        this.logger = logger;
    }

    public void Log(string message)
    {
        // Format the message in your way
        string formattedMessage = $"[{DateTime.Now}] {message}";

        // Use the concrete logger to log the formatted message
        logger.LogInfo(formattedMessage);
        logger.LogWarn(formattedMessage);
        logger.LogError(formattedMessage);
    }
}

// Define a class for testing
public class Program
{
     static void Main(string[] args)
     {
         // Create some loggers with different configurations
         ILogger emailLogger = new EmailLogger("admin@example.com");
         ILogger databaseLogger = new DatabaseLogger("Server=localhost;Database=LogDB;User Id=sa;Password=123;");
         ILogger textFileLogger = new TextFileLogger("info.txt", "warn.txt", "error.txt");

         // Create some custom loggers with different concrete loggers
         ICustomLogger customEmailLogger = new CustomLogger(emailLogger);
         ICustomLogger customDatabaseLogger = new CustomLogger(databaseLogger);
         ICustomLogger customTextFileLogger = new CustomLogger(textFileLogger);

         // Use any custom logger in the application and log a message
         customEmailLogger.Log("This is a test message.");
         customDatabaseLogger.Log("This is another test message.");
         customTextFileLogger.Log("This is yet another test message.");
        
         Console.ReadLine();
     }
}

Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form