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