Log.cs
usingSystem;usingSystem.Collections.Generic;usingsystem.web;usingSystem.IO;namespacepc.common{ Public classLog {//Create a log directory under the Web site root directory Public Static stringPath = HttpContext.Current.Request.PhysicalApplicationPath +"logs"; /** * Write debug information to log file * @param className class name * @param content Write contents*/ Public Static voidDebug (stringClassName,stringcontent) { if(Loglevel.log_levenl >=3) {Writelog ("DEBUG", className, content); } } /** * Write run-time information to log file * @param className class name * @param content writes*/ Public Static voidInfo (stringClassName,stringcontent) { if(Loglevel.log_levenl >=2) {Writelog ("INFO", className, content); } } /** * Write error message to log file * @param className class name * @param content Write contents*/ Public Static voidError (stringClassName,stringcontent) { if(Loglevel.log_levenl >=1) {Writelog ("ERROR", className, content); } } /** * Actual write Log operation * @param type log record type * @param className class name * @param content Write contents*/ protected Static voidWritelog (stringTypestringClassName,stringcontent) { if(! Directory.Exists (PATH))//If the log directory does not exist, create{directory.createdirectory (path); } stringTime = DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss.fff");//get current system time stringfilename = path +"/"+ DateTime.Now.ToString ("YYYY-MM-DD") +". Log";//Name the log file by date//Create or open a log file to append records to the end of the log fileStreamWriter MYSW =file.appendtext (filename); //writing to a log file stringWrite_content = time +" "+ Type +" "+ ClassName +": "+content; Mysw.writeline (write_content); //Close log fileMysw.close (); } }}
LogLevel.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespacepc.common{ Public classLogLevel { Public Static stringAppKey (stringkey) { returnSystem.configuration.configurationmanager.appsettings[key]; } /// <summary> ///log level, 0. Do not output the log; 1. Output error messages only; 2. Output error and normal information; 3. Output error messages, normal information, and debug information/// </summary> Public Static intLOG_LEVENL {Get { stringLOG_LEVENL ="0"; if(AppKey ("Log_leven") !="") {log_levenl= AppKey ("Log_leven"); } returnConvert.ToInt32 (LOG_LEVENL); } } }}
Web. config
Invocation mode
Log.debug (this. GetType (). ToString (), "JSON:" + JSON);
Custom log Log