Search code examples
t-sqliisasp.net-mvc-5logparser

How to query IIS web logs from T-SQL directly


I learned how to do basic queries of the IIS web log with LogParser Studio and to export the LogParser query into a PowerShell script, which is great because I can have the Windows Task Scheduler run the script on a schedule.

However, I was wondering if it might be possible to directly query the IIS web log using a T-SQL Script rather than using my current procedure:

  1. Use TaskScheduler to run Powershell script to run LogParser query of IIS web log and export results as CSV
  2. Use SQL Agent to run SSIS package to import CSV into SQL Server Table
  3. View table results on MVC webpage

What I'd really like to do is have the user in the MVC webpage click a button to trigger (via actionlink) a stored procedure that re-queries the IIS weblog, runs the SSIS import package via SQL Agent Job, and displays the results on screen.

While I could have the Task Scheduler and Agent Jobs run more frequently, having it run by demand (by user click) makes sure that the query is run only on demand, and not during time intervals in which there is no demand for the query results.

Is such a thing even possible with the state of SQL Server? (I'm running version 2014 and IIS version 8.5).


Solution

  • As @Malik mentioned, sp_start_job can be used to run an unscheduled Agent job.

    In this case, my job has two steps:

    1. Run the PowerShell script, with the script pasted directly into the Agent job step. I had to make sure that I had a proxy for PowerShell set up.
    2. Run the SSIS package to import the CSV into the SQL table.

    My stored procedure is very simple:

    ALTER PROCEDURE [dbo].[RefreshErrorQuery] 
        -- No parameters
    
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        -- Rigger unscheduled job to run
        exec msdb.dbo.sp_start_job N'Refresh Web Query';
    END