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

📄 xmlloggingprovider.cs

📁 SharpNuke源代码
💻 CS
📖 第 1 页 / 共 4 页
字号:
		private LogTypeConfigInfo GetLogTypeConfigInfoFromXML(XmlNode xmlLogTypeInfo)
		{
			LogTypeConfigInfo logTypeConfigInfo = new LogTypeConfigInfo();
			logTypeConfigInfo.LogFileNameWithPath = GetFilePath(xmlLogTypeInfo.Attributes["FileName"].Value);
			logTypeConfigInfo.LogFileName = xmlLogTypeInfo.Attributes["FileName"].Value;
			logTypeConfigInfo.LogTypeKey = xmlLogTypeInfo.Attributes["LogTypeKey"].Value;
			if (xmlLogTypeInfo.Attributes["LogTypePortalID"] != null)
			{
				logTypeConfigInfo.LogTypePortalID = xmlLogTypeInfo.Attributes["LogTypePortalID"].Value;
			}
			else
			{
				logTypeConfigInfo.LogTypePortalID = "*";
			}
			if (xmlLogTypeInfo.Attributes["KeepMostRecent"] != null)
			{
				logTypeConfigInfo.KeepMostRecent = xmlLogTypeInfo.Attributes["KeepMostRecent"].Value;
			}
			else
			{
				logTypeConfigInfo.KeepMostRecent = "*";
			}
			logTypeConfigInfo.ID = xmlLogTypeInfo.Attributes["LogFileID"].Value;
			if (xmlLogTypeInfo.Attributes["LoggingStatus"] != null)
			{
				logTypeConfigInfo.LoggingIsActive = xmlLogTypeInfo.Attributes["LoggingStatus"].Value.ToLower() == "on";
			}
			else
			{
				logTypeConfigInfo.LoggingIsActive = false;
			}
			if (xmlLogTypeInfo.Attributes["EmailNotificationStatus"] != null)
			{
				logTypeConfigInfo.EmailNotificationIsActive = xmlLogTypeInfo.Attributes["EmailNotificationStatus"].Value.ToLower() == "on";
			}
			else
			{
				logTypeConfigInfo.EmailNotificationIsActive = false;
			}
			if (xmlLogTypeInfo.Attributes["MailFromAddress"] != null)
			{
				logTypeConfigInfo.MailFromAddress = xmlLogTypeInfo.Attributes["MailFromAddress"].Value;
			}
			else
			{
				logTypeConfigInfo.MailFromAddress = Null.NullString;
			}
			if (xmlLogTypeInfo.Attributes["MailToAddress"] != null)
			{
				logTypeConfigInfo.MailToAddress = xmlLogTypeInfo.Attributes["MailToAddress"].Value;
			}
			else
			{
				logTypeConfigInfo.MailToAddress = Null.NullString;
			}
			if (xmlLogTypeInfo.Attributes["NotificationThreshold"] != null)
			{
				logTypeConfigInfo.NotificationThreshold = Convert.ToInt32(xmlLogTypeInfo.Attributes["NotificationThreshold"].Value);
			}
			else
			{
				logTypeConfigInfo.NotificationThreshold = - 1;
			}
			if (xmlLogTypeInfo.Attributes["NotificationThresholdTime"] != null)
			{
				logTypeConfigInfo.NotificationThresholdTime = Convert.ToInt32(xmlLogTypeInfo.Attributes["NotificationThresholdTime"].Value);
			}
			else
			{
				logTypeConfigInfo.NotificationThresholdTime = - 1;
			}
			if (xmlLogTypeInfo.Attributes["NotificationThresholdTimeType"] != null)
			{
				string notificationThresholdTimeType = xmlLogTypeInfo.Attributes["NotificationThresholdTimeType"].Value;
				switch (notificationThresholdTimeType)
				{
					case "1":
						logTypeConfigInfo.NotificationThresholdTimeType = LogTypeConfigInfo.NotificationThresholdTimeTypes.Seconds;
						break;
					case "2":
						logTypeConfigInfo.NotificationThresholdTimeType = LogTypeConfigInfo.NotificationThresholdTimeTypes.Minutes;
						break;
					case "3":
						logTypeConfigInfo.NotificationThresholdTimeType = LogTypeConfigInfo.NotificationThresholdTimeTypes.Hours;
						break;
					case "4":
						logTypeConfigInfo.NotificationThresholdTimeType = LogTypeConfigInfo.NotificationThresholdTimeTypes.Days;
						break;
					default:
						logTypeConfigInfo.NotificationThresholdTimeType = LogTypeConfigInfo.NotificationThresholdTimeTypes.None;
						break;
				}
			}
			else
			{
				logTypeConfigInfo.NotificationThresholdTimeType = LogTypeConfigInfo.NotificationThresholdTimeTypes.None;
			}
			
			return logTypeConfigInfo;
		}
		
		private LogTypeInfo GetLogTypeInfoFromXML(XmlNode xmlLogTypeInfo)
		{
			LogTypeInfo logTypeInfo = new LogTypeInfo();
			logTypeInfo.LogTypeKey = xmlLogTypeInfo.Attributes["LogTypeKey"].Value;
			logTypeInfo.LogTypeFriendlyName = xmlLogTypeInfo.Attributes["LogTypeFriendlyName"].Value;
			logTypeInfo.LogTypeDescription = xmlLogTypeInfo.Attributes["LogTypeDescription"].Value;
			logTypeInfo.LogTypeOwner = xmlLogTypeInfo.Attributes["LogTypeOwner"].Value;
			logTypeInfo.LogTypeCSSClass = xmlLogTypeInfo.Attributes["LogTypeCSSClass"].Value;
			return logTypeInfo;
		}
		
		private XmlElement GetXMLFromLogTypeConfigInfo(string logFileID, bool isActive, string logTypeKey, string logTypePortalID, 
			string keepMostRecent, string logFileName, bool emailNotificationIsActive, string notificationThreshold, 
			string notificationThresholdTime, string notificationThresholdTimeType, string mailFromAddress, string mailToAddress)
		{
			string blankLogConfig;
			blankLogConfig = "	<LogTypeConfig LogFileID=\"\" LoggingStatus=\"\" LogTypeKey=\"\" LogTypePortalID=\"\" KeepMostRecent=\"\" FileName=\"\" EmailNotificationStatus=\"\" NotificationThreshold=\"\" NotificationThresholdTime=\"\" NotificationThresholdTimeType=\"\" MailFromAddress=\"\" MailToAddress=\"\"/>";
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.LoadXml(blankLogConfig);
			XmlElement xmlDocEl;
			xmlDocEl = xmlDoc.DocumentElement;
			xmlDocEl.Attributes["LogFileID"].Value = logFileID;
			xmlDocEl.Attributes["LoggingStatus"].Value = isActive ? "On" : "Off";
			xmlDocEl.Attributes["LogTypeKey"].Value = logTypeKey;
			xmlDocEl.Attributes["LogTypePortalID"].Value = logTypePortalID;
			xmlDocEl.Attributes["KeepMostRecent"].Value = keepMostRecent;
			xmlDocEl.Attributes["FileName"].Value = logFileName;
			xmlDocEl.Attributes["EmailNotificationStatus"].Value = emailNotificationIsActive ? "On" : "Off";
			xmlDocEl.Attributes["NotificationThreshold"].Value = notificationThreshold;
			xmlDocEl.Attributes["NotificationThresholdTime"].Value = notificationThresholdTime;
			xmlDocEl.Attributes["NotificationThresholdTimeType"].Value = notificationThresholdTimeType;
			xmlDocEl.Attributes["MailFromAddress"].Value = mailFromAddress;
			xmlDocEl.Attributes["MailToAddress"].Value = mailToAddress;
			return xmlDocEl;
		}
		
		private XmlElement GetXMLFromLogTypeInfo(string logTypeKey, string logTypeFriendlyName, string logTypeDescription, string logTypeCSSClass, string logTypeOwner)
		{
			string blankLogConfig;
			blankLogConfig = "<LogType LogTypeKey=\"\" LogTypeFriendlyName=\"\" LogTypeDescription=\"\" LogTypeOwner=\"\" LogTypeCSSClass=\"\"/>";
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.LoadXml(blankLogConfig);
			XmlElement xmlDocEl;
			xmlDocEl = xmlDoc.DocumentElement;
			xmlDocEl.Attributes["LogTypeKey"].Value = logTypeKey;
			xmlDocEl.Attributes["LogTypeFriendlyName"].Value = logTypeFriendlyName;
			xmlDocEl.Attributes["LogTypeDescription"].Value = logTypeDescription;
			xmlDocEl.Attributes["LogTypeOwner"].Value = logTypeOwner;
			xmlDocEl.Attributes["LogTypeCSSClass"].Value = logTypeCSSClass;
			return xmlDocEl;
		}
		
		private void WriteLog (LogQueueItem logQueueItem)
		{
			
			//--------------------------------------------------------------
			//Write the log entry
			//--------------------------------------------------------------
			FileStream fileStream = null;
			StreamWriter streamWriter = null;
			
			LogTypeConfigInfo logTypeConfigInfo = logQueueItem.LogTypeConfigInfo;
			string logString = logQueueItem.LogString;
			
			try
			{
				if (logTypeConfigInfo.LogFileNameWithPath != "")
				{
					//--------------------------------------------------------------
					// Write the entry to the log.
					//--------------------------------------------------------------
					lockLog.AcquireWriterLock(writerLockTimeout);
					int intAttempts = 0;
					//wait for up to 100 milliseconds for the file
					//to be unlocked if it is not available
					while (fileStream == null|| intAttempts < 100)
					{
						intAttempts ++;
						try
						{
							fileStream = new FileStream(logTypeConfigInfo.LogFileNameWithPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
						}
						catch (IOException)
						{
							Thread.SpinWait(1);
						}
					}
					
					if (fileStream == null)
					{
						if (HttpContext.Current != null)
						{
							HttpContext.Current.Response.Write("An error has occurred writing to the exception log.");
							HttpContext.Current.Response.End();
						}
					}
					else
					{
						//--------------------------------------------------------------
						//Instantiate a new StreamWriter
						//--------------------------------------------------------------
						streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
						long fileLength;
						fileLength = fileStream.Length;
						//--------------------------------------------------------------
						//check to see if this file is new
						//--------------------------------------------------------------
						if (fileLength > 0)
						{
							//--------------------------------------------------------------
							//file is not new, set the position to just before
							//the closing root element tag
							//--------------------------------------------------------------
							fileStream.Position = fileLength - 9;
						}
						else
						{
							//--------------------------------------------------------------
							//file is new, create the opening root element tag
							//--------------------------------------------------------------
							logString = "<logs>" + logString;
						}
						
						//--------------------------------------------------------------
						//write out our exception
						//--------------------------------------------------------------
						streamWriter.WriteLine(logString + "</logs>");
						streamWriter.Flush();
					}
					if (streamWriter != null)
					{
						streamWriter.Close();
					}
					if (fileStream != null)
					{
						fileStream.Close();
					}
				}
				if (logTypeConfigInfo.EmailNotificationIsActive)
				{
					try
					{
						lockNotif.AcquireWriterLock(readerLockTimeout);
						
						XmlDocument xmlDoc = new XmlDocument();
						xmlDoc.LoadXml(logString);
						
						//If the threshold for email notifications is
						//set to 0, send an email notification each
						//time a log entry is written.
						
						
						if (logTypeConfigInfo.NotificationThreshold == 0)
						{
							Globals.SendNotification(logTypeConfigInfo.MailFromAddress, logTypeConfigInfo.MailToAddress, "", "Event Notification", xmlDoc.InnerXml);
						}
						else if (logTypeConfigInfo.LogTypeKey != "LOG_NOTIFICATION_FAILURE")
						{
							XmlDocument xmlPendingNotificationsDoc = new XmlDocument();
							try
							{
								xmlPendingNotificationsDoc.Load(GetFilePath(pendingNotificationsFile));
							}
							catch (FileNotFoundException)
							{
								//file not created yet
								xmlPendingNotificationsDoc.LoadXml("<PendingNotifications></PendingNotifications>");
							}
							XmlNode xmlLogNode;
							xmlLogNode = xmlPendingNotificationsDoc.ImportNode(xmlDoc.FirstChild, true);
							
							XmlAttribute xmlAttrib;
							xmlAttrib = xmlPendingNotificationsDoc.CreateAttribute("MailFromAddress");
							xmlAttrib.Value = Convert.ToString(logTypeConfigInfo.MailFromAddress);
							xmlLogNode.Attributes.Append(xmlAttrib);
							
							xmlAttrib = xmlPendingNotificationsDoc.CreateAttribute("NotificationLogTypeKey");
							xmlAttrib.Value = Convert.ToString(logTypeConfigInfo.LogTypeKey);
							xmlLogNode.Attributes.Append(xmlAttrib);
							
							xmlAttrib = xmlPendingNotificationsDoc.CreateAttribute("LogTypePortalID");
							if (logTypeConfigInfo.LogTypePortalID == "-1")
							{
								xmlAttrib.Value = "*";
							}
							else
							{
								xmlAttrib.Value = logTypeConfigInfo.LogTypePortalID;
							}
							xmlLogNode.Attributes.Append(xmlAttrib);
							
							XmlElement element;
							element = xmlPendingNotificationsDoc.CreateElement("EmailAddress");
							element.InnerText = logTypeConfigInfo.MailToAddress;
							xmlLogNode.AppendChild(element);
							
							xmlPendingNotificationsDoc.DocumentElement.AppendChild(xmlLogNode);
							xmlPendingNotificationsDoc.Save(GetFilePath(pendingNotificationsFile));
						}
					}
					finally
					{
						lockNotif.ReleaseWriterLock();
					}
				}
				//--------------------------------------------------------------
				//handle the more common exceptions up
				//front, leave less common ones to the end
				//--------------------------------------------------------------
			}
			catch (UnauthorizedAccessException exc)
			{
				if (HttpContext.Current != null)
				{
					HttpResponse response = HttpContext.Current.Response;
					HtmlUtils.WriteHeader(response, "Unauthorized Access Error");
					
					string message = exc.Message + " The Windows User Account listed below must have Read/Write Privileges to this path.";
					HtmlUtils.WriteError(response, logTypeConfigInfo.LogFileNameWithPath, message);
					
					HtmlUtils.WriteFooter(response);
					response.End();
				}
			}
			catch (DirectoryNotFoundException exc)
			{
				if (HttpContext.Current != null)
				{
					HttpResponse response = HttpContext.Current.Response;
					HtmlUtils.WriteHeader(response, "Directory Not Found Error");
					
					string message = exc.Message;
					HtmlUtils.WriteError(response, logTypeConfigInfo.LogFileNameWithPath, message);
					
					HtmlUtils.WriteFooter(response);
					response.End();
				}
			}
			catch (PathTooLongException exc)
			{
				if (HttpContext.Current != null)
				{
					HttpResponse response = HttpContext.Current.Response;
					HtmlUtils.WriteHeader(response, "Path Too Long Error");
					
					string message = exc.Message;
					HtmlUtils.WriteError(response, logTypeConfigInfo.LogFileNameWithPath, message);
					
					HtmlUtils.WriteFooter(response);
					response.End();
				}
			}
			catch (IOException exc)
			{
				if (HttpContext.Current != null)
				{
					HttpResponse response = HttpContext.Current.Response;
					HtmlUtils.WriteHeader(response, "IO Error");
					
					string message = exc.Message;
					HtmlUtils.WriteError(response, logTypeConfigInfo.LogFileNameWithPath, message);
					
					HtmlUtils.WriteFooter(response);
					response.End();
				}
			}
			catch (Exception exc)
			{
				if (HttpContext.Current != null)
				{
					HttpResponse response = HttpContext.Current.Response;
					HtmlUtils.WriteHeader(response, "Unhandled Error");
					
					string message = exc.Message;
					HtmlUtils.WriteError(response, logTypeConfigInfo.LogFileNameWithPath, message);
					
					HtmlUtils.WriteFooter(response);
					response.End();
				}
			}
			finally
			{
				if (streamWriter != null)
				{
					streamWriter.Close();
				}
				if (fileStream != null)
				{
					fileStream.Close();
				}
				
				lockLog.ReleaseWriterLock();
			}
		}
		
		#endregion
		
	}
		
}

⌨️ 快捷键说明

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