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