C# – Best practices for logging user actions in production

asp.netclogging

I was planning on logging a lot of different stuff in my production environment, things like when a user:

  • Logs In, Logs Off
  • Change Profile
  • Edit Account settings
  • Change password

    etc

Is this a good practice to do on a production enviornment? Also what is a good way to log all this. I am currently using the following code block to log to:

public void LogMessageToFile(string msg)
        {

            System.IO.StreamWriter sw = System.IO.File.AppendText(
                GetTempPath() + @"MyLogFile.txt");
            try
            {
                string logLine = System.String.Format(
                    "{0:G}: {1}.", System.DateTime.Now, msg);
                sw.WriteLine(logLine);
            }
            finally
            {
                sw.Close();
            }
        }

Will this be ok for production? My application is very new so im not expecting millions of users right away or anything, looking for the best practices to keeping track of actions on a website or if its even best practice to.

Best Answer

This is not a direct answer to the question, more of an expansion on it.

When you launch a new app I recommend logging everything the user does: log in, log out, scratches their a**, everything. If it's web-based, consider using heat maps so you know what their mouse was doing.

When I was on the BravoX project at Xerox in the late 70's we recorded pixel-by-pixel mouse movements to figure out how users might use this weird thing called a WYSIWYG editor. We would watch playbacks of user sessions during lunch. It was extremely instructive. We discovered a use pattern we called Charlie Browning—the user would select some text and make it italic ... then they would undo ... then they would redo ... back and forth, back and forth. It turns out they were trying to understand this stuff at an emotional level. So we (Greg Kusnik did the code, if memory serves) put in some specific optimizations to support exactly this behavior.

Without the recording we would have never thought to do this.