using System;
using System.IO;
using System.Xml;
namespace OS.Spin.Common
{
///
/// 日志记录类(记录到文本文件中)
///
public static class LogisTrac
{
private static readonly string LOG_DIR = "Log";
private static readonly string LOG_FILE = string.Format("{0}\\log{1}.txt", LOG_DIR, System.DateTime.Now.ToString("yyyy-MM-dd"));
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);
}
public static void WriteInfoLog(string sInfo)
{
m_log.Info(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("--------------------------------------[本次异常结束]--------------------------------------\r\n");
}
///
/// 配置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 pRollingFileAppenderNode = myElement.SelectSingleNode("descendant::appender[@name='RollingFileAppender']/file");
// Create an attribute collection from the element.
XmlAttributeCollection attrColl = pRollingFileAppenderNode.Attributes;
attrColl[1].Value = sLogFile;
log4net.Config.XmlConfigurator.Configure(myElement);
}
}
}