写日志 写SQL语句执行日志
2010-09-19 14:21 yuejianjun 阅读(582) 评论(1) 编辑 收藏 举报///
<summary>
/// 写日志1
/// </summary>
public void
WriteGuidToLog()
{
ReaderWriterLock m_boolLogLock = new ReaderWriterLock();
string strLog = Guid.NewGuid().ToString();//GUID
string _strLogFileDir = Server.MapPath("~/log/");
string _strLogFileSubDir = "t/";
string getLogFileName = _strLogFileDir +
_strLogFileSubDir +DateTime .Now .ToString ("yyyy-MM-dd")+"log.log";
string _strRowSep = "\r\n";
try
{
if (m_boolLogLock != null)
{
m_boolLogLock.AcquireWriterLock(Timeout.Infinite);
if (!Directory.Exists(_strLogFileDir
+ _strLogFileSubDir))
{
Directory.CreateDirectory(_strLogFileDir +
_strLogFileSubDir);
}
if (File.Exists(getLogFileName))
{
File.AppendAllText(getLogFileName, strLog +
_strRowSep, System.Text.Encoding.Default);
}
else
{
File.WriteAllText(getLogFileName, strLog +
_strRowSep, System.Text.Encoding.Default);
}
}
}
catch
{ }
finally
{
if (m_boolLogLock != null)
{
m_boolLogLock.ReleaseWriterLock();
}
}
}
/// <summary>
/// 写日志2----写SQL语句执行日志
/// </summary>
/// <param name="p_strSQL"></param>
/// <param name="p_strStrat"></param>
/// <param name="p_strEnd"></param>
/// <param name="p_strSpace"></param>
/// <returns></returns>
public bool
AddSQLLog(string p_strSQL, string p_strStrat, string
p_strEnd, string p_strSpace)
{
ReaderWriterLock m_boolLogLock = new ReaderWriterLock();
// 获取SQL存储Txt文本的路径
string strLogDiv = Server.MapPath("~/log/");
if (!Directory.Exists(strLogDiv))
{
Directory.CreateDirectory(strLogDiv);
}
//获取Txt文本的路径名称
string strFilePath = strLogDiv + DateTime.Now.ToString("yyyy-MM-dd")
+ "SQL.txt";
StreamWriter objStrWriter = null;
try
{
if (m_boolLogLock != null)
{
m_boolLogLock.AcquireWriterLock(System.Threading.Timeout.Infinite);
// 判断该日志文件是否存在
if (!File.Exists(strFilePath))
{
objStrWriter = File.CreateText(strFilePath);
}
else
{
objStrWriter = File.AppendText(strFilePath);
}
// 写入当前日志信息
objStrWriter.WriteLine("<Log>");
objStrWriter.WriteLine("
<SQL>" + p_strSQL.Replace("<",
"《").Replace(">", "》") + "</SQL>");
objStrWriter.WriteLine("
<Start>" + p_strStrat + "</Start>");
objStrWriter.WriteLine("
<End>" + p_strEnd + "</End>");
objStrWriter.WriteLine("
<Space>" + p_strSpace + "</Space>");
objStrWriter.WriteLine("</Log>");
objStrWriter.WriteLine("\r\n\r\n\r\n");
}
}
catch
{
}
finally
{
if (m_boolLogLock != null)
{
m_boolLogLock.ReleaseWriterLock();
if (objStrWriter != null)
{
objStrWriter.Close();
}
}
}
return true;
}
/// <summary>
/// 写日志--写访问记录
/// </summary>
/// <param name="p_hasLog"></param>
public void writeLog(Hashtable p_hasLog)
{
_strColSep = Chr(26);
_strLogFileDir = ConfigurationManager.AppSettings["UserLogFileDir"].ToString();
_strLogFileSubDir = DateTime.Now.ToString("yyyyMM");
if (PublicMethods.GetCookie("sys", "loguid") == string.Empty)
{
_strGuid = Guid.NewGuid().ToString();
_bolIsNew = true;
//记录下是否是新用户
PublicMethods.InsertCookie("sys", "isNew", "1", 3650);
}
else
{
_strGuid = PublicMethods.GetCookie("Sys", "loguid");
}
PublicMethods.InsertCookie("sys", "loguid", _strGuid, 3650);
//组合日志内容
string strLog = string.Empty;
//日志第一字段为用户唯一标示
strLog += _strGuid;
//获取服务器变量
//referer
strLog += _strColSep + getServerVar("HTTP_REFERER");
//url
strLog += _strColSep + getServerVar("URL");
//querystring
strLog += _strColSep + getServerVar("QUERY_STRING");
//InnerIP
strLog += _strColSep + InIP;
//IP
strLog += _strColSep + UserIP;
//程序变量
//area
strLog += _strColSep + getQueryString("area");
//resumeid
strLog += _strColSep + PublicMethods.GetCookie("ResumeInfo", "ResumeID");
//type
strLog += _strColSep + getHashTableLog(p_hasLog, "type");
//是不是新用户
strLog += _strColSep + (_bolIsNew ? "1" : "0");
//时间
strLog += _strColSep + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//keyid
if (getServerVar("URL") == "/fin/company/index.aspx")
{//最热公司ID
strLog += _strColSep + getQueryString("coid");
}
else if (getServerVar("URL") == "/fin/company/cp.aspx")
{//最热点评ID
strLog += _strColSep + getQueryString("cpid");
}
else if (getServerVar("URL") == "/fin/news/newsinfo.aspx")
{//最热新闻ID
strLog += _strColSep + getQueryString("newsid");
}
else if (getServerVar("URL") == "/fin/Job/JobTitleUrl.aspx")
{//点职位标题
strLog += _strColSep + getQueryString("url");
}
else if (getServerVar("URL") == "/fin/Job/JobUrl.aspx")
{//点职位公司名
strLog += _strColSep + getQueryString("url");
}
else
{
strLog += _strColSep + string.Empty;
}
//马甲
strLog += _strColSep + PublicMethods.GetCookie("ResumeInfo", "Majia");
//预留字段1
strLog += _strColSep + getServerVar("Http_User_Agent");
//预留字段2
strLog += _strColSep + "";
//写日志
try
{
if (m_boolLogLock != null)
{
m_boolLogLock.AcquireWriterLock(Timeout.Infinite);
if (!Directory.Exists(_strLogFileDir + _strLogFileSubDir))
{
Directory.CreateDirectory(_strLogFileDir + _strLogFileSubDir);
}
if (File.Exists(getLogFileName()))
{
File.AppendAllText(getLogFileName(), strLog + _strRowSep, System.Text.Encoding.Default);
}
else
{
File.WriteAllText(getLogFileName(), strLog + _strRowSep, System.Text.Encoding.Default);
}
}
}
catch
{
}
finally
{
if (m_boolLogLock != null)
{
m_boolLogLock.ReleaseWriterLock();
}
}
}