配置文件log4net_config.xml中的內容如下:
該配置文件可以用於往文本文件及sql server數據庫中記錄日志(啟用哪一個由下面的配置決定):
日志類封裝:
////// 日志記錄類(記錄到數據庫) /// public static class LogisTracToSqlDB { private static readonly log4net.ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private const string LOG4NET_CONFIG = log4net_config.xml; static LogisTracToSqlDB() { try { ConfigureLoad(); } catch { } } ////// 輸出日志 /// /// public static void WriteLog(string sInfo) { m_log.Error(sInfo); } ////// 記錄debug信息 /// /// public static void WriteLog(Exception e) { WriteLog(e.ToString()); //WriteLog(--------------------------------------[本次異常開始]--------------------------------------); //WriteLog(Message : + e.Message); //WriteLog(Source : + e.Source); //WriteLog(StackTrace : + e.StackTrace); //WriteLog(TargetSite : + e.TargetSite); //WriteLog(--------------------------------------[本次異常結束]-------------------------------------- ); } ////// 配置log4net環境 /// private static void ConfigureLoad() { XmlDocument doc = new XmlDocument(); //使用當前dll路徑 string sPath = FilesOperate.GetAssemblyPath(); if (!sPath.EndsWith(\)) { sPath += \; } sPath += LOG4NET_CONFIG; doc.Load(@sPath); XmlElement myElement = doc.DocumentElement; log4net.Config.XmlConfigurator.Configure(myElement); } }
///
/// 日志記錄類(記錄到文本文件中)
///
public static class LogisTrac
{
private static readonly string LOG_DIR = 日志;
private static readonly string LOG_FILE = LOG_DIR + \log + System.DateTime.Now.ToString(yyyy-MM-dd) + .txt;
private const string LOG4NET_CONFIG = log4net_config.xml;
private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(typeof(LogisTrac));
static LogisTrac()
{
try
{
ConfigureLoad();
}
catch { }
}
///
/// 返回ILog接口
///
private static log4net.ILog Log
{
get
{
return m_log;
}
}
///
/// 輸出日志
///
///
public static void WriteLog(string sInfo)
{
m_log.Error(sInfo);
}
///
/// 記錄debug信息
///
///
public static void WriteLog(Exception e)
{
WriteLog(--------------------------------------[本次異常開始]--------------------------------------);
WriteLog(Message : + e.Message);
WriteLog(Source : + e.Source);
WriteLog(StackTrace : + e.StackTrace);
WriteLog(TargetSite : + e.TargetSite);
WriteLog(--------------------------------------[本次異常結束]--------------------------------------
);
}
///
/// 配置log4net環境
///
private static void ConfigureLoad()
{
XmlDocument doc = new XmlDocument();
//使用當前dll路徑
string sPath = FilesOperate.GetAssemblyPath();
if (!sPath.EndsWith(\))
{
sPath += \;
}
//查看Log文件夾是否存在,如果不存在,則創建
string sLogDir = sPath + LOG_DIR;
if (!Directory.Exists(sLogDir))
{
Directory.CreateDirectory(sLogDir);
}
string sLogFile = sPath + LOG_FILE;
sPath += LOG4NET_CONFIG;
doc.Load(@sPath);
XmlElement myElement = doc.DocumentElement;
//修改log.txt的路徑
XmlNode pLogFileAppenderNode = myElement.SelectSingleNode(descendant::appender[@name='LogFileAppender']/file);
// Create an attribute collection from the element.
XmlAttributeCollection attrColl = pLogFileAppenderNode.Attributes;
attrColl[0].Value = sLogFile;
log4net.Config.XmlConfigurator.Configure(myElement);
}
}
///
/// 文件 操作
///
public static class FilesOperate
{
///
/// 獲取App的當前路徑 \結束
///
///
public static string getAppPath()
{
return GetAssemblyPath();
}
///
/// 獲取Assembly的運行路徑 \結束
///
///
public static string GetAssemblyPath()
{
string sCodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
sCodeBase = sCodeBase.Substring(8, sCodeBase.Length - 8); // 8是 file:// 的長度
string[] arrSection = sCodeBase.Split(new char[] { '/' });
string sDirPath = ;
for (int i = 0; i < arrSection.Length - 1; i++)
{
sDirPath += arrSection[i] + Path.DirectorySeparatorChar;
}
return sDirPath;
}
///
/// 文件夾復制
///
///原始路徑
///目標路徑
///
public static bool CopyDirectory(string sSourceDirName, string sDestDirName)
{
if (string.IsNullOrEmpty(sSourceDirName) || string.IsNullOrEmpty(sDestDirName))
{
return false;
}
//不復制.svn文件夾
if (sSourceDirName.EndsWith(svn))
{
return true;
}
if (sSourceDirName.Substring(sSourceDirName.Length - 1) != Path.DirectorySeparatorChar.ToString())
{
sSourceDirName = sSourceDirName + Path.DirectorySeparatorChar;
}
if (sDestDirName.Substring(sDestDirName.Length - 1) != Path.DirectorySeparatorChar.ToString())
{
sDestDirName = sDestDirName + Path.DirectorySeparatorChar;
}
#region 復制函數
if (Directory.Exists(sSourceDirName))
{
if (!Directory.Exists(sDestDirName))
{
Directory.CreateDirectory(sDestDirName);
}
foreach (string item in Directory.GetFiles(sSourceDirName))
{
File.Copy(item, sDestDirName + System.IO.Path.GetFileName(item), true);
}
foreach (string item in Directory.GetDirectories(sSourceDirName))
{
CopyDirectory(item, sDestDirName + item.Substring(item.LastIndexOf(Path.DirectorySeparatorChar) + 1));
}
}
return true;
#endregion
}
///
/// 啟動其他的應用程序
///
///應用程序名稱
///應用程序工作目錄
///命令行參數
///窗口風格
public static bool StartProcess(string file, string workdirectory, string args, ProcessWindowStyle style)
{
try
{
Process pMyProcess = new Process();
ProcessStartInfo pStartInfo = new ProcessStartInfo(file, args);
pStartInfo.WindowStyle = style;
pStartInfo.WorkingDirectory = workdirectory;
pMyProcess.StartInfo = pStartInfo;
pMyProcess.StartInfo.UseShellExecute = false;
pMyProcess.Start();
return true;
}
catch (Exception ex)
{
//LogAPI.debug(ex);
return false;
}
}
///
/// 獲得本地計算機名
///
///
public static string GetComputerName()
{
return Dns.GetHostName();
}
///
/// 獲得計算機IP地址
///
///
public static string GetIPAddress()
{
try
{
string sComputerName;
sComputerName = GetComputerName();
string sIpAddress = ;
IPAddress[] addr = Dns.GetHostAddresses(sComputerName);
//for (int i = 0; i < addr.Length; i++)
//{
// sIpAddress += addr[i].ToString() + ;
//}
sIpAddress = addr[0].ToString();
return sIpAddress;
}
catch (Exception ep)
{
//LogAPI.debug(ep);
return 127.0.0.1;
}
}
///
/// 描述:創建目錄
///
///
public static bool CreateFolder(string sFolder)
{
//如果臨時文件夾不存在,則創建該文件夾
if (!Directory.Exists(sFolder))
{
Directory.CreateDirectory(sFolder);
}
return true;
}
}