Export IIS Log to SQL Server

Did some research on how to analyse IIS logs. Desktop software doesn’t work with 700M log files. Instead I found LogParser did a better job by exporting the log to SQL server database. It’s then flexible to generate any kind of report.


Step 1. Create SQL Server database:


CREATE TABLE [dbo].[iislog](
[LogFilename] [varchar](255) NULL,
[RowNumber] [int] NOT NULL,
[EntryTime] [datetime] NULL,
[ServerIpAddress] [varchar](255) NULL,
[Method] [varchar](255) NOT NULL,
[UriStem] [varchar](255) NOT NULL,
[UriQuery] [varchar](255) NOT NULL,
[Port] [int] NULL,
[Username] [varchar](255) NULL,
[ClientIpAddress] [varchar](255) NULL,
[UserAgent] [varchar](255) NULL,
[HttpStatus] [int] NULL
)


Step 2. Create PowerShell script:


$ErrorActionPreference = “Stop”

function ImportLogFiles(
[string] $httpLogPath)
{
If ([string]::IsNullOrEmpty($httpLogPath) -eq $true)
{
Throw “The log path must be specified.”
}

[string] $logParser = “${env:ProgramFiles(x86)}” `
+ “\Log Parser 2.2\LogParser.exe”

[string] $query = `
“SELECT” `
+ ” LogFilename” `
+ “, RowNumber” `
+ “, TO_TIMESTAMP(date, time) AS EntryTime” `
+ “, s-ip AS ServerIpAddress” `
+ “, cs-method AS Method” `
+ “, cs-uri-stem AS UriStem” `
+ “, cs-uri-query AS UriQuery” `
+ “, s-port AS Port” `
+ “, cs-username AS Username” `
+ “, c-ip AS ClientIpAddress” `
+ “, cs(User-Agent) AS UserAgent” `
+ “, sc-status AS HttpStatus” `
+ ” INTO Intranet” `
+ ” FROM $httpLogPath\*.log”

[string] $connectionString = “Driver={SQL Native Client};Server=myserver;Database=mydatabase;Trusted_Connection=yes;”

[string[]] $parameters = @()

$parameters += $query
$parameters += “-i:W3C”
$parameters += “-o:SQL”
$parameters += “-oConnString:$connectionString”

Write-Debug “Parameters: $parameters”

Write-Host “Importing log files to database…”
& $logParser $parameters
}

function Main
{
[string] $httpLogPath = “C:\iislog”

ImportLogFiles $httpLogPath

Write-Host -Fore Green “Successfully imported log files.”
}

Main


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值