Logging In Web Applications

Posted by Sumit on September 30, 2011
General Topics, Microsoft

I have seen a fair amount of discussions on this topic. Everybody knows what the logging is, but most of the people get confuse when to log and what to log. One of my friends started this conversation and we finally came up with Guide lines to do logging in a web application. I thought to share them, so here it goes.

I believe and this is coming from coding in .net 1.0 before VS was available. Debug logging should be good enough to provide us exactly how the code had flown in its execution to allow tracing the debug path. Below are guidelines that we decided (for debug)

  1.  Each log statement should exactly tell us where exactly in the code this log was written. This is best done by having log identifiers which should be of format
    1. Time : By Default
    2. Machine Name : By Default
    3. Session Id : By Default
    4. Class Name : By Default
    5. Method Name : From our log message
    6. Id within method : From our log message
    7. The message

    So a message could look like 2011-09-30 10:20:55 224 ~ Machine1~ewhfiwehfoiwe~ MyProject.DAL.MyDalClass ~ CreateUser ~ Start~ UserName=uywieyrw, Passowrd=wehfdiw

  2. Log elements should be separated by a ~. We could use any other thing but I prefer ~ because it is a very uncommon character thus opening it in excel will make life very easy.
  3. We should not log in a loop as far as possible; we should however log number of times a loop was executed.
  4. We should always log the then and else part of a log.
  5.  We are told that whenever we enter a method and exit from a method that should be logged. I have realized logging the arguments which are received by that method is more helpful. Similarly one can also log return values.
  6. Whenever inside a method you call another function and receive any data that data should be logged.
  7. Whenever you are contacting external resource and exchanging some data then, data sent and received should be logged. E.g. whenever there is a web service call or server to server post, the data sent and received should be logged as Info Logs.
  8. Finally anything going into the database and sp executed should be logged.

The best way to learn logging is not using VS for debugging and instead relying upon logs. You will end up writing best debug logs which will be helpful in production debugging.

 

Share

Tags: , , ,

7 Comments to Logging In Web Applications

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>