public void DoProcessing()
{
TraceMessage("Something happened.");
}
public void TraceMessage(string message,
[System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
System.Diagnostics.Trace.WriteLine("message: " + message);
System.Diagnostics.Trace.WriteLine("member name: " + memberName);
System.Diagnostics.Trace.WriteLine("source file path: " + sourceFilePath);
System.Diagnostics.Trace.WriteLine("source line number: " + sourceLineNumber);
}
// Sample Output:
// message: Something happened.
// member name: DoProcessing
// source file path: c:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs
// source line number: 31
Keeping track of interesting places I put my feet on in the software development world.
Search This Blog
Thursday, July 2, 2015
.NET Built-In Logging Capabilities
I just learned something nice for when you are writing some logging utilities (if you are stubborn as I am to write your own instead of using out-of-the-box frameworks such as NLog, log4net, etc).
Instead of playing with exceptions trace searching for "line number" (that's what I was up to doing...)
You can accomplish the same thing in a much more easy and elegant way:
From MSDN:
Labels:
Logging
Subscribe to:
Post Comments (Atom)
C# 6's "nameof" parameter will also come in handy here.
ReplyDelete