?? log.cs
字號(hào):
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
/*
* Copyright by DigitalSupport
* summary: This is used for Network Diagnosis Project
* version: 1.0.0
* author: Zhang Haoting @ 2006.12.11
* email: zht_china@126.com
*
*/
namespace DigitalSupport
{
/// <summary>
/// This class is desigen for logging error when meet a exception
/// </summary>
public class Log
{
#region Private Members
/// <summary>
/// Log File Path
/// </summary>
private string logFile = null;
/// <summary>
/// Operation time
/// </summary>
private string strTime = null;
/// <summary>
/// Module description
/// </summary>
private string strModule = null;
/// <summary>
/// Error description
/// </summary>
private string strDescription = null;
#endregion
#region Public Properties
/// <summary>
/// Error description
/// </summary>
public string Description
{
get { return this.strDescription; }
set { this.strDescription = value; }
}
/// <summary>
/// Module location
/// </summary>
public string Module
{
get { return this.strModule; }
set { this.strModule = value; }
}
#endregion
/// <summary>
/// Default Constructor
/// </summary>
public Log()
{
this.logFile = ".//log.xml";
this.strTime = DateTime.Now.ToString();
}
/*public Log(string strDescription)
{
this.logFile = ".//log.xml";
this.strTime = DateTime.Now.ToString();
this.strDescription = strDescription;
}*/
/// <summary>
/// Constructor with exception description and module location
/// </summary>
/// <param name="strDescription">Exception description</param>
/// <param name="strModule">Module location</param>
public Log(string strDescription, string strModule)
{
this.logFile = ".//log.xml";
this.strTime = DateTime.Now.ToString();
this.strDescription = strDescription;
this.strModule = strModule;
}
/// <summary>
/// Write the error information to the log file
/// </summary>
public void WriteLog()
{
//this.logFile = "E://a.xml";
try
{
XmlDocument doc = new XmlDocument();
//first check log file is existing
FileInfo f = new FileInfo(logFile);
if (!f.Exists)
{
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", ""));
doc.AppendChild(doc.CreateElement("Logs"));
doc.Save(this.logFile);
}
doc.Load(this.logFile);
XmlElement newLog = doc.CreateElement("Log");
XmlElement newTime = doc.CreateElement("Time");
newTime.InnerText = this.strTime;
newLog.AppendChild(newTime);
XmlElement newModule = doc.CreateElement("Module");
newModule.InnerText = this.strModule;
newLog.AppendChild(newModule);
XmlElement newDescription = doc.CreateElement("Description");
newDescription.InnerText = this.strDescription;
newLog.AppendChild(newDescription);
doc.DocumentElement.AppendChild(newLog);
doc.Save(this.logFile);
}
catch (Exception ex)
{
// when cannot logging error
// it may need some other method(log tise error in the system log file) -
// to log this error when cannot logging previous error
// :))
Console.WriteLine("Unable to Log Error: " + ex.Message); ;
}
finally
{
}
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -