0

What are the better ways to achieve low overhead distributed logging on Azure?

iCode
  • 4,308
  • 10
  • 44
  • 77

1 Answers1

1

We use log4net as the logging library for our multi-tennant Azure Web Role. The log4net ADO.Net appender is used to log all errors to a SQL Azure database. To filter logs by client/user, we use a few log4net.GlobalContext properties, like so:

log4net.GlobalContext.Properties["Domain"] = new Log4netDomainProvider();
log4net.GlobalContext.Properties["Username"] = new Log4netUsernameProvider();

Each "Provider" is just a simple class that overrides the ToString() method. When ToString() is called by log4net, we grab a value from the current session (this technique is discussed in this SO answer).

public class Log4netDomainProvider
{
    public override string ToString()
    {
        string retval = null;

        if (HttpContext.Current == null)
            retval = "HttpContext is null";
        else if (HttpContext.Current.Session == null)
            retval = "HttpContext.Current.Session is null";
        else
            retval = HttpContext.Current.Session["Domain"];

        return retval;
    }
}

I hope this addresses your question. If so, you might want to check out this good write-up on log4net logging contexts.

Community
  • 1
  • 1
Jonathan McIntire
  • 2,665
  • 23
  • 26
  • Thanks. I will look into this. Did you also try the RoleManager.WriteToLog thing too? If so, what was your experience? – iCode Jun 29 '11 at 02:05
  • We took a look at it, but we didn't want to manually sift through a heap of logs in blob storage. One of our requirements was to easily filter by a client/user combination. I didn't mention this in my answer, but we also use log4net.GlobalContext properties for obtaining the remote ip address and machine name. If you wanted to get more elaborate with your filtering options, you could use the Azure Service Management API to store the instance #, deployment name, etc. in static variables when the role starts up. You could then create log4net "Providers" for these variables as well. – Jonathan McIntire Jun 29 '11 at 14:16