Xml
Logging an application's action log can use databases, text files, XML files, and so on. What I'm introducing here is using an XML file to record an action log.
I think there are several benefits to using XML to log operations logs:
1. Do not occupy the space of the database, you can delete the history operation log arbitrarily.
2. DataTable can be read into the XML file, the DataTable can also be easily saved as an XML file.
3. Easy to view the log, you can directly open the XML file view, you can read into the DataTable, and then through the program view.
Use the XML file to record the action log method in VS2005 as follows:
1. Set up data set: joblogdataset.xsd
This includes: TraceLevel (log type), user (user), DateTime (Operation time), module (module), function (function), message (Messages) 6 fields.
Not enough to add it, where TraceLevel (log type) refers to Info,warning,error,trance,off.
2. Create 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>
Log not logged
</summary>
Off
}
2. Method of writing Log
<summary>
Write log
</summary>
<param name= "TraceLevel" > Log type (info,warning,error,trance,off) </param>
<param name= "User" > Users </param>
<param name= "module" > Modules </param>
<param name= "function" > Functions </param>
<param name= "message" > Messages </param>
public static void Writelog (LogType logtype,string User, string module, String function, String message)
{
Try
{
Log not logged with type Logtype.off
if (LogType = = Logtype.off)
Return
Joblogdataset.joblogdatatable t = new joblogdataset.joblogdatatable ();
One log file per day (. XML file), the file name of the log 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);
From. Read Log in 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. Method of reading Log
///<summary>
///read log
///</ Summary>
///<returns> return datatable</returns> to read log;
public static joblogdataset.joblogdatatable Readlog ()
{
joblogdataset.joblogdatatable joblogdatatable = new joblogdataset.joblogdatatable ();
try
{
/From the application folder, get all log files Joblog*.xml
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 to the staging DataTable
Joblogdataset.joblogdatatable t = new joblogdataset.joblogdatatable ();
T.readxml (Joblogfile);
Import log record to primary log DataTable
foreach (Joblogdataset.joblogrow r in T)
Joblogdatatable.importrow (R);
}
}
Returns the log DataTable read
return joblogdatatable;
}
catch (Exception)
{
return joblogdatatable;
}
}
4. Where you need to write the log, call the Writelog method directly.