📄 xmlloggingprovider.cs
字号:
}
private XmlDocument GetConfigDoc()
{
if (((XmlDocument) DataCache.GetCache("LoggingGetConfigDoc"))== null)
{
string configDoc = GetConfigDocFileName();
XmlDocument xmlConfigDoc = new XmlDocument();
if (!File.Exists(configDoc))
{
string templateLogConfig = Globals.HostMapPath + "Logs\\LogConfig\\LogConfigTemplate.xml.resources";
File.Copy(templateLogConfig, configDoc);
File.SetAttributes(configDoc, FileAttributes.Normal);
}
int intAttempts = 0;
//wait for up to 100 milliseconds for the file
//to be unlocked if it is not available
while (xmlConfigDoc.OuterXml == "" && intAttempts < 100)
{
intAttempts ++;
try
{
xmlConfigDoc.Load(configDoc);
}
catch (IOException)
{
Thread.SpinWait(1);
}
}
string filePath = GetConfigDocFileName();
if (File.Exists(filePath))
{
DataCache.SetCache("LoggingGetConfigDoc", xmlConfigDoc, new System.Web.Caching.CacheDependency(filePath));
}
else
{
return xmlConfigDoc;
}
}
return ((XmlDocument) DataCache.GetCache("LoggingGetConfigDoc"));
}
private string GetConfigDocFileName()
{
//--------------------------------------------------------------
// Read the configuration specific information for this provider
//--------------------------------------------------------------
Provider provider = ((Provider) providerConfiguration.Providers[providerConfiguration.DefaultProvider]);
return GetFilePath(provider.Attributes["configfilename"], "LogConfig\\");
}
private string GetConfigProviderPath()
{
//--------------------------------------------------------------
// Read the configuration specific information for this provider
//--------------------------------------------------------------
Provider provider = ((Provider) providerConfiguration.Providers[providerConfiguration.DefaultProvider]);
return HttpContext.Current.Server.MapPath(provider.Attributes["providerPath"]);
}
private static string GetFilePath(string fileName)
{
return GetFilePath(fileName, "");
}
private static string GetFilePath(string fileName, string folder)
{
//--------------------------------------------------------------
//check to see if they entered a filename or an absolute file path
//--------------------------------------------------------------
if (fileName.IndexOf("\\") == - 1 && fileName.IndexOf ("/") == - 1)
{
//--------------------------------------------------------------
//Config settings specified a filename only, with no absolute path
//Use the /Portals/_default/Logs directory to store the log file
//This allows user to specify alternative location for
//log files to be stored, othre than the DNN directory.
//--------------------------------------------------------------
HttpContext httpContext = HttpContext.Current;
if (folder != null && folder.Length > 0)
{
fileName = Globals.HostMapPath + "Logs\\" + folder + fileName;
}
else
{
fileName = Globals.HostMapPath + "Logs\\" + fileName;
}
}
return fileName;
}
private string GetLogFileByLogFileID(XmlDocument xmlConfigDoc, string logFileID)
{
XmlNode xmlLogFile;
xmlLogFile = xmlConfigDoc.SelectSingleNode("/LogConfig/LogTypeConfig[@LogFileID=\'" + logFileID + "\']/@FileName");
if (xmlLogFile != null)
{
return GetFilePath(xmlLogFile.InnerText);
}
return "";
}
private ArrayList GetLogFiles(XmlDocument xmlConfigDoc)
{
return GetLogFiles(xmlConfigDoc, "*", "*");
}
private ArrayList GetLogFiles(XmlDocument xmlConfigDoc, string portalID)
{
return GetLogFiles(xmlConfigDoc, portalID, "*");
}
private ArrayList GetLogFiles(XmlDocument xmlConfigDoc, string portalID, string logTypeKey)
{
Hashtable ht = new Hashtable();
ArrayList arrFiles = new ArrayList();
//--------------------------------------------------------------
//First see if there is a log file specified
//for this log type and this PortalID
//--------------------------------------------------------------
XmlNodeList xmlLogFiles = null;
if (portalID == "*" && logTypeKey == "*")
{
xmlLogFiles = xmlConfigDoc.SelectNodes("/LogConfig/LogTypeConfig/@FileName");
}
else if (portalID != "*" && logTypeKey == "*")
{
xmlLogFiles = xmlConfigDoc.SelectNodes("/LogConfig/LogTypeConfig[@LogTypePortalID=\'" + portalID + "\']/@FileName");
}
else if (portalID == "*" && logTypeKey != "*")
{
xmlLogFiles = xmlConfigDoc.SelectNodes("/LogConfig/LogTypeConfig[@LogTypeKey=\'" + logTypeKey + "\']/@FileName");
}
else if (portalID != "*" && logTypeKey != "*")
{
xmlLogFiles = xmlConfigDoc.SelectNodes("/LogConfig/LogTypeConfig[@LogTypePortalID=\'" + portalID + "\' and @LogTypeKey=\'" + logTypeKey + "\']/@FileName");
}
if (xmlLogFiles != null)
{
foreach (XmlNode xmlLogFile in xmlLogFiles)
{
if (xmlLogFile.InnerText != "" && ht[xmlLogFile.InnerText] == null)
{
//dedupe
arrFiles.Add(GetFilePath(xmlLogFile.InnerText));
ht.Add(xmlLogFile.InnerText, true);
}
}
}
//If arrFiles.Count > 0 Then
// Return arrFiles
//End If
//--------------------------------------------------------------
//This is a catch all...it gets the default log file name.
//--------------------------------------------------------------
XmlNode xmlDefaultLogFile;
xmlDefaultLogFile = xmlConfigDoc.SelectSingleNode("/LogConfig/LogTypeConfig[@LogTypeKey=\'*\' and @LogTypePortalID=\'*\']/@FileName");
if (xmlDefaultLogFile != null && ht[xmlDefaultLogFile.InnerText] == null)
{
//dedupe
arrFiles.Add(GetFilePath(xmlDefaultLogFile.InnerText));
ht.Add(xmlDefaultLogFile.InnerText, true);
}
return arrFiles;
}
private LogInfoArray GetLogFromXPath(string xpath, string portalID, string logType, int pageSize, int pageIndex, ref int totalRecords)
{
XmlDocument xmlConfigDoc = GetConfigDoc();
ArrayList arrLogFiles = GetLogFiles(xmlConfigDoc, portalID, logType);
XmlDocument xmlLogFiles = new XmlDocument();
xmlLogFiles.LoadXml("<LogCollection></LogCollection>");
XmlElement xmlLogFilesDocEl;
xmlLogFilesDocEl = xmlLogFiles.DocumentElement;
ArrayList arrLogInfo = new ArrayList();
foreach (string logFile in arrLogFiles)
{
bool fileIsCorrupt = false;
bool fileExists = true;
XmlDocument xmlLogFile = new XmlDocument();
try
{
lockLog.AcquireReaderLock(readerLockTimeout);
xmlLogFile.Load(logFile);
}
catch (FileNotFoundException)
{
fileExists = false;
//file doesn't exist
}
catch (XmlException)
{
fileExists = false;
fileIsCorrupt = true;
//file is corrupt
}
finally
{
lockLog.ReleaseReaderLock();
}
if (fileIsCorrupt)
{
string s = "A log file is corrupt \'" + logFile + "\'.";
if (logFile.IndexOf("Exceptions.xml.resources") > -1)
{
s += " This could be due to an older exception log file being written to by the new logging provider. Try removing \'Exceptions.xml.resources\' from the logs directory to solve the problem.";
}
LogInfo eventLogInfo = new LogInfo();
eventLogInfo.AddProperty("Note", s);
eventLogInfo.BypassBuffering = true;
eventLogInfo.LogTypeKey = "HOST_ALERT";
EventLogController eventLog = new EventLogController();
eventLog.AddLog(eventLogInfo);
}
else if (fileExists)
{
XmlNodeList xmlNodes;
xmlNodes = xmlLogFile.SelectNodes(xpath);
XmlElement xmlLogNodes;
xmlLogNodes = xmlLogFiles.CreateElement("logs");
foreach (XmlNode xmlNode in xmlNodes)
{
xmlLogNodes.AppendChild(xmlLogFiles.ImportNode(xmlNode, true));
}
xmlLogFilesDocEl.AppendChild(xmlLogNodes);
}
}
return GetLogInfoFromXML(xmlLogFiles, pageSize, pageIndex, ref totalRecords);
}
private LogInfoArray GetLogInfoFromXML(XmlDocument xmlLogFiles, int pageSize, int pageIndex, ref int totalRecords)
{
XslTransform xslt = new XslTransform();
xslt.Load(GetConfigProviderPath() + "log.xslt");
//Create the Stream to place the output.
Stream stream = new MemoryStream();
StreamWriter streamWriter = new StreamWriter(stream, System.Text.Encoding.UTF8);
//Transform the file.
xslt.Transform(xmlLogFiles, null, streamWriter, null);
//flush and set the position to 0
streamWriter.Flush();
stream.Position = 0;
StreamReader streamReader = new StreamReader(stream);
xmlLogFiles.Load(streamReader);
streamReader.Close();
totalRecords = xmlLogFiles.DocumentElement.SelectNodes("logs/log").Count;
int totalPages;
totalPages = Convert.ToInt32(Math.Ceiling((double)(totalRecords / (double)pageSize)));
int lowNum;
int highNum;
lowNum = pageIndex * pageSize;
highNum = (pageIndex * pageSize) + pageSize;
if (highNum > totalRecords)
highNum = totalRecords;
LogInfoArray arrLog = new LogInfoArray();
foreach (XmlNode xmlNode in xmlLogFiles.DocumentElement.SelectNodes("logs/log[position()>=" + lowNum.ToString() + " and position()<" + highNum.ToString() + "]"))
{
LogInfo log = new LogInfo();
LogProperties logProp = new LogProperties();
if (xmlNode.Attributes["LogTypeKey"] != null)
{
log.LogTypeKey = Convert.ToString(xmlNode.Attributes["LogTypeKey"].Value);
}
if (xmlNode.Attributes["LogCreateDate"] != null)
{
log.LogCreateDate = Convert.ToDateTime(xmlNode.Attributes["LogCreateDate"].Value);
}
if (xmlNode.Attributes["LogCreateDateNum"] != null)
{
log.LogCreateDateNum = long.Parse(xmlNode.Attributes["LogCreateDateNum"].Value);
}
if (xmlNode.Attributes["LogGUID"] != null)
{
log.LogGUID = Convert.ToString(xmlNode.Attributes["LogGUID"].Value);
}
if (xmlNode.Attributes["LogUserID"] != null)
{
log.LogUserID = Convert.ToInt32(xmlNode.Attributes["LogUserID"].Value);
}
if (xmlNode.Attributes["LogUserName"] != null)
{
log.LogUserName = Convert.ToString(xmlNode.Attributes["LogUserName"].Value);
}
if (xmlNode.Attributes["LogPortalID"] != null)
{
log.LogPortalID = Convert.ToInt32(xmlNode.Attributes["LogPortalID"].Value);
}
if (xmlNode.Attributes["LogPortalName"] != null)
{
log.LogPortalName = Convert.ToString(xmlNode.Attributes["LogPortalName"].Value);
}
if (xmlNode.Attributes["LogFileID"] != null)
{
log.LogFileID = Convert.ToString(xmlNode.Attributes["LogFileID"].Value);
}
if (xmlNode.Attributes["LogServerName"] != null)
{
log.LogServerName = Convert.ToString(xmlNode.Attributes["LogServerName"].Value);
}
XmlNodeList xmlPropertyNodes;
xmlPropertyNodes = xmlNode.SelectNodes("properties/property");
string propertyName;
string propertyValue;
foreach (XmlNode propertyNode in xmlPropertyNodes)
{
LogDetailInfo logDetails = new LogDetailInfo();
propertyName = XmlUtils.GetNodeValue(propertyNode, "name", "");
if (propertyName == "logdetail")
{
XmlDocument xmlDetail = new XmlDocument();
xmlDetail.LoadXml(XmlUtils.GetNodeValue(propertyNode, "value", ""));
foreach (XmlNode childNode in xmlDetail.DocumentElement.ChildNodes)
{
if (!childNode.HasChildNodes)
{
propertyName = childNode.Name;
propertyValue = childNode.InnerText;
logProp.Add(new LogDetailInfo(propertyName, propertyValue));
}
}
}
else
{
propertyValue = XmlUtils.GetNodeValue(propertyNode, "value", "");
logProp.Add(new LogDetailInfo(propertyName, propertyValue));
}
}
log.LogProperties = logProp;
arrLog.Add(log);
}
return arrLog;
}
private LogTypeConfigInfo GetLogTypeConfig(string portalID, string logTypeKey)
{
//--------------------------------------------------------------
//First see if there is a log file specified
//for this log type and this PortalID
//--------------------------------------------------------------
XmlNode xmlLogTypeInfo = xmlConfigDoc.SelectSingleNode("/LogConfig/LogTypeConfig[@LogTypeKey=\'" + logTypeKey + "\' and @LogTypePortalID=\'" + portalID + "\']");
if (xmlLogTypeInfo != null)
{
return GetLogTypeConfigInfoFromXML(xmlLogTypeInfo);
}
//--------------------------------------------------------------
//There's no log file specified for this
//log type and PortalID, so check to see
//if there is a log file specified for
//all LogTypes for this PortalID
//--------------------------------------------------------------
xmlLogTypeInfo = xmlConfigDoc.SelectSingleNode("/LogConfig/LogTypeConfig[@LogTypeKey=\'*\' and @LogTypePortalID=\'" + portalID + "\']");
if (xmlLogTypeInfo != null)
{
return GetLogTypeConfigInfoFromXML(xmlLogTypeInfo);
}
//--------------------------------------------------------------
//No logfile has been found yet, so let's
//check if there is a logfile specified
//for this log type and all PortalIDs
//--------------------------------------------------------------
xmlLogTypeInfo = xmlConfigDoc.SelectSingleNode("/LogConfig/LogTypeConfig[@LogTypeKey=\'" + logTypeKey + "\' and @LogTypePortalID=\'*\']");
if (xmlLogTypeInfo != null)
{
return GetLogTypeConfigInfoFromXML(xmlLogTypeInfo);
}
//--------------------------------------------------------------
//This is a catch all...it gets the default log file name.
//--------------------------------------------------------------
xmlLogTypeInfo = xmlConfigDoc.SelectSingleNode("/LogConfig/LogTypeConfig[@LogTypeKey=\'*\' and @LogTypePortalID=\'*\']");
if (xmlLogTypeInfo != null)
{
return GetLogTypeConfigInfoFromXML(xmlLogTypeInfo);
}
return null; //@SV
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -