The small system uses write text as the log record

Source: Internet
Author: User

Working on weekdays will write some small tools, but the use of log4net is too big, feel unnecessary, so use Notepad to record some system logs

The code is as follows:

1 Private Static ObjectObjlock =New Object();//Read and write file locks2         /// <summary>3         ///Logging error Logs4         /// </summary>5         /// <param name= "Lcontent" >Error Log Contents</param>6         /// <param name= "FilePath" >error Log save file path</param>7          Public Static voidWriteerrorlog (stringFilePath,stringlcontent)8         {9             stringDirectoty = DateTime.Now.ToString ("YYYYMMDD") +"\\"+ FilePath +"\\";Ten             stringFileName =string. Empty; One  A             if(string. IsNullOrEmpty (fileName)) -             { -FileName = DateTime.Now.ToString ("YYYYMMDD") +". txt"; the             } -             Else -             { -filename = filename +". txt"; +             } -             stringContent = DateTime.Now.ToString () +"\ r \ n"+ Lcontent +"\r\n\r\n"; + writeinfotofile (Directoty, fileName, content); A         } at  -         /// <summary> -         ///logging errors to the log file -         /// </summary> -         /// <param name= "directory" >Catalogue</param> -         /// <param name= "FileName" >file name</param> in         /// <param name= "Content" >Error Content</param> -         Private Static voidWriteinfotofile (stringDirectorystringFileName,stringcontent) to         { +System.IO.FileStream fs =NULL; -System.IO.TextWriter tw =NULL; the             Try *             { $                 Lock(Objlock)Panax Notoginseng                 { -                     stringLogPath =assembly.getexecutingassembly (). Location; the                     //string LogPath =httpcontext.current.server.mappath ("~"); +LogPath = logpath.substring (0, Logpath.lastindexof ('\\'));//Delete file name; ALogPath = LogPath +@"\log\"+directory; the                     if(!System.IO.Directory.Exists (logPath)) + System.IO.Directory.CreateDirectory (logPath); -  $FS =NewSystem.IO.FileStream (LogPath +FileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite); $TW =NewSystem.IO.StreamWriter (FS); -  -Fs. Seek (0, System.IO.SeekOrigin.End); theTW =System.IO.TextWriter.Synchronized (TW); - tw. Write (content);Wuyi tw. Flush (); the tw. Close (); - FS. Close (); Wu                 } -             } About             Catch(IOException ex) $             { -                 Throwex; -             } -             finally A             { +                 if(TW! =NULL) the                 { - tw. Close (); $ tw. Dispose (); the                 } the                 if(FS! =NULL) the                 { the FS. Close (); - FS. Dispose (); in                 } the             } the}

The small system uses write text as the log record

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.