C # log class

Source: Internet
Author: User

The lock keyword ensures that when a thread is located in the Code critical section, the other thread does not enter the critical section. If other threads attempt to enter the locked code, it waits until the object is released.

 

Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. configuration; <br/> using system. io; <br/> namespace writelogclass {<br/> /// error message processing class /// <br/> public class error {<br/> //// // object // <br/> Public object OBJ = new object (); <br/> // object // <br/> Public static object OBJ = new object (); <br/> ///// error message in the error message /// <br/> private string errmes Sage; <br/> ///// error message in the error message // <br/> Public String errmessage <br/>{< br/> get {return errmessage ;} <br/> set {errmessage = value ;}< br/>}< br/> //// error ID /// <br/> private string errid; <br/> // error ID // <br/> Public String id <br/>{< br/> get {return errid ;} <br/> set {errid = value ;}< br/>}< br/> // error message /// <br/> private string message; <br/> // error message // <br/> Public String message <br/>{< br/> get {return message ;}< br/> set {message = value ;} <br/>}< br/> ///// class name // <br/> private string classname; <br/> // class name // <br/> Public String classname <br/>{< br/> get {return classname ;} <br/> set {classname = value ;}< br/>}< br/> /// // method name /// <br/> private string functionname; <br/> // method name // <br/> Public String functionname <br />{< Br/> get {return functionname ;}< br/> set {functionname = value ;} <br/>}< br/> ///// error source // <br/> private string datasource; <br/> // error source // <br/> Public String source <br/>{< br/> get {return datasource ;} <br/> set {datasource = value ;} <br/>}< br/> # region writes error information to the log // write error information to the log // <br/> Public void writelog () <br/>{< br/> lock (this. OBJ) {<br/> string Path = ""; <Br/> try {<br/> Path = system. configuration. configurationmanager. appsettings ["logpath"]. tostring (); <br/>}< br/> catch (exception e) <br/> {Path = "/log"; this. errmessage = E. message;} string P = system. appdomain. currentdomain. setupinformation. applicationbase + "//" + path + "//" + datetime. now. tostring ("yyyy-mm-dd") + ". TXT "; <br/> Path = system. appdomain. currentdomain. setupinformation. appli Cationbase + "//" + path; <br/> If (! Directory. exists (PATH) <br/>{< br/> directoryinfo dir = directory. createdirectory (PATH); Dir. refresh (); <br/>}< br/> If (! File. exists (p) <br/> {filestream FSO = file. create (p); FSO. close (); <br/> FSO. dispose (); streamwriter Sw = file. appendtext (p); <br/> SW. writeline ("========================================== ========================================================== ============================== "); SW. writeline ("log files on this site"); <br/> SW. writeline ("========================================== ========================================================== ============================== "); <Br/> SW. writeline (""); <br/> SW. flush (); <br/> SW. close (); <br/> SW. dispose (); <br/>}< br/> streamwriter Sw = file. appendtext (p); <br/> SW. writeline (""); <br/> SW. writeline ("========================================== ========================================================== ============================== "); <br/> SW. writeline (""); <br/> SW. writeline ("[" + datetime. now. tostring ("yyyy-mm-dd hh: mm: SS") + "]"); <br/> SW. writeline ("Error Data source:" + this. source); Sw. writeline ("error method:"); <br/> SW. writeline ("error method class:" + this. classname); Sw. writeline ("error method:" + this. functionname); <br/> try {<br/> SW. writeline ("current IP:" + Xiaofeng. function. getip4address () + ""); <br/> SW. writeline ("current error page address:" + system. web. httpcontext. current. request. URL. tostring ();} catch (exception ex) {}< br/> SW. writeline ("current error message:" + this. message. tostring (); <br/> SW. flush (); <br/> SW. close (); <br/> SW. dispose (); </P> <p >}</P> <p >}//// write error logs //// exception class <br/> Public static void writelog (exception E) {lock (OBJ) {<br/> string Path = ""; <br/> try {Path = system. configuration. configurationmanager. appsettings ["logpath"]. tostring (); <br/>}< br/> catch (exception e) <br/> {Path = "/log" ;}< br/> string P = system. appdomain. currentdomain. setupinf Ormation. applicationbase + "//" + path + "//" + datetime. now. tostring ("yyyy-mm-dd") + ". TXT "; <br/> Path = system. appdomain. currentdomain. setupinformation. applicationbase + "//" + path; <br/> If (! Directory. exists (PATH) {directoryinfo dir = directory. createdirectory (PATH); <br/> dir. Refresh (); <br/>}< br/> If (! File. exists (p) <br/>{< br/> filestream FSO = file. create (p); <br/> FSO. close (); <br/> FSO. dispose (); <br/> streamwriter Sw = file. appendtext (p); Sw. writeline ("========================================== ========================================================== ============================== "); <br/> SW. writeline ("log files on this site"); <br/> SW. writeline ("========================================== ========================================================== ============================== "); <br/> SW. writeline (""); <br/> SW. flush (); <br/> SW. close (); <br/> SW. dispose (); <br/>}< br/> streamwriter Sw = file. appendtext (p); Sw. writeline (""); <br/> SW. writeline ("========================================== ========================================================== ============================== "); <br/> SW. writeline (""); Sw. writeline ("[" + datetime. now. tostring ("yyyy-mm-dd hh: mm: SS") + "]"); Sw. writeline ("Error Data source:" + E. source); <br/> SW. writeline ("error method class:" + E. targetsite. declaringtype. name. tostring (); <br/> SW. writeline ("error method:" + E. targetsite. name. tostring (); <br/> try {SW. writeline ("current IP:" + Xiaofeng. function. getip4address () + ""); <br/> SW. writeline ("current error page address:" + system. web. httpcontext. current. request. URL. tostring ();} catch (exception ex) {}< br/> SW. writeline ("current error message:" + E. message. tostring (); <br/> SW. flush (); <br/> SW. close (); <br/> SW. dispose (); <br/>}< br/> # endregion} another simple log Writing Method ------------------------------------ // record the log information private void writelog (string info) <br/>{< br/> try {<br/> string Path = pathbase + "log" + datetime. now. tolongdatestring () + ". TXT "; // if it does not exist, create a new file. If it exists, add it directly to system. io. streamwriter RW = new system. io. streamwriter (path, true); RW. writeline (Info + "---------" + datetime. now. tostring (); // Add the time tag RW. close ();} catch {//} -------------------------------- another simple log Writing Method ------------------------------------------ public static class exphelper {private static object loglock = new object (); public static string fileconsolepath = application. startuppath + "// TXT // cos _" + datetime. now. tostring (). replace (':','. ') + ". TXT "; public static string filelogpath = application. startuppath + "// TXT // log _" + datetime. now. tostring (). replace (':','. ') + ". TXT "; public static void cos (string text) {file. appendalltext (fileconsolepath, datetime. now. tostring () + "" + TEXT + environment. newline);} public static void log (string text) {lock (loglock) file. appendalltext (filelogpath, datetime. now. tostring () + "" + TEXT + environment. newline) ;}-------------------------------------------------------------------- public static class writelogclass {///// object /// private static object loglock = new object (); ///// application startup path // Private Static string pathbase = system. appdomain. currentdomain. basedirectory; ////// write log information ///// public static void writelog (string loginfo) {lock (loglock) {string Path = pathbase + "log // log _" + datetime. now. tolongdatestring () + ". TXT "; // string path_add = application. startuppath + "// log _" + datetime. now. tolongdatestring () + ". TXT "; // file. appendalltext (path_add, datetime. now. tostring () + "" + loginfo + environment. newline); system. io. streamwriter Sw = new system. io. streamwriter (path, true); // create a file if it does not exist. If it exists, add try {SW directly. writeline (datetime. now. tostring () + "---------" + loginfo); // Add a time tag} catch {} SW. flush (); </P> <p> SW. close (); Sw. dispose () ;}}</P> <p>

 

 

 

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.