Log4net Components Easy Package

Source: Internet
Author: User
Tags log4net

usinglog4net;usinglog4net. Appender;usinglog4net. Config;usinglog4net. Core;usinglog4net. Layout;usingSystem;usingSystem.IO;usingsystem.web;namespaceutility{/// <summary>    ///Simple Package log4net basic functions///1 The Log4net.dll log can be used normally regardless of whether the Log4net.config file exists///2 supports two types of Logger:systemlogger and Databaselogger, and can add more Logger objects/// </summary>     Public classLogger {Private Static ReadOnlyType declaringtype =typeof(Logger); protected StaticILog Systemlog {Get; Private Set; }        protected StaticILog DatabaseLog {Get; Private Set; }        StaticLogger () {Const stringFileName ="Log4net.config"; stringBasePath = httpcontext.current! =NULL?AppDomain.CurrentDomain.SetupInformation.PrivateBinPath:AppDomain.CurrentDomain.BaseDirectory; stringFilePath =Path.Combine (BasePath, fileName); if(File.exists (FilePath)) {Xmlconfigurator.configureandwatch (NewFileInfo (FilePath)); }            Else            {                //default settings (log4net by adorner mode, support: ConsoleLog, FileLog, eventlog, etc.)Rollingfileappender Appender =NewRollingfileappender {Name="Root", File="Logs\\log.txt", Appendtofile=true, Rollingstyle= RollingFileAppender.RollingMode.Composite,//A new log file is automatically generated when the date changes or the log file size exceeds 10MDatepattern ="yyyymmdd\ ". txt\"",//If the date changes, automatically saves the history log file and generates a new log fileMaxsizerollbackups =5,//keep several history log filesLayout =NewPatternlayout ("[%d{yyyy-mm-dd HH:mm:ss.fff}] [%t]%-5level%logger PROPERTY:[%PROPERTY{NDC}]-%message%newline")//%m indicates the contents of the Log                };                Basicconfigurator.configure (Appender); Appender.            Activateoptions (); }            //add different types of parameter logger for log4net to configure which method of output log is selectedSystemlog = Logmanager.getlogger ("Systemlogger");//Returns or creates a new named Logger objectDatabaseLog = Logmanager.getlogger ("Databaselogger"); }        Private Static voidWritelog (ILog log, level level, Exception Exception,stringmessage) {log.        Logger.Log (declaringtype, level, message, exception); }        #regionDecorators >> system Logs Public Static voidDebug (stringMessage, Exception Exception =NULL{writelog (Systemlog, Level.debug, exception, message); }         Public Static voidInfo (stringMessage, Exception Exception =NULL{writelog (Systemlog, Level.info, exception, message); }         Public Static voidWarn (stringMessage, Exception Exception =NULL{writelog (Systemlog, Level.warn, exception, message); }         Public Static voidError (stringMessage, Exception Exception =NULL{writelog (Systemlog, Level.error, exception, message); }         Public Static voidFatal (stringMessage, Exception Exception =NULL{writelog (Systemlog, Level.fatal, exception, message); }        #endregion        #regionDecorators >> Database Logs Public Static voidDatabaseinfo (stringMessage, Exception Exception =NULL{writelog (DatabaseLog, Level.info, exception, message); }         Public Static voidDatabasewarn (stringMessage, Exception Exception =NULL{writelog (DatabaseLog, Level.warn, exception, message); }         Public Static voidDatabaseerror (stringMessage, Exception Exception =NULL{writelog (DatabaseLog, Level.error, exception, message); }        #endregion    }}

Log4net Components Easy Package

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.