/// LogParser COM Input format sample
namespace MSUtil.LogQuery.Sample
{
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Xml;
using System.Xml.XPath;
public interface ILogParserInputContext
{
void OpenInput(string from);
int GetFieldCount();
string GetFieldName(int index);
int GetFieldType(int index);
bool ReadRecord();
object GetValue(int index);
void CloseInput(bool abort);
}
// TODO: Generate a unique GUID for your input format
/// XMLInputFormat for LogParser
[Guid("FBB03457-D80A-4d83-AB3A-2DF63779DDC8")]
public class XMLInputFormat : ILogParserInputContext
{
// For an XML file, such as books.xml, the following 'fields' are available:
// Author
// Price
// PubDate
XmlDocument xmlFile;
IEnumerator bookNodes;
ArrayList xmlFields;
#region LogField Class
private class LogField
{
string fieldName;
FieldType fieldType;
public LogField(string FieldName, FieldType FieldType)
{
fieldName = FieldName;
fieldType = FieldType;
}
public string FieldName
{
get { return fieldName; }
set { fieldName = value; }
}
public FieldType FieldType
{
get { return fieldType; }
set { fieldType = value; }
}
}
#endregion
/// XMLInputFormat constructor
public XMLInputFormat()
{
xmlFields = new ArrayList();
xmlFields.Add(new LogField("Author", FieldType.String));
xmlFields.Add(new LogField("Price", FieldType.Real));
xmlFields.Add(new LogField("PubDate", FieldType.Timestamp));
}
/// InputFormat.FieldType Enumeration
private enum FieldType
{
/// VT_I8
Integer = 1,
/// VT_R8
Real = 2,
/// VT_BSTR
String = 3,
/// VT_DATE or VT_I8 (UTC)
Timestamp = 4
}
/// Open a log file or resource
/// Entity to open that specified in the FROM statement
public void OpenInput(string from)
{
if( from != "" )
{
// Load the xml file
xmlFile = new XmlDocument();
xmlFile.Load(from);
XmlNode xmlNodeRoot = xmlFile.FirstChild;
XmlNode booksNode = xmlNodeRoot.NextSibling;
// Get an enumerator for the book nodes
XmlNodeList bookNodeList = booksNode.SelectNodes("book");
bookNodes = bookNodeList.GetEnumerator();
}
}
/// Return the total number of fields that are exported to logparser
/// Number of fields in entity
public int GetFieldCount()
{
return xmlFields.Count;
}
/// Return the name of the field. Method is called as many times as specified by GetFieldCount.
/// 0 based index of the field name to return
/// Field name
public string GetFieldName(int index)
{
LogField logfield = (LogField)xmlFields[index];
return logfield.FieldName;
}
/// Returns the type of the field. Method is called as many times as specified by GetFieldCount.
/// 0 based index of the field type to return
/// Field Type
public int GetFieldType(int index)
{
LogField logfield = (LogField)xmlFields[index];
return (int)logfield.FieldType;
}
/// Read a new record advancing the internal 'state' machine. This method is called until there are no more records to export.
/// TRUE if record was read, FALSE if there are no more records to read
public bool ReadRecord()
{
return bookNodes.MoveNext();
}
/// Must return the value of the last record 'read'
/// 0 based index of the field value to read
/// Value of the field. Can also return null.
public object GetValue(int index)
{
LogField lf = (LogField)xmlFields[index];
// Look each value in the XML file
XmlNode bookNode = (XmlNode)bookNodes.Current;
if (String.Compare(lf.FieldName, "Author") == 0)
{
XmlNode bookDetailsNode = bookNode.SelectSingleNode("author");
return bookDetailsNode.InnerText;
}
else if (String.Compare(lf.FieldName, "Price") == 0)
{
XmlNode bookDetailsNode = bookNode.SelectSingleNode("price");
Double price = Double.Parse(bookDetailsNode.InnerText);
return price;
}
else if (String.Compare(lf.FieldName, "PubDate") == 0)
{
XmlNode bookDetailsNode = bookNode.SelectSingleNode("pubdate");
DateTime dt = DateTime.Parse(bookDetailsNode.InnerText);
return dt;
}
return null;
}
/// Close log file or resource
/// TRUE if execution is terminated by abnormal situation, such as Ctrl+C
public void CloseInput(bool abort)
{
}
}
}