Use XML files to record operation logs

Source: Internet
Author: User
Record applications Program Operation logs can use databases, text files, XML files, and so on. 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

///   <Summary>
/// Log Type
///   </Summary>
Public   Enum Logtype
{
///   <Summary>
/// Information
///   </Summary>
Info,
///   <Summary>
/// Warning
///   </Summary>
Warning,
///   <Summary>
/// Error
///   </Summary>
Error,
///   <Summary>
/// Tracking
///   </Summary>
Trace,
///   <Summary>
/// No logs recorded
///   </Summary>
Off
}

2. Log Writing Method ///   <Summary>
/// Write logs
///   </Summary>
///   <Param name = "tracelevel"> logs Type (Info, warning, error, trance, off) </Param>
///   <Param name = "user"> User </Param>
///   <Param name = "module"> Module </Param>
///   <Param name = "function"> Function </Param>
///   <Param name = "message"> Message </Param>
Public   Static   Void Writelog (logtype, String User, String Module, String Function, String Message)
{
Try
{
// Non-recorded logs whose type is logtype. Off
If (Logtype = Logtype. Off)
Return ;

Joblogdataset. joblogdatatable t= NewJoblogdataset. joblogdatatable ();

// One log file (. xml file) every day, 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 logs from the. xml file
T. readxml (joblogfile );

// 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 to log to XML file
T. writexml (joblogfile );
}
Catch(Exception)
{}
}

3. Log reading Method ///   <Summary>
/// Read logs
///   </Summary>
///   <Returns> Returns the datatable for reading logs. </Returns>
Public   Static Joblogdataset. joblogdatatable readlog ()
{
Joblogdataset. joblogdatatable =   New Joblogdataset. joblogdatatable ();
Try
{
// Obtain all the log files joblog *. XML from the application folder.
String [] Joblogfiles = Directory. getfiles (
Appdomain. currentdomain. basedirectory, " Joblog *. xml " , Searchoption. topdirectoryonly );

// Read each log record to the log able.
Foreach ( String Joblogfile In Joblogfiles)
{
If (File. exists (joblogfile ))
{
// Read all log files to the temporary able
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 );
}
}
// Returns the read log datatable.
Return Joblogdatatable;
}
Catch (Exception)
{
Return Joblogdatatable;
}
}

4. directly call the writelog method where logs need to be written.

Address: http://www.cnblogs.com/anjou/archive/2007/04/10/705986.html

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.