Sen
Sen

Reputation: 145

need help parsing an IIS log in c#

My IIS log has a query parameter(cs-uri-query) that looks like below

"TraceId=8c0b8329-f125-4dec-90af-f508674284f5,PartnerId=Partner1\r\n,UserInput=Address1:+1234+block+of+XYZ+Street+Address2:+Santa+Fe+Springs+State:+California+ZipCode:+90000+Country:+United+States+"

I need to extract Address1,Address2,State,ZipCode and Country from the above line. also note that these fields need not always appear in the same order.

What is the best and quick way to parse this?I'am trying to do this using a c# command line tool.any other script based solution is also fine.

Upvotes: 1

Views: 2477

Answers (2)

nateirvin
nateirvin

Reputation: 1183

LogParser is very good for this sort of thing. It's not necessary to write C# code however; you can access LogParser from the command line/in shell scripts. And to extract the data you want, the LogParser function EXTRACT_TOKEN would be of use. For example, to get the UserInput portion of that query string, you could say:

LogParser -i:IISW3C "SELECT EXTRACT_TOKEN(cs-uri-query, 3, '=') FROM ex10082012.log"

(Parsing it down into the Address1, Address2, etc. will require applying more nested "extracts" of course.)

For more info on how to use EXTRACT_TOKEN, look at the LogParser.chm file that comes with the install package, or run LogParser -h FUNCTIONS EXTRACT_TOKEN from the command line.

Upvotes: 0

Rubens Farias
Rubens Farias

Reputation: 57976

Take a look here: Simple log parsing using MS Log Parser 2.2, in C#.NET

Upvotes: 2

Related Questions