⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlloggingprovider.cs

📁 SharpNuke源代码
💻 CS
📖 第 1 页 / 共 4 页
字号:
				}
			}
			
			XmlNode xmlNode;
			xmlNode = xmlDoc.SelectSingleNode("/logs/log[@LogGUID=\'" + logInfo.LogGUID + "\']");
			
			XmlDocument xmlDocOut = new XmlDocument();
			xmlDocOut.LoadXml("<SingleLog></SingleLog>");
			XmlNode xmlNewNode;
			xmlNewNode = xmlDocOut.ImportNode(xmlNode, true);
			xmlDocOut.DocumentElement.AppendChild(xmlNewNode);
			
			int totalRecords = 0;
			if (returnType == LoggingProvider.ReturnType.XML)
			{
				return xmlDocOut.DocumentElement.SelectSingleNode("log");
			}
			else
			{
				return GetLogInfoFromXML(xmlDocOut, int.MaxValue, 1, ref totalRecords).GetItem(0);
			}
			
		}
		
		//--------------------------------------------------------------
		//Method to see if logging is enabled for a log type & portal
		//--------------------------------------------------------------
		public override bool LoggingIsEnabled(string logType, int portalID)
		{
			LogTypeConfigInfo logTypeConfigInfo;
			string configPortalID = portalID.ToString();
			if (portalID == - 1)
			{
				configPortalID = "*";
			}
			logTypeConfigInfo = GetLogTypeConfig(configPortalID, logType);
			return (logTypeConfigInfo != null && logTypeConfigInfo.LoggingIsActive);
		}
		
		public override void PurgeLogBuffer ()
		{
			try
			{
				lockPurgeLog.AcquireWriterLock(writerLockTimeout);
				LogQueueItem logQueueItem;
				ArrayList list = XMLLoggingProvider.LogQueue;
				int j = list.Count;
				for (int i = 0; i < list.Count; i++)
				{
					j --;
					//in case the log was removed
					//by another thread simultaneously
					logQueueItem = list[j] as LogQueueItem;
					if (logQueueItem != null)
					{
						WriteLog(logQueueItem);
					}
					//in case the log was removed
					//by another thread simultaneously
					if (list[j] != null)
					{
						list.RemoveAt(j);
					}
					
					//use "j" instead of "i" so we
					//can iterate in reverse, taking
					//items out of the rear of the
					//collection
				}
				
				
				XMLLoggingProvider loggingProvider = new XMLLoggingProvider();
				ArrayList arrayList = loggingProvider.GetLogTypeConfigInfo();
				int k;
				for (k = 0; k < arrayList.Count ; k++)
				{
					LogTypeConfigInfo logTypeConfigInfo;
					logTypeConfigInfo = ((LogTypeConfigInfo)(arrayList[k]));
					//--------------------------------------------------------------
					//if the KeepMostRecent setting has a numeric value,
					//use it to limit the log file to "x" of the most recent
					//logs, where "x" is the value of KeepMostRecent.
					//A value of "*" signifies to keep all log entries.
					//--------------------------------------------------------------
					if (logTypeConfigInfo.KeepMostRecent != "*" && logTypeConfigInfo.LogFileName != "")
					{
						XmlDocument xmlLog = new XmlDocument();
						bool fileExists = true;
						try
						{
							int intAttempts = 0;
							//wait for up to 100 milliseconds for the file
							//to be unlocked if it is not available
							while (xmlLog.OuterXml == "" && intAttempts < 100)
							{
								intAttempts ++;
								try
								{
									xmlLog.Load(logTypeConfigInfo.LogFileNameWithPath);
									fileExists = true;
								}
								catch (IOException)
								{
									Thread.SpinWait(1);
								}
							}
						}
						catch (FileNotFoundException)
						{
							fileExists = false;
							//file doesn't exist
						}
						catch (XmlException)
						{
							fileExists = false;
							//file is corrupt
						}
						if (fileExists)
						{
							XmlNodeList totalNodes;
							if (logTypeConfigInfo.LogTypePortalID == "*")
							{
								totalNodes = xmlLog.DocumentElement.SelectNodes("log[@LogTypeKey=\'" + logTypeConfigInfo.LogTypeKey + "\']");
							}
							else
							{
								totalNodes = xmlLog.DocumentElement.SelectNodes("log[@LogTypeKey=\'" + logTypeConfigInfo.LogTypeKey + "\' and LogPortalID=\'" + logTypeConfigInfo.LogTypePortalID + "\']");
							}
							
							int nodeCount = totalNodes.Count;
							int keepMostRecent = Convert.ToInt32(logTypeConfigInfo.KeepMostRecent);
							if (nodeCount > keepMostRecent)
							{
								int m = 0;
								
								foreach (XmlNode totalNode in totalNodes)
								{
									if (nodeCount - m > keepMostRecent)
									{
										xmlLog.DocumentElement.RemoveChild(totalNode);
									}
									m ++;
								}
								xmlLog.Save(logTypeConfigInfo.LogFileNameWithPath);
							}
							else
							{
								xmlLog = null;
							}
						}
					}
				}
			}
			finally
			{
				lockPurgeLog.ReleaseWriterLock();
			}
		}
		
		//--------------------------------------------------------------
		//Method to send email notifications
		//--------------------------------------------------------------
		public override void SendLogNotifications ()
		{
			try
			{
				lockNotif.AcquireWriterLock(writerLockTimeout);
				XmlDocument xmlPendingNotificationsDoc = new XmlDocument();
				try
				{
					xmlPendingNotificationsDoc.Load(GetFilePath(pendingNotificationsFile));
				}
				catch (FileNotFoundException)
				{
					//file not found
					return;
				}
				
				ArrayList arrLogTypeInfo;
				
				XMLLoggingProvider loggingProvider = new XMLLoggingProvider();
				arrLogTypeInfo = loggingProvider.GetLogTypeConfigInfo();
				
				PurgeLogBuffer();
				
				for (int a = 0; a < arrLogTypeInfo.Count ; a++)
				{
					LogTypeConfigInfo logTypeInfo;
					logTypeInfo = ((LogTypeConfigInfo) arrLogTypeInfo[a]);
					
					if (logTypeInfo.EmailNotificationIsActive)
					{
						XmlNodeList xmlPendingNotifications = xmlPendingNotificationsDoc.DocumentElement.SelectNodes("log[@NotificationLogTypeKey=\'" + 
							logTypeInfo.LogTypeKey + "\' and @LogTypePortalID=\'" + logTypeInfo.LogTypePortalID + "\' and number(@LogCreateDateNum) > " + DateToNum(logTypeInfo.StartDateTime).ToString() + "]");
						
						if (xmlPendingNotifications.Count >= logTypeInfo.NotificationThreshold)
						{
							//we have notifications to send out
							XmlDocument xmlOut = new XmlDocument();
							xmlOut.LoadXml("<notification></notification>");
							foreach (XmlNode xmlPendingNotification in xmlPendingNotifications)
							{
								XmlNode tmpNode;
								tmpNode = xmlOut.ImportNode(xmlPendingNotification, true);
								xmlOut.DocumentElement.AppendChild(tmpNode);
								
								//Remove the node from the list of pending notifications
								xmlPendingNotificationsDoc.DocumentElement.RemoveChild(xmlPendingNotification);
							}
							
							bool notificationFailed = false;
							string errSendNotif;
							
							errSendNotif = Globals.SendNotification(logTypeInfo.MailFromAddress, logTypeInfo.MailToAddress, 
								"", "Log Notification", xmlOut.OuterXml);
							
							if (errSendNotif != "")
							{
								//notification failed to send
								notificationFailed = true;
							}
							
							EventLogController eventLogController = new EventLogController();
							if (notificationFailed)
							{
								//Notification failed, log it
								LogInfo eventLogInfo = new LogInfo();
								eventLogInfo.LogTypeKey = EventLogController.EventLogType.LOG_NOTIFICATION_FAILURE.ToString();
								eventLogInfo.AddProperty("Log Notification Failed: ", errSendNotif);
								eventLogController.AddLog(eventLogInfo);
								
								//need to reload the xml doc because
								//we removed xml nodes above
								xmlPendingNotificationsDoc.Load(GetFilePath(pendingNotificationsFile));
								
								if (xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationFailure"] == null)
								{
									XmlAttribute xmlNotificationFailed;
									xmlNotificationFailed = xmlPendingNotificationsDoc.CreateAttribute("LastNotificationFailure");
									xmlNotificationFailed.Value = DateTime.Now.ToString();
									xmlPendingNotificationsDoc.DocumentElement.Attributes.Append(xmlNotificationFailed);
								}
								else
								{
									xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationFailure"].Value = DateTime.Now.ToString();
								}
								xmlPendingNotificationsDoc.Save(GetFilePath(pendingNotificationsFile));
							}
							else
							{
								//Notification succeeded.
								//Save the updated pending notifications file
								//so we remove the notifications that have been completed.
								if (xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationFailure"] != null)
								{
									xmlPendingNotificationsDoc.DocumentElement.Attributes.Remove(xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationFailure"]);
								}
								
								if (xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationSuccess"] == null)
								{
									XmlAttribute xmlNotificationSucceeded;
									xmlNotificationSucceeded = xmlPendingNotificationsDoc.CreateAttribute("LastNotificationSuccess");
									xmlNotificationSucceeded.Value = DateTime.Now.ToString();
									xmlPendingNotificationsDoc.DocumentElement.Attributes.Append(xmlNotificationSucceeded);
								}
								else
								{
									xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationSuccess"].Value = DateTime.Now.ToString();
								}
								xmlPendingNotificationsDoc.Save(GetFilePath(pendingNotificationsFile));
							}
						}
					}
				}
				
				loggingProvider.DeleteOldPendingNotifications();
			}
			catch (Exception exception)
			{
				Exceptions.Exceptions.LogException(exception);
			}
			finally
			{
				lockNotif.ReleaseWriterLock();
			}
		}
		
		//--------------------------------------------------------------
		//Methods to return functionality support indicators
		//--------------------------------------------------------------
		public override bool SupportsEmailNotification()
		{
			return true;
		}
		
		public override bool SupportsInternalViewer()
		{
			return true;
		}
		
		public override bool SupportsSendToCoreTeam()
		{
			return true;
		}
		
		public override bool SupportsSendViaEmail()
		{
			return true;
		}
		
		//--------------------------------------------------------------
		//Method to update Log Type Configuration
		//--------------------------------------------------------------
		public override void UpdateLogTypeConfigInfo (string id, bool isActive, string logTypeKey, string logTypePortalID, string keepMostRecent, 
			string logFileName, bool emailNotificationIsActive, string notificationThreshold, string notificationThresholdTime, string notificationThresholdTimeType, 
			string mailFromAddress, string mailToAddress)
		{
			DeleteLogTypeConfigInfo(id);
			AddLogTypeConfigInfo(id, isActive, logTypeKey, logTypePortalID, keepMostRecent, logFileName, emailNotificationIsActive, 
				notificationThreshold, notificationThresholdTime, notificationThresholdTimeType, mailFromAddress, mailToAddress);
		}
		
		#endregion
		
		#region "Private Helper Methods"
		
		private long DateToNum(DateTime dt)
		{
			
			long i;
			i = Convert.ToInt64(dt.Year) * 10000000000000;
			i += Convert.ToInt64(dt.Month) * 100000000000;
			i += Convert.ToInt64(dt.Day) * 1000000000;
			i += Convert.ToInt64(dt.Hour) * 10000000;
			i += Convert.ToInt64(dt.Minute) * 100000;
			i += Convert.ToInt64(dt.Second) * 1000;
			i += Convert.ToInt64(dt.Millisecond) * 1;
			return i;
		}
		
		private void DeleteOldPendingNotifications ()
		{
			XmlDocument xmlPendingNotificationsDoc = new XmlDocument();
			try
			{
				xmlPendingNotificationsDoc.Load(GetFilePath(pendingNotificationsFile));
			}
			catch (FileNotFoundException)
			{
				//file not found
				return;
			}
			//Check to see if we have had any
			//errors sending notifications.
			//If so, get out of this sub
			//so we don't delete any pending
			//notifications.  We only want
			//to delete old notifications
			//if the last notification succeeded.
			DateTime lastNotificationSuccess = Null.NullDate;
			DateTime lastNotificationFailure = Null.NullDate;
			if (xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationFailure"] != null)
			{
				lastNotificationFailure = System.Convert.ToDateTime(xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationFailure"].Value);
			}
			if (xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationSuccess"] != null)
			{
				lastNotificationSuccess = System.Convert.ToDateTime(xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationSuccess"].Value);
			}
			
			if (lastNotificationFailure > lastNotificationSuccess)
			{
				//the most recent notification cycle
				//failed, so we don't want to delete
				//any pending notifications
				return;
			}
			
			XmlDocument xmlConfigDoc = GetConfigDoc();
			
			ArrayList arrLogTypeInfo;
			arrLogTypeInfo = GetLogTypeConfigInfo();
			
			foreach (LogTypeConfigInfo logTypeInfo in arrLogTypeInfo)
			{				
				if (logTypeInfo.EmailNotificationIsActive)
				{
					XmlNodeList xmlPendingNotifications = xmlPendingNotificationsDoc.DocumentElement.SelectNodes("log[@LogTypeKey=\'" + 
						logTypeInfo.LogTypeKey + "\' and @LogTypePortalID=\'" + logTypeInfo.LogTypePortalID + "\' and @CreateDateNum < \'" + DateToNum(logTypeInfo.StartDateTime).ToString() + "\']");
					
					if (xmlPendingNotifications.Count > 0)
					{
						//we have pending notifications to delete
						//because time has elapsed putting
						//them out of scope for the log type settings
						foreach (XmlNode xmlPendingNotification in xmlPendingNotifications)
						{
							//Remove the node from the list of pending notifications
							xmlPendingNotificationsDoc.DocumentElement.RemoveChild(xmlPendingNotification);
						}
					}
				}
			}
			xmlPendingNotificationsDoc.Save(GetFilePath(pendingNotificationsFile));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -