The Adventures of the Snail (ii) Web framework (middle)

Source: Internet
Author: User
Tags log4net

This article briefly describes the AUTOFAC used by the framework, and uses the IOC provided by AUTOFAC to manage the lifecycle of all objects in the entire Web project and to implement the framework interface-oriented programming. The following is a description of the framework's log system.

First, before the introduction

Is there a need for the framework log to exist? If you think your frame will never bug, or you can restore the scene based on the error, you don't need a log. Currently, such items do not exist (except HelloWorld). Therefore, the log framework is still necessary.

The log frame is used to record the error details of the system during operation, to record the unexpected situation during the system's use, and to record some state information of the system. Through different classification, different severity of log information can easily solve problems for us, to improve the efficiency of the system to provide data support.

Currently. NET direction of mature log framework has log4net, Nlog, and so on, these two frameworks each have pros and cons, how to choose according to the level of personal familiarity, as well as the use of the environment to judge.

Second, initialize

Perhaps for a variety of reasons, we will replace the original log frame after the system is put into use. Therefore, at the beginning of the system design, we should be careful not to let the framework itself and the log framework has too much reliance. So, we define the level of logging and the standard interface for logging:

1. Log level

 /// <summary>    ///  Log Level     /// </ summary>    public enum loglevel {         /// <summary>        ///  Commissioning          /// </summary>         debug,        /// <summary>         ///  Information         /// </summary>         information,        ///  <summary>        ///  Warnings          /// </summary>        Warning,         /// <summary>        ///  Error          /// </summary>        error,         /// <summary>         ///  Critical Errors         /// </summary>         fatal    }

    2. Log base interface:

    /// <summary>    ///  Log Interface     /  </summary>    public interface ILogger {         /// <summary>        ///   Determine if the log level is open         /// </summary>         /// <param name= "level" > Log Levels </param>         /// <returns> returns True if turned on, otherwise returns false</returns>         bool isenabled (Loglevel level);         /// <summary>        ///  Logging          /// </summary>        /  <param name= "LeVel "> Log level </param>        /// <param name=" Exception "> Exception information </param>        /// <param name=" Format "> Log formats </param>        /// <param name=" Args "> Format parameter </param>        void log for log format (LogLevel  Level, exception exception, string format, params object[] args);     }

    3. Log extension method:

  public static class LoggingExtensions {         /// <summary>        ///  Logging Debug Logs          /// </summary>         /// <param name= "Logger" > Log Objects </param>         /// <param name= "message" > Messages </param>         public static void debug (this ilogger logger, string message)  {             filteredlog (logger,  Loglevel.debug, null, message, null);        }         /// <summary>         ///  Logging Information Log         /// </summary>        /// < Param name= "Logger" > Log Objects </param>        /// < Param name= "message" > Messages </param>        public  Static void information (this ilogger logger, string message)  {             filteredlog (Logger, loglevel.information,  null, message, null);        }         /// <summary>        ///  Logging warning Logs         /// </summary>         /// <param name= "Logger" > Log Objects </param>         /// <pAram name= "message" > Messages </param>        public static  void warning (this ilogger logger, string message)  {             filteredlog (Logger, loglevel.warning, null,  message, null);        }         /// <summary>        ///  Logging error logs         /// </summary>         /// <param name= "Logger" > Log Objects </param>         /// <param name= "message" > Messages </param>         public static void error (This ilogger logger, string message)  {     &Nbsp;      filteredlog (Logger, loglevel.error, null, message,  null);         }        /  <summary>        ///  Log Critical error logs          /// </summary>        ///  <param name= "Logger" > Log Object </param>        ///  <param name= "message" > Messages </param>        public  static void fatal (this ilogger logger, string message)  {             filteredlog (logger, loglevel.fatal, null ,  message, null);        }         public sTatic void debug (this ilogger logger, exception exception, string  Message)  {            filteredlog (logger,  loglevel.debug, exception, message, null);         }        public static void information (This  ilogger logger, exception exception, string message)  {             filteredlog (logger, loglevel.information,  Exception, message, null);        }         public static void warning (this ilogger logger,  Exception exception, string message)  {             filteredlog (Logger,&nbsP Loglevel.warning, exception, message, null);         }         public static void error (This ILogger  logger, exception exception, string message)  {             filteredlog (logger, loglevel.error, exception,  Message, null);        }         public static void fatal (this ilogger logger, exception  Exception, string message)  {             filteredlog (logger, loglevel.fatal, exception, message, null);         }        public static void  debug (THIS&NBSP;ILOGGER&NBSP;LOGGER,&NBsp;string format, params object[] args)  {             filteredlog (Logger, loglevel.debug, null, format, args);         }        public  Static void information (this ilogger logger, string format, params  Object[] args)  {            filteredlog ( Logger, loglevel.information, null, format, args);         }        public static void warning (This  ilogger logger, string format, params object[] args)  {             filteredlog (logger, loglevel.warning,  Null, format, args),         }        public  Static void error (this ilogger logger, string format, params object[]  args)  {            filteredlog (logger,  loglevel.error, null, format, args);         }         public static void fatal (This ILogger  logger, string format, params object[] args)  {             filteredlog (logger, loglevel.fatal, null,  Format, args);        }         public static void debug (This ilogger logger, exception exception,  string format, paraMs object[] args)  {             Filteredlog (Logger, loglevel.debug, exception, format, args);         }        public static void  Information (this ilogger logger, exception exception, string format, params  object[] args)  {             Filteredlog (Logger, loglevel.information, exception, format, args);         }        public static void  warning (this ilogger logger, exception exception, string format,  Params object[] args)  {             Filteredlog (Logger, loglevel.warninG, exception, format, args);        }         public static void error (this ilogger logger,  Exception exception, string format, params object[] args)  {             filteredlog (logger, loglevel.error,  Exception, format, args);        }         public static void fatal (this ilogger logger, exception  exception, string format, params object[] args)  {             filteredlog (logger, loglevel.fatal, exception,  Format, args);        }         private static&Nbsp;void filteredlog (ilogger logger, loglevel level, exception exception,  string format, object[] objects)  {             if  (logger. IsEnabled (level))  {                 logger. Log (level, exception, format, objects);             }        }    }

We can achieve their own implementation based on Log4net Log4netlogger, based on the implementation of Nlog Nloglogger, all inherit ILogger interface, and the implementation of the log method, due to a number of similar articles, this article does not repeat.

Iii. advantages and disadvantages of combining with IOC

Set the log frame as a global singleton, which reduces createlogger each time and improves efficiency. But sometimes we need to log records before the framework is initialized. Like the Adventures of the Snail (ii) the Preapplicationstartmethodof the section in the Web framework (above), we need to do a copy initialization of the plugin before the framework is initialized, logging here, It is convenient for us to analyze and solve the problem of frame startup failure. It is therefore necessary to provide a log system independent of the framework, as well as to provide a log system within the IOC environment used for the framework. We can add a loggerfactory, with the factory according to different conditions to provide a specific way to implement the log.

Iv. use of logs

A healthy log should contain: Occurrence time, log level, log category, log stack, log contents.

public class homecontroller:controller{public ILogger logger{get;set;}        The log uses the attribute to inject private readonly iservice _service;    Public HomeController (IService service) {_service=service; Public ActionResult Index () {var val=_service.        Dosomebusiness (); Logger.information (NULL, "Logging, starting the business.")    The result is: "+val);//Return View (Val); }}

V. Summary

The log framework is essential, how to standardize logging, facilitate query troubleshooting, and also need to do more work outside the framework.

The Adventures of the Snail (ii) Web framework (middle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.