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.
Joblogdataset.jpg(7.55 KB)
2. Create the log type the code: run the code! ">
General browsing copy code Print Code
- /// <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
- /// </Summary>
- Off
- }
/// <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
/// </Summary>
Off
}
2. Log writing method the code: General browse copy code Print Code
- /// <Summary>
- /// Write logs
- /// </Summary>
- /// <Param name = "tracelevel"> Log 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
- {
- // Do not record logs whose type is logtype. Off
- If (logtype = logtype. Off)
- Return;
- Joblogdataset. joblogdatatable T = new joblogdataset. 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 the log to the XML file
- T. writexml (joblogfile );
- }
- Catch (exception)
- {}
- }
/// <Summary>
/// Write logs
/// </Summary>
/// <Param name = "tracelevel"> Log 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
{
// Do not record logs whose type is logtype. Off
If (logtype = logtype. Off)
Return;
Joblogdataset. joblogdatatable T = new joblogdataset. 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 the log to the XML file
T. writexml (joblogfile );
}
Catch (exception)
{}
}
3. Log reading method the code: General browse copy code Print Code
- /// <Summary>
- /// Read logs
- /// </Summary>
- /// <Returns> return the able for reading logs </returns>
- Public static joblogdataset. joblogdatatable readlog ()
- {
- Joblogdataset. joblogdatatable = new joblogdataset. joblogdatatable ();
- Try
- {
- // Obtain all 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 );
- }
- }
- // Return the read log datatable
- Return joblogdatatable;
- }
- Catch (exception)
- {
- Return joblogdatatable;
- }
- }
/// <Summary>
/// Read logs
/// </Summary>
/// <Returns> return the able for reading logs </returns>
Public static joblogdataset. joblogdatatable readlog ()
{
Joblogdataset. joblogdatatable = new joblogdataset. joblogdatatable ();
Try
{
// Obtain all 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 );
}
}
// Return the read log datatable
Return joblogdatatable;
}
Catch (exception)
{
Return joblogdatatable;
}
}
4. directly call the writelog method where logs need to be written.