?? logprintthread.cs
字號:
using System;
using System.IO;
using System.Threading;
using System.Collections;
using MonitorSystem.BasicClass;
using MonitorSystem.LogFileModule;
using MonitorSystem.RuleFileProcess;
namespace MonitorSystem.MonitorInterface
{
public class LogRecord
{
/// <summary>
/// 日志隊列
/// </summary>
public static BasicQueue LogQueue = new BasicQueue();
/// <summary>
/// 平臺ID
/// </summary>
public int iPlatformID;
/// <summary>
/// 節點ID
/// </summary>
public int iNodeID;
/// <summary>
/// 監控類型ID
/// </summary>
public int iTypeID;
/// <summary>
/// 監控規則ID
/// </summary>
public int iRuleID;
/// <summary>
/// 所監控對象的具體名稱
/// </summary>
public string strRuleName;
/// <summary>
/// 監控數據
/// </summary>
public string strValue;
/// <summary>
/// 狀態 0-正常 1-一般警告 2-嚴重警告 3-一般故障 4-嚴重故障 5-未知(等待分析)
/// </summary>
public int iStatus;
/// <summary>
/// 報警方式 0-無 1-鳴叫 2-短信 3-電子郵件通知 4-等待分析
/// </summary>
public int iAction;
/// <summary>
/// 報警方式參數
/// </summary>
public string strActionParam;
/// <summary>
/// 保留1
/// </summary>
public string strReserve1;
/// <summary>
/// 保留2
/// </summary>
public string strReserve2;
/// <summary>
/// 報警方式列表
/// </summary>
public int[] iInterfaceAction;
/// <summary>
/// 報警參數列表
/// </summary>
public string[] strInActionParam;
/// <summary>
/// 報警說明
/// </summary>
public string strAlertRemark;
public string strDateTime;
public string strLogName; //文件路徑名稱
public void SetLog(
string strLogName, //文件路徑名稱
string strDateTime,
int iPlatformID, //平臺ID
int iNodeID, //節點ID
int iTypeID, //監控類型ID
int iRuleID, //監控規則ID
string strRuleName, //所監控對象的具體名稱
string strValue, //監控數據
int iStatus, //狀態 0-正常 1-一般警告 2-嚴重警告 3-一般故障 4-嚴重故障 5-未知(等待分析)
int iAction, //報警方式 0-無 1-鳴叫 2-短信 3-電子郵件通知 4-等待分析
string strActionParam, //報警方式參數
string strReserve1,
string strReserve2)
{
this.strLogName = strLogName;
this.strDateTime = strDateTime;
this.iPlatformID = iPlatformID;
this.iNodeID = iNodeID;
this.iTypeID = iTypeID;
this.iRuleID = iRuleID;
this.strRuleName = strRuleName;
this.strValue = strValue;
this.iStatus = iStatus;
this.iAction = iAction;
this.strActionParam = strActionParam;
this.strReserve1 = strReserve1;
this.strReserve2 = strReserve2;
LogQueue.Enqueue(this);
}
public void SetLog(
string strLogName, //文件路徑名稱
string strDateTime,
int iPlatformID, //平臺ID
int iNodeID, //節點ID
int iTypeID, //監控類型ID
int iRuleID, //監控規則ID
string strRuleName, //所監控對象的具體名稱
string strValue, //監控數據
int iStatus, //狀態 0-正常 1-一般警告 2-嚴重警告 3-一般故障 4-嚴重故障 5-未知(等待分析)
int[] iAction, //報警方式 0-無 1-鳴叫 2-短信 3-電子郵件通知 4-等待分析
string[] strActionParam, //報警方式參數
string strReserve1,
string strReserve2)
{
this.strLogName = strLogName;
this.strDateTime = strDateTime;
this.iPlatformID = iPlatformID;
this.iNodeID = iNodeID;
this.iTypeID = iTypeID;
this.iRuleID = iRuleID;
this.strRuleName = strRuleName;
this.strValue = strValue;
this.iStatus = iStatus;
this.iInterfaceAction = iAction;
this.strInActionParam = strActionParam;
this.strReserve1 = strReserve1;
this.strReserve2 = strReserve2;
LogQueue.Enqueue(this);
}
}
/// <summary>
/// LogPrintThread 的摘要說明。
/// </summary>
public class LogPrintThread
{
private Thread m_Thread;
private bool m_Pause;
private bool m_Exit;
public DateTime CreateTime;
public static bool m_IsUpdate = false;
public static string m_UploadFile;
private StreamWriter out_fd=null;
private SystemLog m_SysLog;
private string m_FileName;
public LogPrintThread()
{
m_Pause = false;
m_Exit = false;
CreateTime = System.DateTime.Now;
m_SysLog=new SystemLog();
}
public void Run()
{
while(true)
{
while(m_Pause)
{
try
{
Thread.Sleep(1000);
}
catch(Exception)
{
}
continue;
}
if(m_Exit)
{
break;
}
if (LogRecord.LogQueue.Count<=0)
{
try
{
Thread.Sleep(1000);
continue;
}
catch(Exception)
{
continue;
}
}
try
{
LogRecord log = new LogRecord();
log = (LogRecord)LogRecord.LogQueue.Dequeue();
InterfaceWriteToFile(log);
}
catch(Exception ex)
{
m_SysLog.WriteToSysLog(ex.Message + ex.StackTrace);
}
}
}
/// <summary>
/// 用于中心接口生成日志文件,以字段間加TABLE符號的方式寫入行,寫入監控日志文件
/// 每分鐘生成一個日志文件,不用指定文件名,文件名由程序產生
/// </summary>
public void InterfaceWriteToFile(LogRecord log)
{
try
{
if(System.DateTime.Now.Minute != CreateTime.Minute)
{
if(out_fd!=null)
{
out_fd.Close();
out_fd = null;
}
}
if(out_fd == null)
{
if (m_FileName!=null)
{
m_IsUpdate = true;
m_UploadFile = m_FileName;
}
m_FileName = string.Format("{0}_{1,4:0000}_{2}{3,2:00}{4,2:00}{5,2:00}{6,2:00}.log",
log.strLogName, log.iPlatformID,
DateTime.Now.Year,DateTime.Now.Month,
DateTime.Now.Day,DateTime.Now.Hour,DateTime.Now.Minute);
//open a file for Detail Record
FileStream fs = new FileStream(m_FileName,FileMode.Append, FileAccess.Write);
out_fd = new System.IO.StreamWriter(fs, System.Text.Encoding.GetEncoding(936));
CreateTime = System.DateTime.Now;
}
string strAction="";
for(int i=0; i<log.iInterfaceAction.Length; i++)
{
if(i>=1)
strAction+="&";
strAction+=Convert.ToString(log.iInterfaceAction[i]);
}
log.strActionParam="";
for(int i=0; i<log.strInActionParam.Length; i++)
{
if(i>=1)
log.strActionParam+="&";
log.strActionParam+=Convert.ToString(log.strInActionParam[i]);
}
string strDataLine=string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}",
log.strDateTime,log.iPlatformID,log.iNodeID,log.iTypeID,log.iRuleID,log.strRuleName,log.strValue,
log.iStatus,strAction,log.strActionParam,log.strReserve1,log.strReserve2);
out_fd.WriteLine(strDataLine);
out_fd.Flush();
}
catch(Exception e)
{
throw new Exception(e.Message + e.StackTrace);
}
}
public void Startup()
{
m_Thread = new Thread(new ThreadStart(this.Run));
// Start the thread
m_Pause = false;
m_Exit = false;
m_Thread.Start();
}
public void Join()
{
if(m_Thread != null)
{
m_Thread.Interrupt();
m_Thread.Join();
}
else
{
return;
}
}
public bool IsAlive()
{
if(m_Thread != null)
{
return m_Thread.IsAlive;
}
else
{
return false;
}
}
public void Suspend()
{
if(m_Pause == false)
m_Pause = true;
}
public void Resume()
{
if(m_Pause == true)
m_Pause = false;
}
public void SetExit()
{
if(m_Exit == false)
m_Exit = true;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -