I’m a big fan of writing into your logs the class and method that is currently executing – it makes fault finding so much easier. Typically I’d do this with reflection – but it turns out that you can do this with a small change to Sitecore’s configuration.
The short form (repeated for posterity) is to add %c to your conversionPattern for your appender in web.config. E.g.
%4t %d{ABSOLUTE} %-5p [%c] %m%n
Seems to work on my Sitecore 7.2 instance, so I assume it will work later too. Nice one Paul George.
As an added bonus, one of my (non-sitecore) colleague pointed me to the CallerMemberNameAttribute that was introduced in .NET 4.5. I’m not sure what I think of it as an approach. On the one hand, it’s probably (i.e. almost certainly) faster than reflection. On the other hand, I quite often use an optional params array to feed a call to string.Format() as a nice way of building a log message. e.g.
public static void Log(string format, params object[] parameters)
{
string message = string.Format(format, parameters);
// Log message
}
...
Logger.Log("Weird thing happened: {0} - {1} - {2}", data1, data2, data3);
This kind of approach doesn’t really work if you’ve also got optional parameters that you have to leave empty so the CallerMemberAttribute automatically populates them. I shall ponder on this.