NewMom2Brandon
asked on
Help I am pulling hair out!!!! Stuck with Arraylists
I have two problem areas which I marked below. The problem I am having is when searching my document for a certain phrase i.e "Search For Me". If it is found twice in the document it does the "iXMLErrorMsgIndex++;" setting it to 2. However the arraylist collections are showing 1. What can I do to solve this. I have tried so many things over 3 days and I am still having issues.
Thank you for you help
//Check to see if the file exists
if (File.Exists(sDirectory + sCurrentFileName))
{
// Save any messages found in the error log.
ArrayList CurrentErrorLogDateAL = new ArrayList();
//Initialize the array lists containing the messages found in the errorlog.
//Current Error Log File Date
XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel ectSingleN ode(TAGS.E rrorLogFil eDate);
CurrentErrorLogDateAL.Add( XmlNodeMes sageChild. InnerText) ;
// Get timestamp of Current errorlog file.
DateTime dCurrentErrorLogFileDate = File.GetLastWriteTime(sDir ectory + sCurrentFileName);
String sCurrentErrorLogFileDate = dCurrentErrorLogFileDate.T oString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
dCurrentErrorLogFileDate = Convert.ToDateTime(sCurren tErrorLogF ileDate).D ate;
if ((dCurrentErrorLogFileDate == dDayBefore) || (dCurrentErrorLogFileDate == dToday))
{
if (dDateTimeCurrent > dRunLast)
{
foreach (string sLogMsgLookingFor in LogMsgLookingFor)
{
bool bFound = false;
//open the file for reading
StreamReader objReader = new StreamReader(sDirectory + sCurrentFileName);
string sCurrentLine = objReader.ReadLine();
string sPreviousLine = "";
************************** ********** ********** ********** ********** ********** ******
Should I really terminate this while loop if the log message you are looking for has been found?
see next note). Could this be part of the index problem.? "bFound = true;
************************** ********** ********** ********** ********** ********** ******
while(sCurrentLine != null)
{
if(sCurrentLine.IndexOf(sL ogMsgLooki ngFor) > -1)
{
if (sPreviousLine != null)
{
bFound = true;
}
}
if (bFound == true)
{
//Change previous line to when even happened time
DateTime dErrorTime = Convert.ToDateTime(sPrevio usLine).Da te;
DateTime dPreviousLine = Convert.ToDateTime(sPrevio usLine);
String sErrorDateTime = dPreviousLine.ToString("dd -MMM-yyyy HH:mm:ss");
//Get the LogErrorTime from XML File
XmlNode xmlLogErrorTime = xmlNodeErrorLogMessage.Sel ectSingleN ode(TAGS.L ogErrorTim e);
DateTime dLogErrorTime = Convert.ToDateTime(xmlLogE rrorTime.I nnerText);
String sLogErrorTime = dLogErrorTime.ToString("dd -MMM-yyyy HH:mm:ss");
//Set the LastFoundTime to today in xml file.
XmlNode xmlNodeLastFoundTime = xmlNodeErrorLogMessage.Sel ectSingleN ode (TAGS.LastFoundTime);
xmlNodeLastFoundTime.Inner Text = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
************************** ********** ********** ********** ********** ********** ******
if bFound == true twice (because "sLogMsgLookingFor" is in errorlog.txt twice), then iXMLErrorMsgIndex will = 2, and LastFoundTimeAL.Count will still = 1, causing the else block to be executed. I believe this is where the index error is happening. I don't know if it is happening else where since it stops here
************************** ********** ********** ********** ********** ********** ******
if(iXMLErrorMsgIndex < LastFoundTimeAL.Count)
{
//Upgrade the array lists.
LastFoundTimeAL[iXMLErrorM sgIndex] = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
else
{
xmlError.InnerText =
CONSTANTS.ERRORLOG_INDEX_O UT_OF_RANG E + "LastFoundTimeAL";
xmlErrorTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
if ((dErrorTime == dToday) && (sErrorDateTime != sLogErrorTime))
{
//Change from string to datetime to use
DateTime dTimeFromLog = Convert.ToDateTime(sPrevio usLine);
//Set the LogErrorTime to what was found in the errlog.txt file for the error.
xmlLogErrorTime = xmlNodeErrorLogMessage.Sel ectSingleN ode(TAGS.L ogErrorTim e);
xmlLogErrorTime.InnerText = dTimeFromLog.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
//Set LastErrorFoundIn to sCurrentFileName
XmlNode xmlLastErrorFoundIn = xmlNodeErrorLogMessage.Sel ectSingleN ode
(TAGS.LastErrorFoundIn);
xmlLastErrorFoundIn.InnerT ext = sCurrentFileName;
//Set LastSentTime to current date time.
XmlNode xmlLastSentTime = xmlNodeErrorLogMessage.Sel ectSingleN ode
(TAGS.LastSentTime);
xmlLastSentTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
//Set ErrorLogFileDate in XML
XmlNode xmlErrorLogFileDate = xmlNodeErrorLogMessage.Sel ectSingleN ode
(TAGS.ErrorLogFileDate);
xmlErrorLogFileDate.InnerT ext = sCurrentErrorLogFileDate;
if(iXMLErrorMsgIndex < LastSentTimeAL.Count)
{
// Upgrade the array lists.
LastSentTimeAL[iXMLErrorMs gIndex] = dDateTimeCurrent.ToString( );
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O UT_OF_RANG E
+ "LastSentTimeAL";
xmlMessageErrorTime.InnerT ext = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
if(iXMLErrorMsgIndex < LogErrorTimeAL.Count)
{
// Upgrade the array lists.
LogErrorTimeAL[iXMLErrorMs gIndex] = dTimeFromLog.ToString();
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O UT_OF_RANG E + "LogErrorTimeAL";
xmlMessageErrorTime.InnerT ext = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
if(iXMLErrorMsgIndex < LastErrorFoundInAL.Count)
{
// Upgrade the array lists.
LastErrorFoundInAL[iXMLErr orMsgIndex ] = sCurrentFileName;
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O UT_OF_RANG E
+ "LastErrorFoundInAL";
xmlMessageErrorTime.InnerT ext = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
if(iXMLErrorMsgIndex < CurrentErrorLogDateAL.Coun t)
{ // Upgrade the array lists.
CurrentErrorLogDateAL[iXML ErrorMsgIn dex] = dCurrentErrorLogFileDate.T oString();
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O UT_OF_RANG E
+ "CurrentErrorLogDateAL";
xmlMessageErrorTime.InnerT ext = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
sDetails[0] = CONSTANTS.ERRORLOG_ERROR_F OUND;
sValues[0] = "Error: " + sCurrentLine + " Error Occured at: " + sPreviousLine;
iSeverityLevel = SEVERITYLEVELS.WARNING;
//Update index into the array list.
iXMLErrorMsgIndex++;
}
else if (sErrorDateTime == sLogErrorTime)
{
xmlError.InnerText = CONSTANTS.NO_NEW_ERRORLOG_ ERRORS;
xmlErrorTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
//Sending an empty string so Client does not send a message
sValues[0] = CONSTANTS.EMPTY_STRING;
}
}
sPreviousLine = sCurrentLine;
bFound = false;
sCurrentLine = objReader.ReadLine();
}//end while loop
objReader.Close();
}
}
}
}
else
{
xmlError.InnerText = ERROR.ERRORLOG_FILE_NOT_FO UND;
xmlErrorTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
Thank you for you help
//Check to see if the file exists
if (File.Exists(sDirectory + sCurrentFileName))
{
// Save any messages found in the error log.
ArrayList CurrentErrorLogDateAL = new ArrayList();
//Initialize the array lists containing the messages found in the errorlog.
//Current Error Log File Date
XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel
CurrentErrorLogDateAL.Add(
// Get timestamp of Current errorlog file.
DateTime dCurrentErrorLogFileDate = File.GetLastWriteTime(sDir
String sCurrentErrorLogFileDate = dCurrentErrorLogFileDate.T
("s",System.Globalization.
dCurrentErrorLogFileDate = Convert.ToDateTime(sCurren
if ((dCurrentErrorLogFileDate
{
if (dDateTimeCurrent > dRunLast)
{
foreach (string sLogMsgLookingFor in LogMsgLookingFor)
{
bool bFound = false;
//open the file for reading
StreamReader objReader = new StreamReader(sDirectory + sCurrentFileName);
string sCurrentLine = objReader.ReadLine();
string sPreviousLine = "";
**************************
Should I really terminate this while loop if the log message you are looking for has been found?
see next note). Could this be part of the index problem.? "bFound = true;
**************************
while(sCurrentLine != null)
{
if(sCurrentLine.IndexOf(sL
{
if (sPreviousLine != null)
{
bFound = true;
}
}
if (bFound == true)
{
//Change previous line to when even happened time
DateTime dErrorTime = Convert.ToDateTime(sPrevio
DateTime dPreviousLine = Convert.ToDateTime(sPrevio
String sErrorDateTime = dPreviousLine.ToString("dd
//Get the LogErrorTime from XML File
XmlNode xmlLogErrorTime = xmlNodeErrorLogMessage.Sel
DateTime dLogErrorTime = Convert.ToDateTime(xmlLogE
String sLogErrorTime = dLogErrorTime.ToString("dd
//Set the LastFoundTime to today in xml file.
XmlNode xmlNodeLastFoundTime = xmlNodeErrorLogMessage.Sel
xmlNodeLastFoundTime.Inner
("s",System.Globalization.
**************************
if bFound == true twice (because "sLogMsgLookingFor" is in errorlog.txt twice), then iXMLErrorMsgIndex will = 2, and LastFoundTimeAL.Count will still = 1, causing the else block to be executed. I believe this is where the index error is happening. I don't know if it is happening else where since it stops here
**************************
if(iXMLErrorMsgIndex < LastFoundTimeAL.Count)
{
//Upgrade the array lists.
LastFoundTimeAL[iXMLErrorM
("s",System.Globalization.
}
else
{
xmlError.InnerText =
CONSTANTS.ERRORLOG_INDEX_O
xmlErrorTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization.
}
if ((dErrorTime == dToday) && (sErrorDateTime != sLogErrorTime))
{
//Change from string to datetime to use
DateTime dTimeFromLog = Convert.ToDateTime(sPrevio
//Set the LogErrorTime to what was found in the errlog.txt file for the error.
xmlLogErrorTime = xmlNodeErrorLogMessage.Sel
xmlLogErrorTime.InnerText = dTimeFromLog.ToString
("s",System.Globalization.
//Set LastErrorFoundIn to sCurrentFileName
XmlNode xmlLastErrorFoundIn = xmlNodeErrorLogMessage.Sel
(TAGS.LastErrorFoundIn);
xmlLastErrorFoundIn.InnerT
//Set LastSentTime to current date time.
XmlNode xmlLastSentTime = xmlNodeErrorLogMessage.Sel
(TAGS.LastSentTime);
xmlLastSentTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization.
//Set ErrorLogFileDate in XML
XmlNode xmlErrorLogFileDate = xmlNodeErrorLogMessage.Sel
(TAGS.ErrorLogFileDate);
xmlErrorLogFileDate.InnerT
if(iXMLErrorMsgIndex < LastSentTimeAL.Count)
{
// Upgrade the array lists.
LastSentTimeAL[iXMLErrorMs
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O
+ "LastSentTimeAL";
xmlMessageErrorTime.InnerT
("s",System.Globalization.
}
if(iXMLErrorMsgIndex < LogErrorTimeAL.Count)
{
// Upgrade the array lists.
LogErrorTimeAL[iXMLErrorMs
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O
xmlMessageErrorTime.InnerT
("s",System.Globalization.
}
if(iXMLErrorMsgIndex < LastErrorFoundInAL.Count)
{
// Upgrade the array lists.
LastErrorFoundInAL[iXMLErr
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O
+ "LastErrorFoundInAL";
xmlMessageErrorTime.InnerT
("s",System.Globalization.
}
if(iXMLErrorMsgIndex < CurrentErrorLogDateAL.Coun
{ // Upgrade the array lists.
CurrentErrorLogDateAL[iXML
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O
+ "CurrentErrorLogDateAL";
xmlMessageErrorTime.InnerT
("s",System.Globalization.
}
sDetails[0] = CONSTANTS.ERRORLOG_ERROR_F
sValues[0] = "Error: " + sCurrentLine + " Error Occured at: " + sPreviousLine;
iSeverityLevel = SEVERITYLEVELS.WARNING;
//Update index into the array list.
iXMLErrorMsgIndex++;
}
else if (sErrorDateTime == sLogErrorTime)
{
xmlError.InnerText = CONSTANTS.NO_NEW_ERRORLOG_
xmlErrorTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization.
//Sending an empty string so Client does not send a message
sValues[0] = CONSTANTS.EMPTY_STRING;
}
}
sPreviousLine = sCurrentLine;
bFound = false;
sCurrentLine = objReader.ReadLine();
}//end while loop
objReader.Close();
}
}
}
}
else
{
xmlError.InnerText = ERROR.ERRORLOG_FILE_NOT_FO
xmlErrorTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization.
}
ASKER
I am looking in an error log for a message that is defined in a XML file.
Believe me initially this was a short easy program...until the arraylist stuff had to be added. I am willing to try any other way.
the errlog sample would be this
___
June 1, 2005 1:24:31 PM
MESSAGE I AM SEARCHING FOR
line here
line here
line here
___
June 1, 2005 1:24:31 PM
Additional Message
line here
line here
line here
___
June 1, 2005 3:24:31 PM
MESSAGE I AM LOOKING FOR (second occurance possible)
line here
line here
line here
___
XML file is this
<Type>ErrorLog Reader</Type>
<RunEvery>1</RunEvery>
<RunSpan>Hour</RunSpan>
<RunLast>2001-01-01T00:00: 00</RunLas t>
<LastSentTime>2001-01-01T0 0:00:00</L astSentTim e>
<!--THIS IS THE FILE INFORMATION -->
<Directory>c:\\</Directory >
<FileName>errlog.txt</File Name>
<LastErrorTime>2001-01-01T 00:00:00</ LastErrorT ime>
<LastError></LastError>
<ErrorLogMessage>
<!-- Just add another whole ErrorLogMessage block
and put the next MessageId consecutive i.e
next would be 2..3..4 and so on.-->
<MessageId>1</MessageId>
<LogMsgLookingFor>Message I am Searching For</LogMsgLookingFor>
<LastFoundTime>2001-01-01T 00:00:00</ LastFoundT ime>
<LogErrorTime>2001-01-01T0 0:00:00</L ogErrorTim e>
<LastErrorFoundIn>errlog.t xt</LastEr rorFoundIn >
<ErrorLogFileDate>2001-01- 01T00:00:0 0</ErrorLo gFileDate>
<LastMessageErrorTime>2001 -01-01T00: 00:00</Las tMessageEr rorTime>
<LastMessageError>
</LastMessageError>
</ErrorLogMessage>
</Task>
</TaskConfiguration>
Believe me initially this was a short easy program...until the arraylist stuff had to be added. I am willing to try any other way.
the errlog sample would be this
___
June 1, 2005 1:24:31 PM
MESSAGE I AM SEARCHING FOR
line here
line here
line here
___
June 1, 2005 1:24:31 PM
Additional Message
line here
line here
line here
___
June 1, 2005 3:24:31 PM
MESSAGE I AM LOOKING FOR (second occurance possible)
line here
line here
line here
___
XML file is this
<Type>ErrorLog Reader</Type>
<RunEvery>1</RunEvery>
<RunSpan>Hour</RunSpan>
<RunLast>2001-01-01T00:00:
<LastSentTime>2001-01-01T0
<!--THIS IS THE FILE INFORMATION -->
<Directory>c:\\</Directory
<FileName>errlog.txt</File
<LastErrorTime>2001-01-01T
<LastError></LastError>
<ErrorLogMessage>
<!-- Just add another whole ErrorLogMessage block
and put the next MessageId consecutive i.e
next would be 2..3..4 and so on.-->
<MessageId>1</MessageId>
<LogMsgLookingFor>Message I am Searching For</LogMsgLookingFor>
<LastFoundTime>2001-01-01T
<LogErrorTime>2001-01-01T0
<LastErrorFoundIn>errlog.t
<ErrorLogFileDate>2001-01-
<LastMessageErrorTime>2001
<LastMessageError>
</LastMessageError>
</ErrorLogMessage>
</Task>
</TaskConfiguration>
ASKER
there is a potential for there to be two of the messages that I am searching for to be in the file with a small time difference between them say about a half hour.
So I only want to send 1 message out but to make sure the message that goes out is the one with the most recent occurance.
So I only want to send 1 message out but to make sure the message that goes out is the one with the most recent occurance.
Hmm i wrote a search program once.
I discovered that the "filename" functions contained the pathname as well. The Directory functions only had the the path name.
e.g
Directory: c:\yahoo\spam
File: C:\yahoo\spam\porn.jpg
Maybe this is a little too simply, but do you think you can simply create an array of strings to "house" each error code and then search through the string array?
also is LastFoundTimeAL.Count a global variable? static?(if not maybe you shoudl make it that way)
The last peice i can offer right now is to just go ahead and scrap everything you have done and start over...maybe you will know what you did wrong :)
I discovered that the "filename" functions contained the pathname as well. The Directory functions only had the the path name.
e.g
Directory: c:\yahoo\spam
File: C:\yahoo\spam\porn.jpg
Maybe this is a little too simply, but do you think you can simply create an array of strings to "house" each error code and then search through the string array?
also is LastFoundTimeAL.Count a global variable? static?(if not maybe you shoudl make it that way)
The last peice i can offer right now is to just go ahead and scrap everything you have done and start over...maybe you will know what you did wrong :)
In all that code, is there something that I am missing? Are you just reading in a log file, and searching for a particular phrase? Or, are you looking for more information?
Bob
Bob
ASKER
I am just reading in a text file. Searching for the one phrase that was entered via the XML file. Then once that line is found I am also pulling the line before it. The line before it contains the date and time that the error happend.
Prior to adding the Arraylist stuff. The code functioned correctly. It read the file and pulled the line before it. Everything went haywire the minute I added the Arraylist stuff. I don't know if maybe there is a way to do this with out adding the Arraylist stuff. The code still reads the errorlog the way it should I am just getting a exception...index must be non-negative and larger than the size of the collection.
See the way the code needs to function would be like this....
Right now every hour the code would run and check to see if the errorlog was written to. If it was then it would open the file and search for the "LogMsgLookingFor" (defined in the XML) say it would be "Broken Part A". As soon as it finds it then it would fill in XML detail listed below(****FILL IN THIS ONE). Then it would move on to the second message to search for "BROKEN PART B".
<ErrorLogMessage>
<!-- Just add another whole ErrorLogMessage block
and put the next MessageId consecutive i.e
next would be 2..3..4 and so on.-->
<MessageId>1</MessageId>
<LogMsgLookingFor>BROKEN PART A</LogMsgLookingFor>
<LastFoundTime>2001-01-01T 00:00:00</ LastFoundT ime>****FI LL IN THIS ONE
<LogErrorTime>2001-01-01T0 0:00:00</L ogErrorTim e>****FILL IN THIS ONE
<LastErrorFoundIn>errlog.t xt</LastEr rorFoundIn >****FILL IN THIS ONE
<ErrorLogFileDate>2001-01- 01T00:00:0 0</ErrorLo gFileDate> ****FILL IN THIS ONE
<LastMessageErrorTime>2001 -01-01T00: 00:00</Las tMessageEr rorTime>
<LastMessageError>
</LastMessageError>
</ErrorLogMessage>
*****SECOND MESSAGE TO LOOK FOR
<ErrorLogMessage>
<!-- Just add another whole ErrorLogMessage block
and put the next MessageId consecutive i.e
next would be 2..3..4 and so on.-->
<MessageId>1</MessageId>
<LogMsgLookingFor>BROKEN PART B</LogMsgLookingFor>
<LastFoundTime>2001-01-01T 00:00:00</ LastFoundT ime>****FI LL IN THIS ONE
<LogErrorTime>2001-01-01T0 0:00:00</L ogErrorTim e>****FILL IN THIS ONE
<LastErrorFoundIn>errlog.t xt</LastEr rorFoundIn >****FILL IN THIS ONE
<ErrorLogFileDate>2001-01- 01T00:00:0 0</ErrorLo gFileDate> ****FILL IN THIS ONE
<LastMessageErrorTime>2001 -01-01T00: 00:00</Las tMessageEr rorTime>
<LastMessageError>
</LastMessageError>
</ErrorLogMessage>
The problem I am running into is say BROKEN PART A is listed 2 times in the errorlog like this
___
June 2, 2005 1:24:31 PM
BROKEN PART A
line here
line here
line here
___
June 2, 2005 1:50:31 PM
BROKEN PART A (second occurance possible)
line here
line here
line here
___
The only one I am really concerned about is the second one "June 2, 2005 1:50:31 PM" This is the one that needs to be used. So the June 2nd date and time would be placed in the XML file and an email would be sent.
I put these in just to find out where the code was having problems which pin pointed to the LastFountTimeAL.Count. But I have a feeling it would stop at every one but since the first one was LastFoundTime it stops there.
if(iXMLErrorMsgIndex < LastSentTimeAL.Count)
{
// Upgrade the array lists.
LastSentTimeAL[iXMLErrorMs gIndex] = dDateTimeCurrent.ToString( );
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O UT_OF_RANG E
+ "LastSentTimeAL";
xmlMessageErrorTime.InnerT ext = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
Prior to adding the Arraylist stuff. The code functioned correctly. It read the file and pulled the line before it. Everything went haywire the minute I added the Arraylist stuff. I don't know if maybe there is a way to do this with out adding the Arraylist stuff. The code still reads the errorlog the way it should I am just getting a exception...index must be non-negative and larger than the size of the collection.
See the way the code needs to function would be like this....
Right now every hour the code would run and check to see if the errorlog was written to. If it was then it would open the file and search for the "LogMsgLookingFor" (defined in the XML) say it would be "Broken Part A". As soon as it finds it then it would fill in XML detail listed below(****FILL IN THIS ONE). Then it would move on to the second message to search for "BROKEN PART B".
<ErrorLogMessage>
<!-- Just add another whole ErrorLogMessage block
and put the next MessageId consecutive i.e
next would be 2..3..4 and so on.-->
<MessageId>1</MessageId>
<LogMsgLookingFor>BROKEN PART A</LogMsgLookingFor>
<LastFoundTime>2001-01-01T
<LogErrorTime>2001-01-01T0
<LastErrorFoundIn>errlog.t
<ErrorLogFileDate>2001-01-
<LastMessageErrorTime>2001
<LastMessageError>
</LastMessageError>
</ErrorLogMessage>
*****SECOND MESSAGE TO LOOK FOR
<ErrorLogMessage>
<!-- Just add another whole ErrorLogMessage block
and put the next MessageId consecutive i.e
next would be 2..3..4 and so on.-->
<MessageId>1</MessageId>
<LogMsgLookingFor>BROKEN PART B</LogMsgLookingFor>
<LastFoundTime>2001-01-01T
<LogErrorTime>2001-01-01T0
<LastErrorFoundIn>errlog.t
<ErrorLogFileDate>2001-01-
<LastMessageErrorTime>2001
<LastMessageError>
</LastMessageError>
</ErrorLogMessage>
The problem I am running into is say BROKEN PART A is listed 2 times in the errorlog like this
___
June 2, 2005 1:24:31 PM
BROKEN PART A
line here
line here
line here
___
June 2, 2005 1:50:31 PM
BROKEN PART A (second occurance possible)
line here
line here
line here
___
The only one I am really concerned about is the second one "June 2, 2005 1:50:31 PM" This is the one that needs to be used. So the June 2nd date and time would be placed in the XML file and an email would be sent.
I put these in just to find out where the code was having problems which pin pointed to the LastFountTimeAL.Count. But I have a feeling it would stop at every one but since the first one was LastFoundTime it stops there.
if(iXMLErrorMsgIndex < LastSentTimeAL.Count)
{
// Upgrade the array lists.
LastSentTimeAL[iXMLErrorMs
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O
+ "LastSentTimeAL";
xmlMessageErrorTime.InnerT
("s",System.Globalization.
}
Is this a console application?
If it is please post ALL of the code you have.
If it is not a console app, go ahead and post ALL the code you have and also a little text at the end stating that you need Listbox1 named mylist..ect..
and after that post an example error log.
If it is please post ALL of the code you have.
If it is not a console app, go ahead and post ALL the code you have and also a little text at the end stating that you need Listbox1 named mylist..ect..
and after that post an example error log.
I don't see where iXMLErrorMsgIndex is defined or initialized.
Bob
Bob
ASKER
I can not post all of the code...since it is a windows service and has multiple files but I can post the whole .cs file for this task..
There is an example of how the errorlog is set up listed in a previous post. It is took big of a file for me to put in here since it is constantly being written too.
Sorry if the alignment is not right. I added the code to the orginal post.
ErrorLogReader.cs
using System;
using System.Globalization;
using System.Xml;
using System.IO;
using System.Collections;
using Microsoft.Win32;
using QTI.Monitor.Configuration;
using System.Windows.Forms;
namespace QTI.Monitor.Tasks
{
public class TaskErrorLogReader : TaskInterface
{
public void DoTask(XmlNode xmlNode, ref int iSeverityLevel,
ref String[] sValues, ref String[] sDetails)
{
string sCurrentFileName = CONSTANTS.NULL;
string sOldFileName = CONSTANTS.NULL;
string sDirectory = CONSTANTS.NULL;
//Sending an empty string so Client does not send a message
sValues[0] = CONSTANTS.EMPTY_STRING;
//Today's date for comparisons
System.DateTime dToday = System.DateTime.Today.Date ;
//Yesterday's date for comparisons
System.DateTime dDayBefore = dToday.AddDays(-1);
//Used to update the Date/Time in XML
DateTime dDateTimeCurrent = DateTime.Now;
//Get the RunLast from XML File
XmlNode xmlRunLast = xmlNode.SelectSingleNode(T AGS.RunLas t);
DateTime dRunLast = Convert.ToDateTime(xmlRunL ast.InnerT ext).Date;
// Get directory from XML File
XmlNode xmlDirectory = xmlNode.SelectSingleNode(T AGS.Direct ory);
sDirectory = xmlDirectory.InnerText;
//Get CurrentFileName (errorlog.txt) from XML File
XmlNode xmlCurrentFileName = xmlNode.SelectSingleNode(T AGS.FileNa me);
sCurrentFileName = xmlCurrentFileName.InnerTe xt;
//Get OldFileName (errorlog.txt.old) from XML File
XmlNode xmlOldFileName = xmlNode.SelectSingleNode(T AGS.OldFil eName);
sOldFileName = xmlOldFileName.InnerText;
//Get Message Error node from XML file
XmlNode xmlMessageError = xmlNode.SelectSingleNode(T AGS.LastMe ssageError );
//Get the Message Error Time node from XML file
XmlNode xmlMessageErrorTime = xmlNode.SelectSingleNode (TAGS.LastMessageErrorTime );
//Get Error node from XML file
XmlNode xmlError = xmlNode.SelectSingleNode(T AGS.LastEr ror);
//Get the Error Time node from XML file
XmlNode xmlErrorTime = xmlNode.SelectSingleNode(T AGS.LastEr rorTime);
try
{
// Save any messages found in the error log.
ArrayList LastFoundTimeAL = new ArrayList();
ArrayList LastSentTimeAL = new ArrayList();
ArrayList LogErrorTimeAL = new ArrayList();
ArrayList LastErrorFoundInAL = new ArrayList();
int iXMLErrorMsgIndex = 0;
// Loop through all errorlog messages,saving any messages that are found in an array.
XmlNodeList nodeListErrorMessages = xmlNode.SelectNodes(TAGS.E rrorLogMes sage);
foreach (XmlNode xmlNodeErrorLogMessage in nodeListErrorMessages)
{
// Message Id.
XmlNode XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel ectSingleN ode(TAGS.M essageId);
double dXMLMessageId = 0;
if (XmlNodeMessageChild.Inner Text.Trim( ).Length > 0)
{
dXMLMessageId = Convert.ToDouble(XmlNodeMe ssageChild .InnerText );
}
// List for the Log message source.
ArrayList LogMsgLookingFor = new ArrayList();
XmlNodeList nodeListLogMsgLookingFor = xmlNodeErrorLogMessage.Sel ectNodes
(TAGS.LogMsgLookingFor);
foreach (XmlNode NodeLogMsgLookingFor in nodeListLogMsgLookingFor)
{
LogMsgLookingFor.Add(NodeL ogMsgLooki ngFor.Inne rText);
}
//Initialize the array lists containing the messages found in the errorlog.
//Last Found Time.
XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel ectSingleN ode(TAGS.L astFoundTi me);
LastFoundTimeAL.Add(XmlNod eMessageCh ild.InnerT ext);
//Last Sent Time
XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel ectSingleN ode(TAGS.L astSentTim e);
LastSentTimeAL.Add(XmlNode MessageChi ld.InnerTe xt);
//Log Error Time
XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel ectSingleN ode(TAGS.L ogErrorTim e);
LogErrorTimeAL.Add(XmlNode MessageChi ld.InnerTe xt);
//Last Error Found In
XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel ectSingleN ode(TAGS.L astErrorFo undIn);
LastErrorFoundInAL.Add(Xml NodeMessag eChild.Inn erText);
//Check to see if the file exists
if (File.Exists(sDirectory + sCurrentFileName))
{
// Save any messages found in the error log.
ArrayList CurrentErrorLogDateAL = new ArrayList();
//Initialize the array lists containing the messages found in the errorlog.
//Current Error Log File Date
XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel ectSingleN ode(TAGS.E rrorLogFil eDate);
CurrentErrorLogDateAL.Add( XmlNodeMes sageChild. InnerText) ;
// Get timestamp of Current errorlog file.
DateTime dCurrentErrorLogFileDate = File.GetLastWriteTime(sDir ectory + sCurrentFileName);
String sCurrentErrorLogFileDate = dCurrentErrorLogFileDate.T oString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
dCurrentErrorLogFileDate = Convert.ToDateTime(sCurren tErrorLogF ileDate).D ate;
if ((dCurrentErrorLogFileDate == dDayBefore) || (dCurrentErrorLogFileDate == dToday))
{
if (dDateTimeCurrent > dRunLast)
{
foreach (string sLogMsgLookingFor in LogMsgLookingFor)
{
bool bFound = false;
//open the file for reading
StreamReader objReader = new StreamReader(sDirectory + sCurrentFileName);
string sCurrentLine = objReader.ReadLine();
string sPreviousLine = "";
************************** ********** ********** ********** ********** ********** ******
Should I really terminate this while loop if the log message you are looking for has been found?
see next note). Could this be part of the index problem.? "bFound = true;
************************** ********** ********** ********** ********** ********** ******
while(sCurrentLine != null)
{
if(sCurrentLine.IndexOf(sL ogMsgLooki ngFor) > -1)
{
if (sPreviousLine != null)
{
bFound = true;
}
}
if (bFound == true)
{
//Change previous line to when even happened time
DateTime dErrorTime = Convert.ToDateTime(sPrevio usLine).Da te;
DateTime dPreviousLine = Convert.ToDateTime(sPrevio usLine);
String sErrorDateTime = dPreviousLine.ToString("dd -MMM-yyyy HH:mm:ss");
//Get the LogErrorTime from XML File
XmlNode xmlLogErrorTime = xmlNodeErrorLogMessage.Sel ectSingleN ode(TAGS.L ogErrorTim e);
DateTime dLogErrorTime = Convert.ToDateTime(xmlLogE rrorTime.I nnerText);
String sLogErrorTime = dLogErrorTime.ToString("dd -MMM-yyyy HH:mm:ss");
//Set the LastFoundTime to today in xml file.
XmlNode xmlNodeLastFoundTime = xmlNodeErrorLogMessage.Sel ectSingleN ode (TAGS.LastFoundTime);
xmlNodeLastFoundTime.Inner Text = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
************************** ********** ********** ********** ********** ********** ******
if bFound == true twice (because "sLogMsgLookingFor" is in errorlog.txt twice), then iXMLErrorMsgIndex will = 2, and LastFoundTimeAL.Count will still = 1, causing the else block to be executed. I believe this is where the index error is happening. I don't know if it is happening else where since it stops here
************************** ********** ********** ********** ********** ********** ******
if(iXMLErrorMsgIndex < LastFoundTimeAL.Count)
{
//Upgrade the array lists.
LastFoundTimeAL[iXMLErrorM sgIndex] = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
else
{
xmlError.InnerText =
CONSTANTS.ERRORLOG_INDEX_O UT_OF_RANG E + "LastFoundTimeAL";
xmlErrorTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
if ((dErrorTime == dToday) && (sErrorDateTime != sLogErrorTime))
{
//Change from string to datetime to use
DateTime dTimeFromLog = Convert.ToDateTime(sPrevio usLine);
//Set the LogErrorTime to what was found in the errlog.txt file for the error.
xmlLogErrorTime = xmlNodeErrorLogMessage.Sel ectSingleN ode(TAGS.L ogErrorTim e);
xmlLogErrorTime.InnerText = dTimeFromLog.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
//Set LastErrorFoundIn to sCurrentFileName
XmlNode xmlLastErrorFoundIn = xmlNodeErrorLogMessage.Sel ectSingleN ode
(TAGS.LastErrorFoundIn);
xmlLastErrorFoundIn.InnerT ext = sCurrentFileName;
//Set LastSentTime to current date time.
XmlNode xmlLastSentTime = xmlNodeErrorLogMessage.Sel ectSingleN ode
(TAGS.LastSentTime);
xmlLastSentTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
//Set ErrorLogFileDate in XML
XmlNode xmlErrorLogFileDate = xmlNodeErrorLogMessage.Sel ectSingleN ode
(TAGS.ErrorLogFileDate);
xmlErrorLogFileDate.InnerT ext = sCurrentErrorLogFileDate;
if(iXMLErrorMsgIndex < LastSentTimeAL.Count)
{
// Upgrade the array lists.
LastSentTimeAL[iXMLErrorMs gIndex] = dDateTimeCurrent.ToString( );
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O UT_OF_RANG E
+ "LastSentTimeAL";
xmlMessageErrorTime.InnerT ext = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
if(iXMLErrorMsgIndex < LogErrorTimeAL.Count)
{
// Upgrade the array lists.
LogErrorTimeAL[iXMLErrorMs gIndex] = dTimeFromLog.ToString();
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O UT_OF_RANG E + "LogErrorTimeAL";
xmlMessageErrorTime.InnerT ext = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
if(iXMLErrorMsgIndex < LastErrorFoundInAL.Count)
{
// Upgrade the array lists.
LastErrorFoundInAL[iXMLErr orMsgIndex ] = sCurrentFileName;
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O UT_OF_RANG E
+ "LastErrorFoundInAL";
xmlMessageErrorTime.InnerT ext = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
if(iXMLErrorMsgIndex < CurrentErrorLogDateAL.Coun t)
{ // Upgrade the array lists.
CurrentErrorLogDateAL[iXML ErrorMsgIn dex] = dCurrentErrorLogFileDate.T oString();
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O UT_OF_RANG E
+ "CurrentErrorLogDateAL";
xmlMessageErrorTime.InnerT ext = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
sDetails[0] = CONSTANTS.ERRORLOG_ERROR_F OUND;
sValues[0] = "Error: " + sCurrentLine + " Error Occured at: " + sPreviousLine;
iSeverityLevel = SEVERITYLEVELS.WARNING;
//Update index into the array list.
iXMLErrorMsgIndex++;
}
else if (sErrorDateTime == sLogErrorTime)
{
xmlError.InnerText = CONSTANTS.NO_NEW_ERRORLOG_ ERRORS;
xmlErrorTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
//Sending an empty string so Client does not send a message
sValues[0] = CONSTANTS.EMPTY_STRING;
}
}
sPreviousLine = sCurrentLine;
bFound = false;
sCurrentLine = objReader.ReadLine();
}//end while loop
objReader.Close();
}
}
}
}
else
{
xmlError.InnerText = ERROR.ERRORLOG_FILE_NOT_FO UND;
xmlErrorTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization. DateTimeFo rmatInfo.I nvariantIn fo);
}
catch (Exception e)
{
System.Diagnostics.EventLo g.WriteEnt ry(this.To String(),
ERROR.FAILED_TO_READ_ERROR _LOG + e.ToString(),
System.Diagnostics.EventLo gEntryType .Error);
sValues[0] = ERROR.FAILED_TO_READ_ERROR _LOG;
iSeverityLevel = SEVERITYLEVELS.INFORMATION AL;
m_iMsgNum = MESSAGENUMBERS.CODE_ERROR_ FOUND;
}
}
public string GetMsgId() {return m_sMsgId;}
public int GetMsgNum() {return m_iMsgNum;}
private static string m_sMsgId = MESSAGETYPES.ERROR_LOG_REA DER;
private static int m_iMsgNum = MESSAGENUMBERS.ERROR_LOG_R EADER;
}
}
constants.cs
here are some of the things from the constants file. Sorry I can not post the whole thing way to large
public const string FAILED_TO_READ_ERROR_LOG
= "Failed to read errlog.txt and errlog.txt.old.";
public const string ERRORLOG_FILE_NOT_FOUND
= "errlog.txt File Not Found";
//ErrorLogReader Tags
public const string OldFileName = "OldFileName";
public const string LogMsgLookingFor = "LogMsgLookingFor";
public const string LastSentTime = "LastSentTime";
public const string LogErrorTime = "LogErrorTime";
public const string LastErrorFoundIn = "LastErrorFoundIn";
public const string ErrorLogFileDate = "ErrorLogFileDate";
public const string OldErrorLogFileDate = "OldErrorLogFileDate";
public const string ErrorLogMessage = "ErrorLogMessage";
public const string MessageId = "MessageId";
public const string LastMessageError = "LastMessageError";
public const string LastMessageErrorTime = "LastMessageErrorTime";
public const string RunLast = "RunLast";
public const string Directory = "Directory";
public const string FileName = "FileName";
public const string LastErrorTime = "LastErrorTime";
public const string LastError = "LastError";
public const string NULL = "null";
public const string EMPTY_STRING = "";
public const string XML_INITIAL_DATE = "2001-01-01T00:00:00";
public const string NO_NEW_ERRORLOG_ERRORS = "No new errors in errlog";
public const string ERRORLOG_INDEX_OUT_OF_RANG E = "Index out of Range must be non-negative and less than size of collection";
There is an example of how the errorlog is set up listed in a previous post. It is took big of a file for me to put in here since it is constantly being written too.
Sorry if the alignment is not right. I added the code to the orginal post.
ErrorLogReader.cs
using System;
using System.Globalization;
using System.Xml;
using System.IO;
using System.Collections;
using Microsoft.Win32;
using QTI.Monitor.Configuration;
using System.Windows.Forms;
namespace QTI.Monitor.Tasks
{
public class TaskErrorLogReader : TaskInterface
{
public void DoTask(XmlNode xmlNode, ref int iSeverityLevel,
ref String[] sValues, ref String[] sDetails)
{
string sCurrentFileName = CONSTANTS.NULL;
string sOldFileName = CONSTANTS.NULL;
string sDirectory = CONSTANTS.NULL;
//Sending an empty string so Client does not send a message
sValues[0] = CONSTANTS.EMPTY_STRING;
//Today's date for comparisons
System.DateTime dToday = System.DateTime.Today.Date
//Yesterday's date for comparisons
System.DateTime dDayBefore = dToday.AddDays(-1);
//Used to update the Date/Time in XML
DateTime dDateTimeCurrent = DateTime.Now;
//Get the RunLast from XML File
XmlNode xmlRunLast = xmlNode.SelectSingleNode(T
DateTime dRunLast = Convert.ToDateTime(xmlRunL
// Get directory from XML File
XmlNode xmlDirectory = xmlNode.SelectSingleNode(T
sDirectory = xmlDirectory.InnerText;
//Get CurrentFileName (errorlog.txt) from XML File
XmlNode xmlCurrentFileName = xmlNode.SelectSingleNode(T
sCurrentFileName = xmlCurrentFileName.InnerTe
//Get OldFileName (errorlog.txt.old) from XML File
XmlNode xmlOldFileName = xmlNode.SelectSingleNode(T
sOldFileName = xmlOldFileName.InnerText;
//Get Message Error node from XML file
XmlNode xmlMessageError = xmlNode.SelectSingleNode(T
//Get the Message Error Time node from XML file
XmlNode xmlMessageErrorTime = xmlNode.SelectSingleNode (TAGS.LastMessageErrorTime
//Get Error node from XML file
XmlNode xmlError = xmlNode.SelectSingleNode(T
//Get the Error Time node from XML file
XmlNode xmlErrorTime = xmlNode.SelectSingleNode(T
try
{
// Save any messages found in the error log.
ArrayList LastFoundTimeAL = new ArrayList();
ArrayList LastSentTimeAL = new ArrayList();
ArrayList LogErrorTimeAL = new ArrayList();
ArrayList LastErrorFoundInAL = new ArrayList();
int iXMLErrorMsgIndex = 0;
// Loop through all errorlog messages,saving any messages that are found in an array.
XmlNodeList nodeListErrorMessages = xmlNode.SelectNodes(TAGS.E
foreach (XmlNode xmlNodeErrorLogMessage in nodeListErrorMessages)
{
// Message Id.
XmlNode XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel
double dXMLMessageId = 0;
if (XmlNodeMessageChild.Inner
{
dXMLMessageId = Convert.ToDouble(XmlNodeMe
}
// List for the Log message source.
ArrayList LogMsgLookingFor = new ArrayList();
XmlNodeList nodeListLogMsgLookingFor = xmlNodeErrorLogMessage.Sel
(TAGS.LogMsgLookingFor);
foreach (XmlNode NodeLogMsgLookingFor in nodeListLogMsgLookingFor)
{
LogMsgLookingFor.Add(NodeL
}
//Initialize the array lists containing the messages found in the errorlog.
//Last Found Time.
XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel
LastFoundTimeAL.Add(XmlNod
//Last Sent Time
XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel
LastSentTimeAL.Add(XmlNode
//Log Error Time
XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel
LogErrorTimeAL.Add(XmlNode
//Last Error Found In
XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel
LastErrorFoundInAL.Add(Xml
//Check to see if the file exists
if (File.Exists(sDirectory + sCurrentFileName))
{
// Save any messages found in the error log.
ArrayList CurrentErrorLogDateAL = new ArrayList();
//Initialize the array lists containing the messages found in the errorlog.
//Current Error Log File Date
XmlNodeMessageChild = xmlNodeErrorLogMessage.Sel
CurrentErrorLogDateAL.Add(
// Get timestamp of Current errorlog file.
DateTime dCurrentErrorLogFileDate = File.GetLastWriteTime(sDir
String sCurrentErrorLogFileDate = dCurrentErrorLogFileDate.T
("s",System.Globalization.
dCurrentErrorLogFileDate = Convert.ToDateTime(sCurren
if ((dCurrentErrorLogFileDate
{
if (dDateTimeCurrent > dRunLast)
{
foreach (string sLogMsgLookingFor in LogMsgLookingFor)
{
bool bFound = false;
//open the file for reading
StreamReader objReader = new StreamReader(sDirectory + sCurrentFileName);
string sCurrentLine = objReader.ReadLine();
string sPreviousLine = "";
**************************
Should I really terminate this while loop if the log message you are looking for has been found?
see next note). Could this be part of the index problem.? "bFound = true;
**************************
while(sCurrentLine != null)
{
if(sCurrentLine.IndexOf(sL
{
if (sPreviousLine != null)
{
bFound = true;
}
}
if (bFound == true)
{
//Change previous line to when even happened time
DateTime dErrorTime = Convert.ToDateTime(sPrevio
DateTime dPreviousLine = Convert.ToDateTime(sPrevio
String sErrorDateTime = dPreviousLine.ToString("dd
//Get the LogErrorTime from XML File
XmlNode xmlLogErrorTime = xmlNodeErrorLogMessage.Sel
DateTime dLogErrorTime = Convert.ToDateTime(xmlLogE
String sLogErrorTime = dLogErrorTime.ToString("dd
//Set the LastFoundTime to today in xml file.
XmlNode xmlNodeLastFoundTime = xmlNodeErrorLogMessage.Sel
xmlNodeLastFoundTime.Inner
("s",System.Globalization.
**************************
if bFound == true twice (because "sLogMsgLookingFor" is in errorlog.txt twice), then iXMLErrorMsgIndex will = 2, and LastFoundTimeAL.Count will still = 1, causing the else block to be executed. I believe this is where the index error is happening. I don't know if it is happening else where since it stops here
**************************
if(iXMLErrorMsgIndex < LastFoundTimeAL.Count)
{
//Upgrade the array lists.
LastFoundTimeAL[iXMLErrorM
("s",System.Globalization.
}
else
{
xmlError.InnerText =
CONSTANTS.ERRORLOG_INDEX_O
xmlErrorTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization.
}
if ((dErrorTime == dToday) && (sErrorDateTime != sLogErrorTime))
{
//Change from string to datetime to use
DateTime dTimeFromLog = Convert.ToDateTime(sPrevio
//Set the LogErrorTime to what was found in the errlog.txt file for the error.
xmlLogErrorTime = xmlNodeErrorLogMessage.Sel
xmlLogErrorTime.InnerText = dTimeFromLog.ToString
("s",System.Globalization.
//Set LastErrorFoundIn to sCurrentFileName
XmlNode xmlLastErrorFoundIn = xmlNodeErrorLogMessage.Sel
(TAGS.LastErrorFoundIn);
xmlLastErrorFoundIn.InnerT
//Set LastSentTime to current date time.
XmlNode xmlLastSentTime = xmlNodeErrorLogMessage.Sel
(TAGS.LastSentTime);
xmlLastSentTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization.
//Set ErrorLogFileDate in XML
XmlNode xmlErrorLogFileDate = xmlNodeErrorLogMessage.Sel
(TAGS.ErrorLogFileDate);
xmlErrorLogFileDate.InnerT
if(iXMLErrorMsgIndex < LastSentTimeAL.Count)
{
// Upgrade the array lists.
LastSentTimeAL[iXMLErrorMs
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O
+ "LastSentTimeAL";
xmlMessageErrorTime.InnerT
("s",System.Globalization.
}
if(iXMLErrorMsgIndex < LogErrorTimeAL.Count)
{
// Upgrade the array lists.
LogErrorTimeAL[iXMLErrorMs
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O
xmlMessageErrorTime.InnerT
("s",System.Globalization.
}
if(iXMLErrorMsgIndex < LastErrorFoundInAL.Count)
{
// Upgrade the array lists.
LastErrorFoundInAL[iXMLErr
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O
+ "LastErrorFoundInAL";
xmlMessageErrorTime.InnerT
("s",System.Globalization.
}
if(iXMLErrorMsgIndex < CurrentErrorLogDateAL.Coun
{ // Upgrade the array lists.
CurrentErrorLogDateAL[iXML
}
else
{
xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_O
+ "CurrentErrorLogDateAL";
xmlMessageErrorTime.InnerT
("s",System.Globalization.
}
sDetails[0] = CONSTANTS.ERRORLOG_ERROR_F
sValues[0] = "Error: " + sCurrentLine + " Error Occured at: " + sPreviousLine;
iSeverityLevel = SEVERITYLEVELS.WARNING;
//Update index into the array list.
iXMLErrorMsgIndex++;
}
else if (sErrorDateTime == sLogErrorTime)
{
xmlError.InnerText = CONSTANTS.NO_NEW_ERRORLOG_
xmlErrorTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization.
//Sending an empty string so Client does not send a message
sValues[0] = CONSTANTS.EMPTY_STRING;
}
}
sPreviousLine = sCurrentLine;
bFound = false;
sCurrentLine = objReader.ReadLine();
}//end while loop
objReader.Close();
}
}
}
}
else
{
xmlError.InnerText = ERROR.ERRORLOG_FILE_NOT_FO
xmlErrorTime.InnerText = dDateTimeCurrent.ToString
("s",System.Globalization.
}
catch (Exception e)
{
System.Diagnostics.EventLo
ERROR.FAILED_TO_READ_ERROR
System.Diagnostics.EventLo
sValues[0] = ERROR.FAILED_TO_READ_ERROR
iSeverityLevel = SEVERITYLEVELS.INFORMATION
m_iMsgNum = MESSAGENUMBERS.CODE_ERROR_
}
}
public string GetMsgId() {return m_sMsgId;}
public int GetMsgNum() {return m_iMsgNum;}
private static string m_sMsgId = MESSAGETYPES.ERROR_LOG_REA
private static int m_iMsgNum = MESSAGENUMBERS.ERROR_LOG_R
}
}
constants.cs
here are some of the things from the constants file. Sorry I can not post the whole thing way to large
public const string FAILED_TO_READ_ERROR_LOG
= "Failed to read errlog.txt and errlog.txt.old.";
public const string ERRORLOG_FILE_NOT_FOUND
= "errlog.txt File Not Found";
//ErrorLogReader Tags
public const string OldFileName = "OldFileName";
public const string LogMsgLookingFor = "LogMsgLookingFor";
public const string LastSentTime = "LastSentTime";
public const string LogErrorTime = "LogErrorTime";
public const string LastErrorFoundIn = "LastErrorFoundIn";
public const string ErrorLogFileDate = "ErrorLogFileDate";
public const string OldErrorLogFileDate = "OldErrorLogFileDate";
public const string ErrorLogMessage = "ErrorLogMessage";
public const string MessageId = "MessageId";
public const string LastMessageError = "LastMessageError";
public const string LastMessageErrorTime = "LastMessageErrorTime";
public const string RunLast = "RunLast";
public const string Directory = "Directory";
public const string FileName = "FileName";
public const string LastErrorTime = "LastErrorTime";
public const string LastError = "LastError";
public const string NULL = "null";
public const string EMPTY_STRING = "";
public const string XML_INITIAL_DATE = "2001-01-01T00:00:00";
public const string NO_NEW_ERRORLOG_ERRORS = "No new errors in errlog";
public const string ERRORLOG_INDEX_OUT_OF_RANG
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bob-
How should I do it if there are mulitple messages being searched for.
How should I do it if there are mulitple messages being searched for.
Add a While loop. Also, I added the WordWrap = false, otherwise GetLineFromCharIndex returns incorrect values because of line wrapping.
public void SearchForMessage(string path, string message)
{
// Create an instance of a RichTextBox to load a file
// and search for text.
RichTextBox rtfSearcher = new RichTextBox();
// Clear word wrap so GetLineFromCharIndex returns correct value.
rtfSearcher.WordWrap = false;
// Load the log file.
rtfSearcher.LoadFile(path, RichTextBoxStreamType.Plai nText);
// The date will be on the line previous to the found message.
string messageLine = String.Empty;
string dateLine = String.Empty;
// Set the initial start position at the beginning.
int start = 0;
// Set the initial value to allow the While loop to work.
int messageIndex = 0;
// Search for all messages.
while (messageIndex != -1 && start < rtfSearcher.Text.Length)
{
// Find the message starting at the current start index.
messageIndex = rtfSearcher.Find(message, start, RichTextBoxFinds.NoHighlig ht);
if (messageIndex != -1)
{
// The message was found, so get the text.
// Convert the char index to a line number.
int lineNumber = rtfSearcher.GetLineFromCha rIndex(mes sageIndex) ;
// Make sure the index is valid.
if (lineNumber < rtfSearcher.Lines.Length)
{
// Get the message, and the date.
messageLine = rtfSearcher.Lines[lineNumb er];
dateLine = rtfSearcher.Lines[lineNumb er - 1];
// Move the start position past the text already found.
start += messageIndex + messageLine.Length;
}
}
}
}
Bob
public void SearchForMessage(string path, string message)
{
// Create an instance of a RichTextBox to load a file
// and search for text.
RichTextBox rtfSearcher = new RichTextBox();
// Clear word wrap so GetLineFromCharIndex returns correct value.
rtfSearcher.WordWrap = false;
// Load the log file.
rtfSearcher.LoadFile(path,
// The date will be on the line previous to the found message.
string messageLine = String.Empty;
string dateLine = String.Empty;
// Set the initial start position at the beginning.
int start = 0;
// Set the initial value to allow the While loop to work.
int messageIndex = 0;
// Search for all messages.
while (messageIndex != -1 && start < rtfSearcher.Text.Length)
{
// Find the message starting at the current start index.
messageIndex = rtfSearcher.Find(message, start, RichTextBoxFinds.NoHighlig
if (messageIndex != -1)
{
// The message was found, so get the text.
// Convert the char index to a line number.
int lineNumber = rtfSearcher.GetLineFromCha
// Make sure the index is valid.
if (lineNumber < rtfSearcher.Lines.Length)
{
// Get the message, and the date.
messageLine = rtfSearcher.Lines[lineNumb
dateLine = rtfSearcher.Lines[lineNumb
// Move the start position past the text already found.
start += messageIndex + messageLine.Length;
}
}
}
}
Bob
ASKER
Thank you for your help.
you guys got me really thinking about it. I decided to try to remove all of the ArrayLists minus one
ArrayList LogMsgLookingFor = new ArrayList();
So far the testing seems to be going good. I still have further testing to do. If what I have done does not work then Bobs solution will be the best next step.
Thanks again!!! I really appreciate your help and your sharing a little knowledge with a newbie!!
you guys got me really thinking about it. I decided to try to remove all of the ArrayLists minus one
ArrayList LogMsgLookingFor = new ArrayList();
So far the testing seems to be going good. I still have further testing to do. If what I have done does not work then Bobs solution will be the best next step.
Thanks again!!! I really appreciate your help and your sharing a little knowledge with a newbie!!
Are you parsing an error log for a specific message? What would a few of the lines in the file look like (not all of them if the file is large)?
It may be quite a bit easier to accomplish this task, but I need to make sure that I understand the requirement.
Bob