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

📄 logcontroller.cs

📁 SharpNuke源代码
💻 CS
字号:
using System;
using System.Collections;
using System.IO;
using System.Threading;
using System.Web;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Users;

//
// DotNetNuke -  http://www.dotnetnuke.com
// Copyright (c) 2002-2005
// by Shaun Walker ( sales@perpetualmotion.ca ) of Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

namespace DotNetNuke.Services.Log.EventLog
{
	
	public class LogController
	{
		
		private static ReaderWriterLock lockLog = new ReaderWriterLock();
		private const int ReaderLockTimeout = 10000; //milliseconds
		private const int WriterLockTimeout = 10000; //milliseconds
		
		public void AddLog (LogInfo logInfo)
		{
			try
			{
				logInfo.LogCreateDate = DateTime.Now;
				logInfo.LogServerName = Common.Globals.ServerName;
				
				if (logInfo.LogUserName == null || logInfo.LogUserName.Length == 0)
				{
					if (HttpContext.Current != null)
					{
						if (HttpContext.Current.Request.IsAuthenticated)
						{
							UserInfo userInfo = UserController.GetCurrentUserInfo();
							logInfo.LogUserName = userInfo.Membership.Username;
						}
					}
				}
				
				LoggingProvider.Instance().AddLog(logInfo);
			}
			catch (Exception)
			{
				try
				{
					string message = XmlUtils.Serialize(logInfo);
					string fileName;
					fileName = Common.Globals.HostMapPath + "\\Logs\\LogFailures.xml.resources";
					WriteLog(fileName, message);
				}
				catch (Exception)
				{
					//critical error writing
				}
			}
		}
		
		public bool LoggingIsEnabled(string logType, int portalID)
		{
			return LoggingProvider.Instance().LoggingIsEnabled(logType, portalID);
		}
		
		public void DeleteLog (LogInfo logInfo)
		{
			LoggingProvider.Instance().DeleteLog(logInfo);
		}
		
		public void ClearLog ()
		{
			LoggingProvider.Instance().ClearLog();
		}
		
		public virtual object GetSingleLog(LogInfo logInfo, LoggingProvider.ReturnType returnType)
		{
			return LoggingProvider.Instance().GetSingleLog(logInfo, returnType);
		}
		
		public void PurgeLogBuffer ()
		{
			LoggingProvider.Instance().PurgeLogBuffer();
		}
		
		[Obsolete("This method has been replaced with one that supports record paging.")]
		public virtual LogInfoArray GetLog()
		{
			return LoggingProvider.Instance().GetLog();
		}
		
		[Obsolete("This method has been replaced with one that supports record paging.")]
		public virtual LogInfoArray GetLog(int portalID)
		{
			return LoggingProvider.Instance().GetLog(portalID);
		}
		
		[Obsolete("This method has been replaced with one that supports record paging.")]
		public virtual LogInfoArray GetLog(int portalID, string logType)
		{
			return LoggingProvider.Instance().GetLog(portalID, logType);
		}
		
		[Obsolete("This method has been replaced with one that supports record paging.")]
		public virtual LogInfoArray GetLog(string logType)
		{
			return LoggingProvider.Instance().GetLog(logType);
		}
		
		public virtual LogInfoArray GetLog(int pageSize, int pageIndex, ref int totalRecords)
		{
			return LoggingProvider.Instance().GetLog(pageSize, pageIndex, ref totalRecords);
		}

		public virtual LogInfoArray GetLog(int portalID, int pageSize, int pageIndex, ref int totalRecords)
		{
			return LoggingProvider.Instance().GetLog(portalID, pageSize, pageIndex, ref totalRecords);
		}
	
		public virtual LogInfoArray GetLog(int portalID, string logType, int pageSize, int pageIndex, ref int totalRecords)
		{
			return LoggingProvider.Instance().GetLog(portalID, logType, pageSize, pageIndex, ref totalRecords);
		}

		public virtual LogInfoArray GetLog(string logType, int pageSize, int pageIndex, ref int totalRecords)
		{
			return LoggingProvider.Instance().GetLog(logType, pageSize, pageIndex, ref totalRecords);
		}

		public virtual ArrayList GetLogTypeConfigInfo()
		{
			return LoggingProvider.Instance().GetLogTypeConfigInfo();
		}
		
		public virtual LogTypeConfigInfo GetLogTypeConfigInfoByID(string logTypeID)
		{
			return LoggingProvider.Instance().GetLogTypeConfigInfoByID(logTypeID);
		}
		
		public virtual ArrayList GetLogTypeInfo()
		{
			return LoggingProvider.Instance().GetLogTypeInfo();
		}
		
		public virtual bool SupportsEmailNotification()
		{
			return LoggingProvider.Instance().SupportsEmailNotification();
		}
		
		public virtual bool SupportsInternalViewer()
		{
			return LoggingProvider.Instance().SupportsInternalViewer();
		}
		
		public virtual void AddLogTypeConfigInfo (LogTypeConfigInfo logTypeConfigInfo)
		{
			LoggingProvider.Instance().AddLogTypeConfigInfo(logTypeConfigInfo.ID, logTypeConfigInfo.LoggingIsActive, 
				logTypeConfigInfo.LogTypeKey, logTypeConfigInfo.LogTypePortalID, logTypeConfigInfo.KeepMostRecent, 
				logTypeConfigInfo.LogFileName, logTypeConfigInfo.EmailNotificationIsActive, 
				Convert.ToString(logTypeConfigInfo.NotificationThreshold), Convert.ToString(logTypeConfigInfo.NotificationThresholdTime), 
				Convert.ToString(logTypeConfigInfo.NotificationThresholdTimeType), logTypeConfigInfo.MailFromAddress, 
				logTypeConfigInfo.MailToAddress );
		}
		
