Databases, text files, XML files, etc. can be used to record application operation logs. What I introduce here is the use of XML files to record operation logs.
I think using XML to record operation logs has the following benefits:
1. It does not occupy database space and can delete historical operation logs at will.
2. DataTable can easily read XML files, and DataTable can also be easily saved as XML files.
3. It is convenient to view the log. You can directly open the XML file to view it, or you can read it into the DataTable and then view it through the program.
The method to use XML files to record operation logs in VS2005 is as follows:
1. Create a data set: JobLogDataSet.xsd
This includes 6 fields: TraceLevel (log type), User (user), DateTime (operation time), Module (module), Function (function), and Message (message).
If it’s not enough, add it yourself. TraceLevel (log type) refers to Info, Warning, Error, Trance, and Off.
2. Create log type /// <summary>
/// Log type
/// </summary>
public enum LogType
{
/// <summary>
/// information
/// </summary>
Info,
/// <summary>
/// warn
/// </summary>
Warning,
/// <summary>
/// mistake
/// </summary>
Error,
/// <summary>
/// Tracking
/// </summary>
Trace,
/// <summary>
/// No logging
/// </summary>
Off
}
2. How to write a log
/// <summary>
/// Write log
/// </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 logType, string user, string module, string function, string message)
{
try
{
// Type LogType.Off does not record logs
if (logType == LogType.Off)
return;
JobLogDataSet.JobLogDataTable t = new JobLogDataSet.JobLogDataTable();
// One log file (.XML file) every day, the log file name is: 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 .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 log to XML file
t.WriteXml(jobLogFile);
}
catch(Exception)
{}
}
3. How to read logs
/// <summary>
/// Read log
/// </summary>
/// <returns>Returns the DataTable for reading the log</returns>
public static JobLogDataSet.JobLogDataTable ReadLog()
{
JobLogDataSet.JobLogDataTable jobLogDataTable = new JobLogDataSet.JobLogDataTable();
try
{
// Get all log files JobLog*.xml from the application folder
string[] jobLogFiles = Directory.GetFiles(
AppDomain.CurrentDomain.BaseDirectory, "JobLog*.xml", SearchOption.TopDirectoryOnly);
// Read each log record into the log DataTable
foreach (string jobLogFile in jobLogFiles)
{
if (File.Exists(jobLogFile))
{
//Read all log files into temporary DataTable
JobLogDataSet.JobLogDataTable t = new JobLogDataSet.JobLogDataTable();
t.ReadXml(jobLogFile);
//Import log records to the main log DataTable
foreach (JobLogDataSet.JobLogRow r in t)
jobLogDataTable.ImportRow(r);
}
}
// Return the read log DataTable
return jobLogDataTable;
}
catch(Exception)
{
return jobLogDataTable;
}
}
4. Wherever you need to write a log, just call the WriteLog method directly.