If you do not have a build drop location set for your build definition, the log does not get written correctly - and in fact it causes the activity to fail.
This section of code is wrong:
// log the full output of sonar to the build logs folder
IBuildDetail build = this.ActivityContext.GetExtension<IBuildDetail>();
string logFolder = Path.GetDirectoryName(build.LogLocation);
string sonarLogFile = Path.Combine(logFolder, "Sonar.log");
Essentially the log location attempts to write to c:\windows\system32 in this case which is not valid. If the drop location is not set, the log should not be written.