DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Adding Comments To The IIS Log

06.07.2006
| 10970 views |
  • submit to reddit
         Adding Comments to the IIS Log

A simple way to provide more information to the IIS log files is to append it yourself with the Response.AppendToLog method. By using Session and Application events you can place keywords that you can later use when search the files. You might even be able to train some of the analysis programs to understand you keywords and hopefully provide more meaningful stats.

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires at the beginning of each request
        Response.AppendToLog("test")
    End Sub
There are some restrictions, however. Since the data you provide is appended to the URI Query portion of the log file, you are limited to 80 characters. In addition, you cannot use commas since they are a delimiter for some of the log file formats. Also, if you anticipate that there will be other querystring elements you may want to prepend your string with an ampersand. 

----

From http://psacake.com/web/ht.asp:
While trying to come up with new ways to record things that are happening on my Web server, I came across the Response Objects AppendToLog Method. This method allows you to write directly to the IIS Log File using only one line of code. This has come in handy for error trapping and troubleshooting. The following is a piece of sample code that writes a single entry to the log file:

     Response.AppendToLog "Database Being Accessed"

The above line would write the following to your IIS log file:

127.0.0.1, -, 01/01/00, 12:00:34, W3SVC1,WEBSERVER, 
127.0.0.1, 161342, 485, 228, 200, 0, get, /somefile.asp, Database Being Accessed

Remember that the IIS log file is a comma-delimited file, and you should try to avoid using commas in the data string you pass to the log file.

----

Rant From http://sqljunkies.com/WebLog/ktegels/archive/2004/01/21/795.aspx:
Since we already have IIS logging turned on, it just made sense to log to that. When we needed to harvest the data for making test case, we could just parse the logs. So, I turned a hopeful eye towards HttpResponse.AppendToLog (which is available to us in ASP.NET as Response.AppendToLog) thinking it might at least be a Copper Bullet. It works, but, where it decides to put your entry in the log could be a problem. Data written to these logs using this method is inserted where the cs-uri-query field would be rather than at the end of the record.

I suppose that this behavior makes sense if you stop to consider that the cs-uri-query field should be blank, null or otherwise meaningless for POSTs (normally). Of course, the de facto RFC doesn't say that, so IIS can get by with it.

Unless of course, you're GETting, not POSTing. But in those cases, you really don't need this trick anyway.

My whole problem with this that the "Extended Log File Format" does offer a more proper place to put this data: x-Comment. If X-Comment appeared at the end of a line, it likely wouldn't break most log parsers, and we'd have a sensible place to look for this.