C #-use XML files to record operation logs

Source: Internet
Author: User
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
  1. /// <Summary>
  2. /// Log Type
  3. /// </Summary>
  4. Public Enum logtype
  5. {
  6. /// <Summary>
  7. /// Information
  8. /// </Summary>
  9. Info,
  10. /// <Summary>
  11. /// Warning
  12. /// </Summary>
  13. Warning,
  14. /// <Summary>
  15. /// Error
  16. /// </Summary>
  17. Error,
  18. /// <Summary>
  19. /// Tracking
  20. /// </Summary>
  21. Trace,
  22. /// <Summary>
  23. /// No logs
  24. /// </Summary>
  25. Off
  26. }
 
/// <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

  1. /// <Summary>
  2. /// Write logs
  3. /// </Summary>
  4. /// <Param name = "tracelevel"> Log Type (Info, warning, error, trance, off) </param>
  5. /// <Param name = "user"> User </param>
  6. /// <Param name = "module"> module </param>
  7. /// <Param name = "function"> function </param>
  8. /// <Param name = "message"> message </param>
  9. Public static void writelog (logtype, string user, string module, string function, string message)
  10. {
  11. Try
  12. {
  13. // Do not record logs whose type is logtype. Off
  14. If (logtype = logtype. Off)
  15. Return;
  16. Joblogdataset. joblogdatatable T = new joblogdataset. joblogdatatable ();
  17. // One log file (. xml file) every day, the log file name is called: joblog yyyy-MM-dd.xml
  18. String joblogfile = appdomain. currentdomain. basedirectory + "joblog" +
  19. Datetime. Today. tostring ("yyyy-mm-dd") + ". xml ";
  20. If (! File. exists (joblogfile ))
  21. T. writexml (joblogfile );
  22. // Read logs from the. xml file
  23. T. readxml (joblogfile );
  24. // Add a log
  25. Joblogdataset. joblogrow r = T. newjoblogrow ();
  26. R. tracelevel = logtype. tostring ();
  27. R. User = user;
  28. R. datetime = datetime. now;
  29. R. module = module;
  30. R. Function = function;
  31. R. Message = message;
  32. T. addjoblogrow (R );
  33. // Save the log to the XML file
  34. T. writexml (joblogfile );
  35. }
  36. Catch (exception)
  37. {}
  38. }
 
/// <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

  1. /// <Summary>
  2. /// Read logs
  3. /// </Summary>
  4. /// <Returns> return the able for reading logs </returns>
  5. Public static joblogdataset. joblogdatatable readlog ()
  6. {
  7. Joblogdataset. joblogdatatable = new joblogdataset. joblogdatatable ();
  8. Try
  9. {
  10. // Obtain all log files joblog *. XML from the application folder
  11. String [] joblogfiles = directory. getfiles (
  12. Appdomain. currentdomain. basedirectory, "joblog *. xml", searchoption. topdirectoryonly );
  13. // Read each log record to the log able
  14. Foreach (string joblogfile in joblogfiles)
  15. {
  16. If (file. exists (joblogfile ))
  17. {
  18. // Read all log files to the temporary able
  19. Joblogdataset. joblogdatatable T = new joblogdataset. joblogdatatable ();
  20. T. readxml (joblogfile );
  21. // Import log records to the primary log datatable
  22. Foreach (joblogdataset. joblogrow R in T)
  23. Joblogdatatable. importrow (R );
  24. }
  25. }
  26. // Return the read log datatable
  27. Return joblogdatatable;
  28. }
  29. Catch (exception)
  30. {
  31. Return joblogdatatable;
  32. }
  33. }
/// <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.

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.