		public virtual void UpdateLogTypeConfigInfo (LogTypeConfigInfo logTypeConfigInfo)
		{
			LoggingProvider.Instance().UpdateLogTypeConfigInfo(logTypeConfigInfo.ID, logTypeConfigInfo.LoggingIsActive, 
				logTypeConfigInfo.LogTypeKey, logTypeConfigInfo.LogTypePortalID, logTypeConfigInfo.KeepMostRecent, 
				logTypeConfigInfo.LogFileName, logTypeConfigInfo.EmailNotificationIsActive, 
				Convert.ToString(logTypeConfigInfo.NotificationThreshold), Convert.ToString(logTypeConfigInfo.NotificationThresholdTime), 
				Convert.ToString(logTypeConfigInfo.NotificationThresholdTimeType), logTypeConfigInfo.MailFromAddress, 
				logTypeConfigInfo.MailToAddress);
		}
		
		public virtual void DeleteLogTypeConfigInfo (LogTypeConfigInfo logTypeConfigInfo)
		{
			LoggingProvider.Instance().DeleteLogTypeConfigInfo(logTypeConfigInfo.ID);
		}
		
		public virtual void AddLogType (LogTypeInfo logTypeInfo)
		{
			LoggingProvider.Instance().AddLogType(logTypeInfo.LogTypeKey, logTypeInfo.LogTypeFriendlyName, 
				logTypeInfo.LogTypeDescription, logTypeInfo.LogTypeCSSClass, logTypeInfo.LogTypeOwner);
		}
		
		public virtual void UpdateLogType (LogTypeInfo logTypeInfo)
		{
			LoggingProvider.Instance().UpdateLogType(logTypeInfo.LogTypeKey, logTypeInfo.LogTypeFriendlyName, 
				logTypeInfo.LogTypeDescription, logTypeInfo.LogTypeCSSClass, logTypeInfo.LogTypeOwner);
		}
		
		public virtual void DeleteLogType (LogTypeInfo logTypeInfo)
		{
			LoggingProvider.Instance().DeleteLogType(logTypeInfo.LogTypeKey);
		}
		
		private void WriteLog (string filePath, string message)
		{
			//--------------------------------------------------------------
			//Write the log entry
			//--------------------------------------------------------------
			FileStream fileStream = null;
			StreamWriter streamWriter = null;
			
			try
			{
				//--------------------------------------------------------------
				// Write the entry to the log.
				//--------------------------------------------------------------
				lockLog.AcquireWriterLock(WriterLockTimeout);
				int attempts = 0;
				//wait for up to 100 milliseconds for the file
				//to be unlocked if it is not available
				while (fileStream == null && attempts < 100)
				{
					attempts ++;
					try
					{
						fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
					}
					catch (IOException)
					{
						Thread.Sleep(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 > 9)
					{
						//--------------------------------------------------------------
						//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
						//--------------------------------------------------------------
						message = "<logs>" + message;
					}
					
					//--------------------------------------------------------------
					//write out our exception
					//--------------------------------------------------------------
					streamWriter.WriteLine(message + "</logs>");
					streamWriter.Flush();
				}
				if (streamWriter != null)
				{
					streamWriter.Close();
				}
				if (fileStream != null)
				{
					fileStream.Close();
				}
				//--------------------------------------------------------------
				//handle the more common exceptions up
				//front, leave less common ones to the end
				//--------------------------------------------------------------
			}
			catch (UnauthorizedAccessException exception)
			{
				if (HttpContext.Current != null)
				{
					HttpResponse response = HttpContext.Current.Response;
					HtmlUtils.WriteHeader(response, "Unauthorized Access Error");
					
					string strMessage = exception.Message + " The Windows User Account listed below must have Read/Write Privileges to this path.";
					HtmlUtils.WriteError(response, filePath, strMessage);
					
					HtmlUtils.WriteFooter(response);
					response.End();
				}
			}
			catch (DirectoryNotFoundException exception)
			{
				if (HttpContext.Current != null)
				{
					HttpResponse response = HttpContext.Current.Response;
					HtmlUtils.WriteHeader(response, "Directory Not Found Error");
					
					string strMessage = exception.Message;
					HtmlUtils.WriteError(response, filePath, strMessage);
					
					HtmlUtils.WriteFooter(response);
					response.End();
				}
			}
			catch (PathTooLongException exception)
			{
				if (HttpContext.Current != null)
				{
					HttpResponse response = HttpContext.Current.Response;
					HtmlUtils.WriteHeader(response, "Path Too Long Error");
					
					string strMessage = exception.Message;
					HtmlUtils.WriteError(response, filePath, strMessage);
					
					HtmlUtils.WriteFooter(response);
					response.End();
				}
			}
			catch (IOException exception)
			{
				if (HttpContext.Current != null)
				{
					HttpResponse response = HttpContext.Current.Response;
					HtmlUtils.WriteHeader(response, "IO Error");
					
					string strMessage = exception.Message;
					HtmlUtils.WriteError(response, filePath, strMessage);
					HtmlUtils.WriteFooter(response);
					response.End();
				}
			}
			catch (Exception exception)
			{
				if (HttpContext.Current != null)
				{
					HttpResponse response = HttpContext.Current.Response;
					HtmlUtils.WriteHeader(response, "Unhandled Error");
					
					string strMessage = exception.Message;
					HtmlUtils.WriteError(response, filePath, strMessage);
					HtmlUtils.WriteFooter(response);
					response.End();
				}
			}
			finally
			{
				if (streamWriter != null)
				{
					streamWriter.Close();
				}
				if (fileStream != null)
				{
					fileStream.Close();
				}
				
				lockLog.ReleaseWriterLock();
			}
		}
	}
	
}





⌨️ 快捷键说明

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