In the software development process, logging is very much important.
For various situations, we need to log information in different media(file, database) or some times need to send an email.

There might be a requirement that includes;
1. Debugging information are logged into file.
2. Errors are sent via email.
3. Other critical information, like users' IPAddress etc, should be logged into database.

"Logger" is a class, better to say rich customized logging class, that logs information in various media using "log4net" logging framework.
And the functionalities of the class are dependent on below files;
  1. log4net.config : Configuration file for log4net.
  2. log4net.dll : log4net framework DLL.
  3. Two configuration keys should be required (generally configured in the App.config/Web.config) which are;
    1. <add key="log4net.config" value="C:\log4net.config" /> - "Logger" class read this key to load the "log4net.config" file.
    2. <add key="log4net.defaultlogtype" value="None" /> - There are 5 possible values;
      1. None : All logs are logged based on "Logger.LogType" (please refer below examples).
      2. File : All logs are logged into file - Use in debug mode
      3. Email: All logs are sent via email - Use in deployment/QA mode
      4. Sql : All logs are logged into MS SQL Server - Use in deployment/QA mode
      5. ClientLog : All logs are logged into another file type that are configured in

And it has very flexible configurations. For more information, please refer below examples;

1. Static Logging
  • Preferable during development.
    class StaticLogging
    {
        //Logs into file
        Logger logger = new Logger("log4net.config", typeof(StaticLogging), Logger.LogType.File);
        public void Create()
        {
            logger.Log(Logger.LogLevel.Info, "Entering...");
            //TODO: logic ....
            logger.Log(Logger.LogLevel.Error, "Error", new Exception());
            //TODO: logic ....
            logger.Log(Logger.LogLevel.Info, "Exiting...");
        }
    }
2. Dynamic Logging
  • Preferable during deployment/QA.
    class DynamicLogging
    {
        Logger logger = new Logger("log4net.config", typeof(DynamicLogging));
        public void Create()
        {
            //Logs into file
            logger.Log(Logger.LogLevel.Info, "Entering...",Logger.LogType.File);
            /*
             * TODO: logic ....
            */
            //logs into sql server
            logger.Log(Logger.LogLevel.Error, "Error", new Exception(), Logger.LogType.Sql);
            //sends an email to admin
            logger.Log(Logger.LogLevel.Fatal, "Fatal - Error", new Exception("Fatal error"), Logger.LogType.Email);
            /*
             * TODO: logic ....
             */
            logger.Log(Logger.LogLevel.Info, "Exiting...",Logger.LogType.File);
        }
    }
3. Configurable Logging
  • Preferable during maintenance/QA.
    /// <summary>
    /// The class depends on below key, configured in app.config
    /// Possible logType values: None=0,File=1,Email=2,ClientLog=3,Sql=4
    /// <add key="log4net.defaultlogtype" value="None" />
    /// None : class works as DynamicLogging
    /// File : class works as Static logging but all the logs are logged into file - Use in debug mode
    /// Email: class works as Static logging but all the logs are sent via email - Use in User Acceptance mode
    /// Sql  : class works as Static logging but all the logs are logged into sql server 
    /// ClientLog  : class works as Static logging but all the logs are logged into another file type
    /// </summary>
    class ConfigurableLogging
    {
        Logger logger = new Logger("log4net.config", typeof(DynamicLogging));
        public void Create()
        {
            //Logs into file
            logger.Log(Logger.LogLevel.Info, "Entering...", Logger.LogType.File);
            /*
             * TODO: logic ....
            */
            //logs into sql server
            logger.Log(Logger.LogLevel.Error, "Error", new Exception(), Logger.LogType.Sql);
            //sends an email to admin
            logger.Log(Logger.LogLevel.Fatal, "Fatal - Error", new Exception("Fatal error"), Logger.LogType.Email);
            /*
             * TODO: logic ....
             */
            logger.Log(Logger.LogLevel.Info, "Exiting...", Logger.LogType.File);
        }
    }

Last edited Nov 23, 2010 at 12:05 PM by vrluckyin, version 25