简体   繁体   中英

C# System.Diagnostics.StackFrame.GetFileName always return null

I used the following code to get the name of the source code's file

StackTrace trace = new StackTrace(true);
string fileName = string.Empty;
foreach (var frame in trace.GetFrames())
{
    string fileToCheck = frame.GetFileName();
    if (someLogicToCheckTheSuitableFile())
    {
        fileName = fileToCheck;
        break;
    }
}

When I ran it at my local server, it worked well. But when I tried it at my linux server, the function "GetFileName" always returns null. Can somebody give me an advice?

StackFrame.GetFileName Method

public virtual string? GetFileName ();

Gets the file name that contains the code that is executing. This information is typically extracted from the debugging symbols for the executable. source

Full example you can finde here

After you build your application VS created pdb file (in release or debug), containing information about code call and links to your code files. This information stores in ***.pdb files which located in the same folder as executable file. You can experiment with your app (or MS Example as I linked in 2 ): build it in debug/release and run application from cmd/PS with *.pdb file and without. When pdb file exists - you will see link to code file, after you delete pdb file - no information will be presented about your code.

You can disable/enable creating pdb files as mentioned here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM