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