Parsing Apache Log files

This is a useful Regex that Craig wrote today, it pulls out various info from an apache log file.

privatestatic Regex __Regex =null;  internalstatic Regex _Regex  {...}{  get{...}    {  if (__Regex ==null)  {...}        {              __Regex =new Regex(@"(?<remoteHost>[^\ ]+?)\ (?<remoteIdent>[^\ ]+?)\ (?<remoteUs"+@"er>[^\ ]+?)\ \[(?<requestTime>[^\]]+?)\]\ \""(?<request>(?<r"+@"equestMethod>[^\ ]+?)?\ ?(?<requestPath>[^\ ]+?)?\ ?(?<reque"+@"stProtocol>[^\ ]+?)?)\""\ (?<statusCode>[^\ ]+?)\ (?<sizeByt"+@"es>[^\ ]+?)\ \""(?<referer>[^\""]*?)\""\ \""(?<userAgent>[^\""]"+@"+?)\""\r?\n?",                 RegexOptions.Compiled & RegexOptions.IgnoreCase & RegexOptions.IgnorePatternWhitespace & RegexOptions.CultureInvariant);        }  return __Regex;      }  }

Update: Craig's finally started writing about it, you can read the article here: Apache Log Fun

Subscribe to TSD

Don’t miss out on the latest posts. Sign up now to get access to the library of members-only posts.
jamie@example.com
Subscribe