MVC Framework builds up the 1st log

Source: Internet
Author: User
Tags string format log4net

1. Log section

First is a log interface

 Public interface ILogger {//<summary>///check level log is enabled///</summary>/ <param name= "level" > Log levels <seealso cref= "T:rosefinch.core.logging.loglevel"/></param>//<r        Eturns> returns TRUE if enabled, otherwise false</returns> bool IsEnabled (LogLevel level) is returned; <summary>///Record level logs///</summary>//<param name= "Levels" > Logging <seeal So cref= "T:rosefinch.core.logging.loglevel"/></param>//<param name= "message" > Content to be recorded </param&gt        ;        void Log (LogLevel level, object message); <summary>///Record level logs///</summary>//<param name= "Levels" > Logging <seeal So cref= "T:rosefinch.core.logging.loglevel"/></param>//<param name= "message" > Content to be recorded </param&gt        ; <param name= "Exception" > Exceptions </param> void Log (LogLevel level, exception exception, ObjeCT message); <summary>///Record level logs///</summary>//<param name= "Levels" > Logging <seeal So cref= "T:rosefinch.core.logging.loglevel"/></param>//<param name= "format" > Content format to be recorded <see cref = "M:System.String.Format (system.string,system.object[])"/></param>//<param name= "args" > Replace Format Placeholder Parameters </param> void Log (LogLevel level, string format, params object[] args);

Then the log factory interface

Public interface iloggerfactory    {        //<summary>//based loggername get <see cref= "T: Rosefinch.Core.Logging.ILogger "/>//</summary>//        <param name=" Loggername "> Journal name (ex: Log4net's logger configuration name) </param>///        <returns><see cref= "T:rosefinch.core.logging.ilogger" /></returns>        ILogger GetLogger (string loggername);        <summary>///        based loggername get <see cref= "T:rosefinch.core.logging.ilogger"/>//        </ summary>//        <returns><see cref= "T:rosefinch.core.logging.ilogger"/></returns>        ILogger GetLogger ();    }

Log Level Enumeration

  Public enum LogLevel    {        Debug,        information,        Warning,        Error,        Fatal    }

Implementation of the log

 Using Log4net;        <summary>///ILogger//</summary> public class Log4netlogger:ilogger {log4net        Private ILog log;        Internal Log4netlogger (ILog log) {this.log = log; }///<summary>///Check whether the level log is enabled//</summary>//<param name= "Levels" > Log level <seealso cref= "T:rosefinch.core.logging.loglevel"/></param>//<returns> returns TRUE if enabled returns FAL                Se</returns> public bool IsEnabled (LogLevel level) {switch (level) {                Case LogLevel.Debug:return this.log.IsDebugEnabled;                Case LogLevel.Information:return this.log.IsInfoEnabled;                Case LogLevel.Warning:return this.log.IsWarnEnabled;                Case LogLevel.Error:return this.log.IsErrorEnabled;              Case Loglevel.fatal:      return this.log.IsFatalEnabled;            Default:return false; }}///<summary>//Record level Log///</summary>//<param name= "levels "> Log level <seealso cref=" T:rosefinch.core.logging.loglevel "/></param>//<param name=" message "> Required The contents of the record </param> public void Log (LogLevel level, object message) {if (!this.            IsEnabled (level)) {return;                    } switch (level) {case LogLevel.Debug:this.log.Debug (message);                Return                    Case LogLevel.Information:this.log.Info (message);                Return                    Case LogLevel.Warning:this.log.Warn (message);                Return                    Case LogLevel.Error:this.log.Error (message);      Return          Case LogLevel.Fatal:this.log.Fatal (message);                Return            Default:return; }}///<summary>//Record level Log///</summary>//<param name= "levels "> Log level <seealso cref=" T:rosefinch.core.logging.loglevel "/></param>//<param name=" message "> Required What's recorded </param>///<param Name= "Exception" > Exceptions </param> public void Log (LogLevel level, syste M.exception Exception, Object message) {if (!this.            IsEnabled (level)) {return; } switch (level) {case LOGLEVEL.DEBUG:THIS.LOG.DEBUG (message, ex                    Ception);                Return                    Case LogLevel.Information:this.log.Info (message, exception);                Return                 Case loglevel.warning:   This.log.Warn (message, exception);                Return                    Case LogLevel.Error:this.log.Error (message, exception);                Return                    Case LogLevel.Fatal:this.log.Fatal (message, exception);                Return            Default:return; }}///<summary>//Record level Log///</summary>//<param name= "levels "> Log level <seealso cref=" T:rosefinch.core.logging.loglevel "/></param>//<param name=" format "> Need to Remember Content format <see cref= "M:System.String.Format (system.string,system.object[])"/></param>//<param name= "             Args "> Replace the parameters of the format placeholder </param> public void Log (LogLevel-level, string format, params object[] args) { if (!this.            IsEnabled (level)) {return; } switch (level) {case LOgLevel.Debug:this.log.DebugFormat (format, args);                Return                    Case LogLevel.Information:this.log.InfoFormat (format, args);                Return                    Case LogLevel.Warning:this.log.WarnFormat (format, args);                Return                    Case LogLevel.Error:this.log.ErrorFormat (format, args);                Return                    Case LogLevel.Fatal:this.log.FatalFormat (format, args);                Return            Default:return; }        }

  Log Factory implementation

 public class Log4netfactory:iloggerfactory {private static bool _isconfigloaded; <summary>///constructor (default load "~/config/log4net.config" as log4net profile)///</summary> Public         Log4netfactory (): This ("~/config/log4net.config") {}///<summary>//constructor         </summary>//<param name= "Configfilename" >//<remarks>/// <para>log4net configuration file path, supported in the following formats:</para>//<list type= "bullet" >//&L             t;item>~/config/log4net.config</item>//<item>~/web.config</item>///        <item>c:\abc\log4net.config</item>//</list>/</remarks>            </param> public log4netfactory (string configfilename) {if (!_isconfigloaded) {if (string. IsnulloremPty (Configfilename)) {configfilename = "~/config/log4net.config";                } System.IO.FileInfo FileInfo = new System.IO.FileInfo (Webutility.getphysicalfilepath (configfilename)); if (!fileinfo.exists) {throw new ApplicationException (string.                Format ("log4net config file {0} not found", fileinfo.fullname));                } xmlconfigurator.configure (FileInfo);            _isconfigloaded = true;        }}///<summary>//Loggername get <see cref= "T:rosefinch.core.logging.ilogger"/> </summary>//<param name= "Loggername" > Journal name (ex: log4net logger configuration name) </param>//< Returns><see cref= "T:rosefinch.core.logging.ilogger"/></returns> public ILogger GetLogger (String lo        Ggername) {return new Log4netlogger (Logmanager.getlogger (loggername)); } public IloggEr GetLogger () {return new Log4netlogger (Logmanager.getlogger ("Rosefinch")); }

MVC Framework builds up the 1st log

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.