4

I have a lot of IIS logs that need to be inserted into a SQL Server database. After some research it looks like Log Parser is the way to go but I am having a hard time figuring out how to automate the process. We have hundreds of these files, some of which are up to 3Gb in size.

The console command to insert a file is

"C:\Program Files (x86)\Log Parser 2.2\LogParser.exe" "SELECT * INTO LogTable FROM C:\Logs\ex1234.log" -i:iisw3c -o:SQL -server:ServerName -database:DatabaseName -driver:"SQL Server" -createTable:ON

In order to iterate through the directories and files I created a small console app in which the main method enumerates through the directories and then sends each file to a method that runs the Log Parser:

    static void RunLogParser(string fileName)
    {
        var logParserPath = @"""C:\\Program Files (x86)\\Log Parser 2.2\\LogParser.exe""";

        var query = $"\"SELECT * INTO LogTable FROM {fileName}\"";            

        var startInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe",
            Arguments = logParserPath + query + "-i:iisw3c -o:SQL -server:ServerName -database:DatabaseName -driver:\"SQL Server\" -createTable:ON",
            CreateNoWindow = true,
            UseShellExecute=false
        };

        try
        {
            using (var process = Process.Start(startInfo))
            {
                process.WaitForExit();
            }
        }
        catch(Exception ex)
        {
            throw new Exception(ex.Message);
        }

        Console.ReadKey();
    }

When I run this nothing seems to happen. Executing the command from the console takes about 20 seconds to insert a test file, but when I run this same command from the code nothing.

I have also tried using Log Parser as the FileName arg in the ProcessStartInfo but when the process executes all that happens is that the command window seems to display the arguments for the Log Parser command with an error message but I can't the window closes too fast to read the message.

Edit: Solved

It seems that I had a typo, much to my chagrin. I changed the ProcessStartInfo filename to the LogParser.exe from cmd.exe, added a space after the select statement and changed the args to a literal string and double quoted the driver argument.

    static void RunLogParser(string fileName)
    {
        var logParserPath = @"""C:\\Program Files (x86)\\Log Parser 2.2\\LogParser.exe""";

        var query = $"\"SELECT * INTO LogTable FROM {fileName}\"";            

        var startInfo = new ProcessStartInfo
        {
            FileName = logParserPath ,
            Arguments = query + @" -i:iisw3c -o:SQL -server:ServerName -database:DatabaseName -driver:""SQL Server"" -createTable:ON",
            CreateNoWindow = true,
            UseShellExecute=false
        };

        try
        {
            using (var process = Process.Start(startInfo))
            {
                process.WaitForExit();
            }
        }
        catch(Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
Community
  • 1
  • 1
crunchy
  • 705
  • 1
  • 13
  • 35
  • did you tried https://chrisbitting.com/2018/03/01/iis-to-sql-log-parser-import-performance-w-transactionrowcount/ ? – Kiquenet Jul 05 '18 at 18:16

0 Answers0