Custom log Log

Source: Internet
Author: User


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 (); }    }}


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

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: 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.