You can use databases, text files, and xml files to record application operation logs. Here I will introduce how to use XML files to record operation logs. You can use databases, text files, and xml files to record application operation logs. Here I will introduce how to use XML files to record operation logs.
I think using XML to record operation logs has the following advantages:
1. you can delete historical operation logs without occupying the database space.
2. DataTable can read XML files, and DataTable can also be saved as XML files conveniently.
3. easy to view logs. you can directly open the XML file, read the DataTable, and view the logs through the program.
The following describes how to use an XML file to record operation logs in VS2005:
1. create a dataset: JobLogDataSet. xsd
The following fields are available: TraceLevel (log type), User (User), DateTime (operation time), Module (Module), Function (Function), and Message (Message.
If you do not need to add more, TraceLevel (log type) indicates Info, Warning, Error, Trance, and Off.
2. create a log type
////// Log type ///Public enum LogType {////// Information ///Info ,////// Warning ///Warning ,////// Error ///Error ,////// Trace ///Trace ,////// No logs recorded ///Off}
2. log writing method
////// Write logs //////Log type (Info, Warning, Error, Trance, Off)///User///Module///Function///MessagePublic static void WriteLog (LogType logType, string user, string module, string function, string message) {try {// The type is LogType. if (logType = LogType. off) return;
JobLogDataSet. JobLogDataTable t = new JobLogDataSet. JobLogDataTable ();
// One log file (. the log file name is called: JobLog yyyy-MM-dd.xml string jobLogFile = AppDomain. currentDomain. baseDirectory + "JobLog" + DateTime. today. toString ("yyyy-MM-dd") + ". xml "; if (! File. Exists (jobLogFile) t. WriteXml (jobLogFile );
// Read the log t. ReadXml (jobLogFile) from the. XML file );
// Add a log JobLogDataSet. jobLogRow r = t. newJobLogRow (); r. traceLevel = logType. toString (); r. user = user; r. datetime = DateTime. now; r. module = module; r. function = function; r. message = message; t. addJobLogRow (r );
// Save the log to the XML file t. WriteXml (jobLogFile);} catch (Exception ){}}
3. log reading method
////// Read logs //////
Returns the DataTable for reading logs.
Public static JobLogDataSet. jobLogDataTable ReadLog () {JobLogDataSet. jobLogDataTable jobLogDataTable = new JobLogDataSet. jobLogDataTable (); try {// Obtain all log files from the application folder JobLog *. xml string [] jobLogFiles = Directory. getFiles (AppDomain. currentDomain. baseDirectory, "JobLog *. xml ", SearchOption. topDirectoryOnly );
// Read each log record to the log DataTable foreach (string jobLogFile in jobLogFiles) {if (File. exists (jobLogFile) {// read all log files to the temporary DataTable JobLogDataSet. jobLogDataTable t = new JobLogDataSet. jobLogDataTable (); t. readXml (jobLogFile); // Import log records to the primary log DataTable foreach (JobLogDataSet. jobLogRow r in t) jobLogDataTable. importRow (r) ;}}// return the read log DataTable return jobLogDataTable;} catch (Exception) {return jobLogDataTable ;}}
4. directly call the WriteLog method where logs need to be written.
The above is the content of Operation logs recorded using XML files. For more information, see PHP Chinese network (www.php1.cn )!