I am using LogParser tool (MSUtil dll) to insert data in db through SSIS Package.
I am using following code in script task component:
Looping through log files and using following :
private bool InsertLogParserToSQL()
{
LogQueryClass logQuery = new LogQueryClass();
COMIISW3CInputContextClass inputContext = new COMIISW3CInputContextClass();
COMSQLOutputContextClass outputContext = new COMSQLOutputContextClass();
bool result = false;
string errMessage = string.Empty;
try
{
inputContext.consolidateLogs = true;
inputContext.recurse = 0;
outputContext.clearTable = false;
outputContext.createTable = false;
outputContext.fixColNames = true;
outputContext.ignoreIdCols = true;
outputContext.ignoreMinWarns = false;
outputContext.transactionRowCount = -1;
outputContext.database = DatabaseName;
outputContext.server = ServerName;
outputContext.username = UserName;
outputContext.password = Password;
outputContext.driver = DriverName;
DirectoryInfo info = new DirectoryInfo(System.IO.Path.GetDirectoryName(DirectoryName));
FileInfo[] logFileInfo = info.GetFiles();
foreach (FileInfo fi in logFileInfo)
{
string logiles = DirectoryName.TrimEnd('\\') + '\\' + fi.Name;
String query = "SELECT TO_LOCALTIME(QUANTIZE(TO_TIMESTAMP(date, time), 3600)),time,cs-method,cs-uri-stem,cs-uri-query,cs-username,c-ip,cs(User-Agent),cs(Referer),cs-host,sc-status,sc-substatus,sc-win32-status,time-taken FROM '" + logiles + "' TO logData";
try
{
logQuery.ExecuteBatch(query, inputContext, outputContext);
result = (logQuery.lastError == 0);
errMessage = logQuery.errorMessages.ToString();
}
catch (System.Runtime.InteropServices.COMException exc)
{
Console.WriteLine("Unexpected error: " + exc.Message);
return result;
}
if (errMessage != null)
{
Console.WriteLine("Logs added to database.\r\n\r\n");
}
else
{
Console.WriteLine("Logs failed to add to database.\r\n\r\n" + logQuery.lastError.ToString() + " at " + logiles);
}
}
return result;
}
catch (System.Runtime.InteropServices.COMException exc)
{
Console.WriteLine("Unexpected error: " + exc.Message);
return result;
}
if (result)
{
Console.WriteLine("Logs added to database.\r\n\r\n");
}
else
{
Console.WriteLine("Logs failed to add to database.\r\n\r\n");
}
return result;
}
By this code,only half data is inserted in table and it doesn't throw any exception .
And my another question is : Is there any faster way to process log file(8-10 lacs) data to db through SSIS Package other than script component?
Please guide.Thanks.
Try checking these links
https://stackoverflow.com/questions/41874275/use-log-parser-to-bulk-insert-iis-logs-into-sql-server
These may help.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly