Iis6.0 Log File Analysis code _ 3 the thread reads files to the database (tested), but lacks the ability to store log files in batch. defines an array. list of stored files. read files to the database in order. reviewed thread operations. array Control. key technologies such as file access and database operations.
// Processing logic for batch log files:
// Method 1: Single-threaded single-file processing
// 1. Select the directory where the log file is located, and store the name, processing result status, and current processing status of all log files in the data table.
// 2. enable reading the file name in the single-thread log list file data table and define all the log field names in the array, write all the corresponding fields into the data table and update them by the number of log-related lines in the read text file.
// 3. Read the log data of the file cyclically until it is completed and write the processing result to the database.
// Method 2 single-threaded Multi-File Processing
// 1. Select the log file directory, traverse all log files under the directory (except for today's), and write all file paths to the array.
// 2. Enable the thread loop to write the log records to the database table based on the input parameters (separate log file path)
// 3. The thread stops reading the log file, returns the result variable, continues to get the data of the next log file from the array, and continues until the data of all log files is read.
// Method 3 single-threaded Multi-File Processing
// 1. Open the dialog box and select the log file to be read. Different IIS sites are distinguished based on the directory name.
// 2. Enable a single thread to read log files in sequence to the database
// 3. The thread reading is complete. Continue reading after a period of time until all log files are written to the database.
// Method 4
// 1. select the log file list, back up all log files to a fixed format text file, use the text file as the data source, and read the text file into the data table according to the database-defined field name, size of the log file 3.20.m. log line 20434, file
// Note: When the thread function is enabled, the progress of the current log processing can be displayed to prevent "false dead" or "stuck" when the program is running.
// Pre-processing the log file data into a valid text file can reduce the memory expenditure for Array and data judgment.
// Add database file IO reference
Using system. IO;
Using system. text;
Using system. Data;
Using system. Data. sqlclient;
// Reference using thread operations
Using system. Threading;
// Button event
Private void button6_click (Object sender, system. eventargs E)
{
// Start the thread to download files: the stable performance is better, and the thread monitoring should continue to be highlighted without getting stuck.
This. button6.enabled = false;
Iislogfileclass myclass = new iislogfileclass ();
Thread mythread = new thread (New threadstart (myclass. readfromlogfile ));
Myclass. logfilepath = @ "C:/Windows/system32/logfiles/w3svc1/ex070412.log ";
Mythread. Start ();
If (mythread. Join (50000) // The unit of time for the main thread to wait for the stop of the sub-thread: milliseconds
{
This. textbox2.text = myclass. logfilepath;
This. textbox3.text = myclass. linecontent. tostring ();
This. textbox4.text = myclass. linefile. tostring ();
This. textbox5.text = myclass. filelength. tostring ();
}
This. button6.enabled = true;
}
// Define the log file processing class.
Public class iislogfileclass
{
Public String logfilepath; // download file storage address
Public long filelength; // defines the parameters returned by the thread.
Public int linesoftware = 0; // defines the parameters returned by the thread.
Public int lineversion = 0; // defines the parameters returned by the thread.
Public int linedate = 0; // defines the parameters returned by the thread.
Public int linefields = 0; // defines the parameters returned by the thread.
Public int linecontent = 0; // defines the parameters returned by the thread.
Public int linefile = 0; // defines the parameters returned by the thread.
Public void readfromlogfile ()
{
// Handle large log files:
// 1. Define all log field names in the array and write all corresponding fields into the text file.
// 2. Use a text file as the data source and write the content of all fields one by one as a record to the corresponding fields in the database
// Define the number of fields to add
Int totalfieldslength = 0;
// Define the field subscript array. The database content must be consistent with the following Array
Arraylist intlogfieldsarray = new arraylist ();
Try
{
// Open the database connection
String strcon = "Initial catalog = 'hmmisdata'; server = '192. 168.1.250 '; user id = 'xqf'; Password = '2016'; persist Security info = true ";
Sqlconnection myconn = new sqlconnection (strcon );
Myconn. open ();
Dataset tempdataset = new dataset ();
Sqldataadapter tempadapter = new sqldataadapter ("select * From iislogfiledslist where 1 = 0", myconn );
Sqlcommandbuilder tempbuilder = new sqlcommandbuilder (tempadapter );
Tempadapter. Fill (tempdataset );
Filestream FS = new filestream (logfilepath, filemode. openorcreate, fileaccess. Read); // open a log file
Filelength = FS. length;
Streamreader mystreamreader = new streamreader (FS); // use the streamreader class to read files
Mystreamreader. basestream. Seek (0, seekorigin. Begin); // read each row from the data stream cyclically until the last row of the file
String strline = mystreamreader. Readline ();
Arraylist mysoftwarearray = new arraylist ();
Arraylist myversionarray = new arraylist ();
Arraylist mydatearray = new arraylist ();
Arraylist myfieldsarray = new arraylist ();
While (strline! = NULL)
{
Linefile + = 1;
If (strline. substring (0, 10) = "# software :")
{
Linesoftware + = 1;
Mysoftwarearray. Add (strline. substring (10, strline. Length-10 ));
}
Else
{
If (strline. substring (0, 9) = "# version :")
{
Lineversion + = 1;
Myversionarray. Add (strline. substring (9, strline. Length-9 ));
}
Else
{
If (strline. substring (0, 6) = "# Date :")
{
Linedate + = 1;
Mydatearray. Add (strline. substring (6, strline. Length-6 ));
}
Else
{
If (strline. substring (0, 8) = "# fields :")
{
Linefields + = 1;
Myfieldsarray. Add (strline. substring (9, strline. Length-9 ));
If (linefields = 1)
{
// Define the name of the record field to be added
String strt = myfieldsarray [0]. tostring ();
String [] tarray = strt. Split ('');
Totalfieldslength = tarray. length;
For (int K = 0; k <totalfieldslength; k ++)
{
Switch (tarray [K]. tostring ())
{
Case "date": intlogfieldsarray. Add (1); break;
Case "time": intlogfieldsarray. Add (2); break;
Case "C-IP": intlogfieldsarray. Add (3); break;
Case "CS-username": intlogfieldsarray. Add (4); break;
Case "s-sitename": intlogfieldsarray. Add (5); break;
Case "s-computername": intlogfieldsarray. Add (6); break;
Case "s-IP": intlogfieldsarray. Add (7); break;
Case "s-port": intlogfieldsarray. Add (8); break;
Case "CS-method": intlogfieldsarray. Add (9); break;
Case "CS-Uri-stem": intlogfieldsarray. Add (10); break;
Case "CS-Uri-query": intlogfieldsarray. Add (11); break;
Case "SC-status": intlogfieldsarray. Add (12); break;
Case "SC-substatus": intlogfieldsarray. Add (13); break;
Case "sc-win32-status": intlogfieldsarray. Add (14); break;
Case "SC-bytes": intlogfieldsarray. Add (15); break;
Case "CS-bytes": intlogfieldsarray. Add (16); break;
Case "CS-version": intlogfieldsarray. Add (17); break;
Case "CS-timetaken": intlogfieldsarray. Add (18); break;
Case "CS-host": intlogfieldsarray. Add (19); break;
Case "CS (User-Agent)": intlogfieldsarray. Add (20); break;
Case "CS-cookie": intlogfieldsarray. Add (21); break;
Case "CS-Referer": intlogfieldsarray. Add (22); break;
}
}
}
}
Else
{
Linecontent + = 1;
String [] currentcontentarray = strline. Split ('');
// Cyclically write all log records to the database
Datarow tempdatarow = tempdataset. Tables [0]. newrow (); // define the row for the new record
For (INT n = 0; n <totalFieldsLength-1; n ++)
{
Tempdatarow [int32.parse (intlogfieldsarray [N]. tostring ()] = currentcontentarray [N];
// Console. writeline (intlogfieldsarray [N]. tostring () + ":" + logarraylist [M, N]);
}
Tempdataset. Tables [0]. Rows. Add (tempdatarow); // Add a new record row
}
}
}
}
Strline = mystreamreader. Readline ();
}
Mystreamreader. Close (); // close this streamreader object
FS. Close ();
Tempadapter. Update (tempdataset );
Myconn. Close (); // close the connection
// Clear all array content
Mysoftwarearray = NULL;
Myversionarray = NULL;
Mydatearray = NULL;
Myfieldsarray = NULL;
}
Catch (exception ex)
{
MessageBox. Show (ex. message, "error ");
}
}
// End the definition class code