Get Cruuent function name, file ... (even in release build)

Source: Internet
Author: User
Tags file info stack trace
Using System;
Using System.Data;
Using System.IO;
Using System.Diagnostics;

Namespace ConsoleApplication1
{
Class Class1
{
[STAThread]
static void Main (string[] args)
{
DoWork ("Hello");
Console.WriteLine ("Press Enter to Continue ...");
Console.ReadLine ();
}

static public void DoWork (string arg)
{
StackTrace trace = new StackTrace (0/*skip frames*/, true/*need file info*/);
StackFrame frame = trace. GetFrame (0);

Console.WriteLine ("File name = {0}", frame.) GetFileName ());
Console.WriteLine ("Line no = {0}", frame. Getfilelinenumber ());
Console.WriteLine ("Function name = {0}", frame.) GetMethod ());

/*
The following text comes from a online forum:

I have tried using this and some success, but it does not seem to work
With using the release version of code.

Also, we are considering using a code obfuscator, and this would Also
Prevent things like function name from being valid when obtaining it at run
Time. I would prefer a compile time method if possible.

========================================================================
"StackTrace information would be most informative with the Debug build
Configurations. By default, the debug builds include debug symbols, while
Release builds does not. The debug symbols contain most of the file, method
Name, line number, and column information used in constructing StackFrame
and StackTrace objects. StackTrace might not the as many method calls as
Expected, due to code transformations that occur during optimization. "

If you are really want to still get full info in release builds (without
Obfuscation), then go to project/properties Select Configuration
Properties/build and set the following:

1.Optimize Code False
2.Generate Debugging Information True

Per the obfuscation bit and the point of it are to deny folks the opportunity
To reverse-engineer your code. The ability to get a good stack trace from
Obfuscated code would defeat the reasons for obfuscation

* */

Console.WriteLine ("Input arg =" + arg);
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.