How To: Obtain Method Name Programmatically For Tracing – Michael Palermo
How To: Obtain Method Name Programmatically For Tracing – Michael Palermo
I am not a fan of hard-coding method names in exception or trace messages. Here is a utility method to allow access to method name at runtime:
1 2 3 4 5 | <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> TraceContext(<span style="color: #0000ff;">string</span> messageFormat) { <span style="color: #2b91af;">Trace</span>.WriteLine(<span style="color: #0000ff;">string</span>.Format(messageFormat, <span style="color: #0000ff;">new</span> System.Diagnostics.<span style="color: #2b91af;">StackFrame</span>(1).GetMethod().Name)); } |
If I call the method above from inside another method:
1 2 3 4 | <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span> Application_AuthorizeRequest(<span style="color: #0000ff;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e) { <span style="color: #2b91af;">Tools</span>.TraceContext(<span style="color: #a31515;">"Inside of {0} event handler"</span>); } |
The resulting output is:
"Inside of Application_AuthorizeRequest event handler"
You May Also Like
How to Build in a PSMethod to your PowerShell Code
0 53 0In this video, PowerShell instructor Jason Yoder shows how to add Methods (PSMethod) to your code using free software that’s added into the PSObject. For instructor-led PowerShell courses, see our course schedule. Microsoft Windows PowerShell Training Download the Building Methods PowerShell script</a> used in this video. <# ╔══════════════════════════════════════════════════════════════════════════════╗ ║ ║ ║ Building Methods ║ ╟──────────────────────────────────────────────────────────────────────────────╢ … Continue reading How to Build in a PSMethod to your PowerShell Code
JavaScript for C# Developers – September 24, 2014
0 494 3Is JavaScript worth taking the time to learn if I’m a server-side .NET developer? How much of C# carries over to JavaScript? In this recorded video from Dan Wahlin’s webinar on September 24,2014, Dan answers these questions and more while also discussing similarities between the languages, key differences, and the future of JavaScript (ES6). If … Continue reading JavaScript for C# Developers – September 24, 2014
Subnetting a TCP/IP Network using the Magic Box Method
0 1806 5See our class schedule for complete Course Schedule Training. Classes are held in Phoenix, AZ and can be attended online from anywhere in the world with RemoteLive™. Instructor: Rick Trader Video Transcription: One of the things that we might have to do in our corporate network is to take a class of IP addresses and then subnet that into … Continue reading Subnetting a TCP/IP Network using the Magic Box Method