Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Since we're all sharing debugging lessons I thought I'd include one for .NET. In System.Runtime.CompilerServices, there are attributes that can be very useful for a logging interface: CallerMemberNameAttribute, CallerLineNumberAttribute, and CallerFilePathAttribute. These add compile-time info to method calls like so:

    void Error(string message, 
    [CallerMemberName] string callingMethod = null, 
    [CallerFilePath] string filePath = null, 
    [CallerLineNumber] int lineNumber = 0);
You then simply call `Error("something wrong with the frobnicator")` and the source line you need to come back to is already there. The only caveat is that this won't work in conjunction with variadic formatting for the message (`params object[]` arguments can't coexist with default arguments). `string.Format` feels like a small price to pay.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: