I've looked at answers that are similar to this issue, but I still don't think I understand why I'm getting an exception.
I'm attempting to debug a section of code that is throwing out an "OutOfMemoryException" when writing to a text file, according to the stacktrace. The exception is being thrown from the following line:
WriteToLogFile("Found in EnumerateActiveDirectoryFilteredMembers: " + e.Message);
The WriteToLogFile method is as follows:
static void WriteToLogFile(string strLine)
{
using (StreamWriter sw = new StreamWriter(strLogFileName, true))
{
sw.WriteLine("[" + DateTime.Now.ToString() + "]: " + strLine);
}
}
It looks like StreamWriter objects can throw this exception sometimes, but isn't the memory being reallocated at the end of this method every time it is called? And isn't the use of the "using" keyword ensuring tht this object is being thrown away?
The file, by the time the exception is thrown, is only 13 KB - so what exactly is going on here?
UPDATE: The OutOfMemoryException is not the WriteToLogFile method. It's being thrown earlier, but I don't know where this is coming from... I'll add a skeleton of the try statement before the catch:
try
{
if (objSearchResults.Count != 0)
{
// ADD TO DATASET HERE //
string tableName = "tempADhold";
DataSet domains = new DataSet();
domains.Tables.Add(tableName);
//add x number of columns
domains.Tables[tableName].Columns.Add("DomainID", typeof(string));
.
.
.
// Get the object from AD.
foreach (SearchResult objResultUser in objSearchResults)
{
// Init variables and init formatting of strings for adding objects
string DomainID = ""; string UserName = ""; string Title = "";
.
.
.
objUserEntry = objResultUser.GetDirectoryEntry();
// Populate obj and LDAP path variables.
// Perform edits before setting variables.
// Match strings to SQL Server table sizes.
// add objects in same manner:
// DomainID
if (objUserEntry.Properties["samAccountName"].Count > 0)
{
DomainID = objUserEntry.Properties["samAccountName"].Value.ToString();
if (DomainID.Substring(0, 1).ToUpper() != "N")
{
WriteToLogFile("RECORD SKIPPED: Invalid N_Number: " + DomainID);
continue; //skip this record
}
else if (DomainID.Length > 8)
{
WriteToLogFile("RECORD SKIPPED: Invalid N_Number length. #s ending in 'c' should be skipped: " + DomainID);
continue; //skip this record
}
}
.
.
.
//set up array struct for adding obj
ADUserList[0] = DomainID; ADUserList[1] = UserName;
ADUserList[2] = ...; ADUserList[3] = ...;
ADUserList[4] = ...; ADUserList[5] = ...;
ADUserList[6] = ...; ADUserList[7] = ...;
ADUserList[8] = ...; ADUserList[9] = ...;
ADUserList[10] = ...; ADUserList[11] = ...;
ADUserList[12] = ...; ADUserList[13] = ...;
ADUserList[14] = ...; ADUserList[15] = ...;
ADUserList[16] = ...;
if (ADUsersIndex > 0 && ADUsersIndex % 2500 == 0)
{
//Add to dataset instead of array here...
if (InsertRows(domains.Tables[tableName]).Equals(false)) { return false; }
domains.Tables[tableName].Rows.Clear();
}
DataRow myRow = domains.Tables[tableName].NewRow();
myRow.ItemArray = ADUserList;
domains.Tables[tableName].Rows.Add(myRow);
ADUsersIndex++;
}
//Write the last rows to the database.
if (InsertRows(domains.Tables[tableName]).Equals(false)) { return false; }
objSearchResults.Dispose();
} //end "if (objSearchResults.Count != 0)"
else
{
WriteToLogFile("Results: No Active Directory filtered members found.");
}
}
catch (Exception e)
{
if (e is OutOfMemoryException) throw;
WriteToConsole("Error in EnumerateActiveDirectoryFilteredMembers: " + e.Message);
return true;
}