473,473 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I get current line number, function name, etc. like in C++?

I want to be able to determine my current line, file, and function in my C#
application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___
macros for getting this, but I cannot find a C# equivalent. Any ideas?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
va***@diebold.com
-----------------------------------
Nov 15 '05 #1
11 21876
I think you can get at least some of this from the StackFrame class.

Jonathan Schafer

On Tue, 29 Jul 2003 16:02:50 -0400, "Ken Varn" <va***@diebold.com>
wrote:
I want to be able to determine my current line, file, and function in my C#
application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___
macros for getting this, but I cannot find a C# equivalent. Any ideas?


Nov 15 '05 #2

Hi Ken,

In C#, you can use System.Diagnostics.StackTrace class to get the code’s
line number, function name, filename and other information.
My sample code was listed below:

using System;
using System.Diagnostics ;

namespace getline
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
StackTrace st=new StackTrace (0,true);

StackFrame sf=new StackFrame ();
sf=st.GetFrame (0);
Console.WriteLine ("FileName: {0}",sf.GetFileName ());
Console.WriteLine ("Line Number:
{0}",sf.GetFileLineNumber ());
Console.WriteLine ("Function Name:
{0}",sf.GetMethod ());

Console.Read ();
}
}
}

Hope this helps.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Ken Varn" <va***@diebold.com>
| From: "Ken Varn" <va***@diebold.com>
| Subject: How can I get current line number, function name, etc. like in
C++?
| Date: Tue, 29 Jul 2003 16:02:50 -0400
| Lines: 13
| Organization: Diebold Inc.
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#w**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 204.151.249.23
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:172768
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I want to be able to determine my current line, file, and function in my
C#
| application. I know that C++ has the __LINE__, __FUNCTION__, and
__FILE___
| macros for getting this, but I cannot find a C# equivalent. Any ideas?
|
| --
| -----------------------------------
| Ken Varn
| Senior Software Engineer
| Diebold Inc.
| va***@diebold.com
| -----------------------------------
|
|
|

Nov 15 '05 #3
I have used the stack frame for this, but the only problem is that I am
contemplating using a .net code obfuscator and I am concerned that this
information will not be readable as runtime data. I would prefer to be able
to get it at compile time if possible.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
va***@diebold.com
-----------------------------------
"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message
news:h3********************************@4ax.com...
I think you can get at least some of this from the StackFrame class.

Jonathan Schafer

On Tue, 29 Jul 2003 16:02:50 -0400, "Ken Varn" <va***@diebold.com>
wrote:
I want to be able to determine my current line, file, and function in my C#application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___macros for getting this, but I cannot find a C# equivalent. Any ideas?

Nov 15 '05 #4
I have tried using this with some success, but it does not seem to work well
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.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
va***@diebold.com
-----------------------------------
"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:sq**************@cpmsftngxa06.phx.gbl...

Hi Ken,

In C#, you can use System.Diagnostics.StackTrace class to get the code's
line number, function name, filename and other information.
My sample code was listed below:

using System;
using System.Diagnostics ;

namespace getline
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
StackTrace st=new StackTrace (0,true);

StackFrame sf=new StackFrame ();
sf=st.GetFrame (0);
Console.WriteLine ("FileName: {0}",sf.GetFileName ());
Console.WriteLine ("Line Number:
{0}",sf.GetFileLineNumber ());
Console.WriteLine ("Function Name:
{0}",sf.GetMethod ());

Console.Read ();
}
}
}

Hope this helps.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Ken Varn" <va***@diebold.com>
| From: "Ken Varn" <va***@diebold.com>
| Subject: How can I get current line number, function name, etc. like in
C++?
| Date: Tue, 29 Jul 2003 16:02:50 -0400
| Lines: 13
| Organization: Diebold Inc.
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#w**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 204.151.249.23
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:172768 | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I want to be able to determine my current line, file, and function in my
C#
| application. I know that C++ has the __LINE__, __FUNCTION__, and
__FILE___
| macros for getting this, but I cannot find a C# equivalent. Any ideas?
|
| --
| -----------------------------------
| Ken Varn
| Senior Software Engineer
| Diebold Inc.
| va***@diebold.com
| -----------------------------------
|
|
|

Nov 15 '05 #5
Ken,

I have tried this, but it doesn't work in a release build, even if we
create the PDB file. Is there a way to get it to work in release? We
would like to use this for logging errors that need to be looked at,
but don't effect our users experience.
BTW, any exceptions that are thrown, the call stack in those also does
not have any line numbers or filenames.

chad

v-*****@online.microsoft.com (Jeffrey Tan[MSFT]) wrote in message news:<sq**************@cpmsftngxa06.phx.gbl>...
Hi Ken,

In C#, you can use System.Diagnostics.StackTrace class to get the code?s
line number, function name, filename and other information.
My sample code was listed below:

using System;
using System.Diagnostics ;

namespace getline
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
StackTrace st=new StackTrace (0,true);

StackFrame sf=new StackFrame ();
sf=st.GetFrame (0);
Console.WriteLine ("FileName: {0}",sf.GetFileName ());
Console.WriteLine ("Line Number:
{0}",sf.GetFileLineNumber ());
Console.WriteLine ("Function Name:
{0}",sf.GetMethod ());

Console.Read ();
}
}
}

Hope this helps.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Ken Varn" <va***@diebold.com>
| From: "Ken Varn" <va***@diebold.com>
| Subject: How can I get current line number, function name, etc. like in
C++?
| Date: Tue, 29 Jul 2003 16:02:50 -0400
| Lines: 13
| Organization: Diebold Inc.
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#w**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 204.151.249.23
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:172768
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I want to be able to determine my current line, file, and function in my
C#
| application. I know that C++ has the __LINE__, __FUNCTION__, and
__FILE___
| macros for getting this, but I cannot find a C# equivalent. Any ideas?
|
| --
| -----------------------------------
| Ken Varn
| Senior Software Engineer
| Diebold Inc.
| va***@diebold.com
| -----------------------------------
|
|
|

Nov 15 '05 #6

Hi Ken,

You can find the following remark in MSDN:

"StackTrace information will be most informative with Debug build
configurations. By default, Debug builds include debug symbols, while
Release builds do 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 report as many method calls as
expected, due to code transformations that occur during optimization."

If you 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, the point of it is 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

Hope this helps.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Ken Varn" <va***@diebold.com>
| From: "Ken Varn" <va***@diebold.com>
| References: <#w**************@TK2MSFTNGP11.phx.gbl>
<sq**************@cpmsftngxa06.phx.gbl>
| Subject: Re: How can I get current line number, function name, etc. like
in C++?
| Date: Wed, 30 Jul 2003 11:48:21 -0400
| Lines: 94
| Organization: Diebold Inc.
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <eL**************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 204.151.249.23
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:172994
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I have tried using this with some success, but it does not seem to work
well
| 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.
|
| --
| -----------------------------------
| Ken Varn
| Senior Software Engineer
| Diebold Inc.
| va***@diebold.com
| -----------------------------------
| "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
| news:sq**************@cpmsftngxa06.phx.gbl...
| >
| > Hi Ken,
| >
| > In C#, you can use System.Diagnostics.StackTrace class to get the code's
| > line number, function name, filename and other information.
| > My sample code was listed below:
| >
| > using System;
| > using System.Diagnostics ;
| >
| > namespace getline
| > {
| > class Class1
| > {
| > [STAThread]
| > static void Main(string[] args)
| > {
| > StackTrace st=new StackTrace (0,true);
| >
| > StackFrame sf=new StackFrame ();
| > sf=st.GetFrame (0);
| > Console.WriteLine ("FileName: {0}",sf.GetFileName ());
| > Console.WriteLine ("Line Number:
| > {0}",sf.GetFileLineNumber ());
| > Console.WriteLine ("Function Name:
| > {0}",sf.GetMethod ());
| >
| > Console.Read ();
| > }
| > }
| > }
| >
| > Hope this helps.
| >
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | Reply-To: "Ken Varn" <va***@diebold.com>
| > | From: "Ken Varn" <va***@diebold.com>
| > | Subject: How can I get current line number, function name, etc. like
in
| > C++?
| > | Date: Tue, 29 Jul 2003 16:02:50 -0400
| > | Lines: 13
| > | Organization: Diebold Inc.
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <#w**************@TK2MSFTNGP11.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: 204.151.249.23
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:172768
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | I want to be able to determine my current line, file, and function in
my
| > C#
| > | application. I know that C++ has the __LINE__, __FUNCTION__, and
| > __FILE___
| > | macros for getting this, but I cannot find a C# equivalent. Any
ideas?
| > |
| > | --
| > | -----------------------------------
| > | Ken Varn
| > | Senior Software Engineer
| > | Diebold Inc.
| > | va***@diebold.com
| > | -----------------------------------
| > |
| > |
| > |
| >
|
|
|

Nov 15 '05 #7
In regards to obfuscation, I understand your point. However, as you
stated, I am trying to keep others from reverse engineering my code, not
myself. I still need the stack trace data to be able to troubleshoot
problems in the field. If some of this information were available only at
compile time (such as function, line number, etc..), then I would be the
only one accessing it, not anyone else. I was able to accomplish this in
C++ using __LINE__, __FILE__, and __FUNCTION__, but not in C#.
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
va***@diebold.com
-----------------------------------
"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:8$**************@cpmsftngxa06.phx.gbl...

Hi Ken,

You can find the following remark in MSDN:

"StackTrace information will be most informative with Debug build
configurations. By default, Debug builds include debug symbols, while
Release builds do 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 report as many method calls as expected, due to code transformations that occur during optimization."

If you 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, the point of it is 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

Hope this helps.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Ken Varn" <va***@diebold.com>
| From: "Ken Varn" <va***@diebold.com>
| References: <#w**************@TK2MSFTNGP11.phx.gbl>
<sq**************@cpmsftngxa06.phx.gbl>
| Subject: Re: How can I get current line number, function name, etc. like in C++?
| Date: Wed, 30 Jul 2003 11:48:21 -0400
| Lines: 94
| Organization: Diebold Inc.
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <eL**************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 204.151.249.23
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:172994 | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I have tried using this with some success, but it does not seem to work
well
| 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.
|
| --
| -----------------------------------
| Ken Varn
| Senior Software Engineer
| Diebold Inc.
| va***@diebold.com
| -----------------------------------
| "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
| news:sq**************@cpmsftngxa06.phx.gbl...
| >
| > Hi Ken,
| >
| > In C#, you can use System.Diagnostics.StackTrace class to get the code's | > line number, function name, filename and other information.
| > My sample code was listed below:
| >
| > using System;
| > using System.Diagnostics ;
| >
| > namespace getline
| > {
| > class Class1
| > {
| > [STAThread]
| > static void Main(string[] args)
| > {
| > StackTrace st=new StackTrace (0,true);
| >
| > StackFrame sf=new StackFrame ();
| > sf=st.GetFrame (0);
| > Console.WriteLine ("FileName: {0}",sf.GetFileName ());
| > Console.WriteLine ("Line Number:
| > {0}",sf.GetFileLineNumber ());
| > Console.WriteLine ("Function Name:
| > {0}",sf.GetMethod ());
| >
| > Console.Read ();
| > }
| > }
| > }
| >
| > Hope this helps.
| >
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | Reply-To: "Ken Varn" <va***@diebold.com>
| > | From: "Ken Varn" <va***@diebold.com>
| > | Subject: How can I get current line number, function name, etc. like in
| > C++?
| > | Date: Tue, 29 Jul 2003 16:02:50 -0400
| > | Lines: 13
| > | Organization: Diebold Inc.
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <#w**************@TK2MSFTNGP11.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: 204.151.249.23
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:172768
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | I want to be able to determine my current line, file, and function in my
| > C#
| > | application. I know that C++ has the __LINE__, __FUNCTION__, and
| > __FILE___
| > | macros for getting this, but I cannot find a C# equivalent. Any
ideas?
| > |
| > | --
| > | -----------------------------------
| > | Ken Varn
| > | Senior Software Engineer
| > | Diebold Inc.
| > | va***@diebold.com
| > | -----------------------------------
| > |
| > |
| > |
| >
|
|
|

Nov 15 '05 #8

Hi Ken,

So I provided you a solution of turning off the code optimize, then you can
use it in the release mode.
And your teammate also can use it to get these information.

Go to Project/Properties select Configuration
Properties/Build and set the following:

1.Optimize Code false
2.Generate Debugging Information true

After finishing getting your information, you can obfuscate your
application.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Ken Varn" <va***@diebold.com>
| From: "Ken Varn" <va***@diebold.com>
| References: <#w**************@TK2MSFTNGP11.phx.gbl>
<sq**************@cpmsftngxa06.phx.gbl>
<eL**************@tk2msftngp13.phx.gbl>
<8$**************@cpmsftngxa06.phx.gbl>
| Subject: Re: How can I get current line number, function name, etc. like
in C++?
| Date: Mon, 4 Aug 2003 09:48:07 -0400
| Lines: 179
| Organization: Diebold Inc.
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#z*************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 204.151.249.23
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:173974
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| In regards to obfuscation, I understand your point. However, as you
| stated, I am trying to keep others from reverse engineering my code, not
| myself. I still need the stack trace data to be able to troubleshoot
| problems in the field. If some of this information were available only at
| compile time (such as function, line number, etc..), then I would be the
| only one accessing it, not anyone else. I was able to accomplish this in
| C++ using __LINE__, __FILE__, and __FUNCTION__, but not in C#.
|
|
| --
| -----------------------------------
| Ken Varn
| Senior Software Engineer
| Diebold Inc.
| va***@diebold.com
| -----------------------------------
| "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
| news:8$**************@cpmsftngxa06.phx.gbl...
| >
| > Hi Ken,
| >
| > You can find the following remark in MSDN:
| >
| > "StackTrace information will be most informative with Debug build
| > configurations. By default, Debug builds include debug symbols, while
| > Release builds do 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 report as many method calls
| as
| > expected, due to code transformations that occur during optimization."
| >
| > If you 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, the point of it is 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
| >
| > Hope this helps.
| >
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | Reply-To: "Ken Varn" <va***@diebold.com>
| > | From: "Ken Varn" <va***@diebold.com>
| > | References: <#w**************@TK2MSFTNGP11.phx.gbl>
| > <sq**************@cpmsftngxa06.phx.gbl>
| > | Subject: Re: How can I get current line number, function name, etc.
| like
| > in C++?
| > | Date: Wed, 30 Jul 2003 11:48:21 -0400
| > | Lines: 94
| > | Organization: Diebold Inc.
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <eL**************@tk2msftngp13.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: 204.151.249.23
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:172994
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | I have tried using this with some success, but it does not seem to
work
| > well
| > | 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.
| > |
| > | --
| > | -----------------------------------
| > | Ken Varn
| > | Senior Software Engineer
| > | Diebold Inc.
| > | va***@diebold.com
| > | -----------------------------------
| > | "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
| > | news:sq**************@cpmsftngxa06.phx.gbl...
| > | >
| > | > Hi Ken,
| > | >
| > | > In C#, you can use System.Diagnostics.StackTrace class to get the
| code's
| > | > line number, function name, filename and other information.
| > | > My sample code was listed below:
| > | >
| > | > using System;
| > | > using System.Diagnostics ;
| > | >
| > | > namespace getline
| > | > {
| > | > class Class1
| > | > {
| > | > [STAThread]
| > | > static void Main(string[] args)
| > | > {
| > | > StackTrace st=new StackTrace (0,true);
| > | >
| > | > StackFrame sf=new StackFrame ();
| > | > sf=st.GetFrame (0);
| > | > Console.WriteLine ("FileName: {0}",sf.GetFileName ());
| > | > Console.WriteLine ("Line Number:
| > | > {0}",sf.GetFileLineNumber ());
| > | > Console.WriteLine ("Function Name:
| > | > {0}",sf.GetMethod ());
| > | >
| > | > Console.Read ();
| > | > }
| > | > }
| > | > }
| > | >
| > | > Hope this helps.
| > | >
| > | > Jeffrey Tan
| > | > Microsoft Online Partner Support
| > | > Get Secure! - www.microsoft.com/security
| > | > This posting is provided "as is" with no warranties and confers no
| > rights.
| > | >
| > | > --------------------
| > | > | Reply-To: "Ken Varn" <va***@diebold.com>
| > | > | From: "Ken Varn" <va***@diebold.com>
| > | > | Subject: How can I get current line number, function name, etc.
| like
| > in
| > | > C++?
| > | > | Date: Tue, 29 Jul 2003 16:02:50 -0400
| > | > | Lines: 13
| > | > | Organization: Diebold Inc.
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | > | Message-ID: <#w**************@TK2MSFTNGP11.phx.gbl>
| > | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | > | NNTP-Posting-Host: 204.151.249.23
| > | > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| > | > | Xref: cpmsftngxa06.phx.gbl
| > | microsoft.public.dotnet.languages.csharp:172768
| > | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > | > |
| > | > | I want to be able to determine my current line, file, and function
| in
| > my
| > | > C#
| > | > | application. I know that C++ has the __LINE__, __FUNCTION__, and
| > | > __FILE___
| > | > | macros for getting this, but I cannot find a C# equivalent. Any
| > ideas?
| > | > |
| > | > | --
| > | > | -----------------------------------
| > | > | Ken Varn
| > | > | Senior Software Engineer
| > | > | Diebold Inc.
| > | > | va***@diebold.com
| > | > | -----------------------------------
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|

Nov 15 '05 #9
Jeffrey,

I have tried this, and I still can't get filename and line numbers in
release builds. Is there something I am missing... BTW, this is an
ASP.NET app.

chad

v-*****@online.microsoft.com (Jeffrey Tan[MSFT]) wrote in message news:<AE**************@cpmsftngxa06.phx.gbl>...
Hi Ken,

So I provided you a solution of turning off the code optimize, then you can
use it in the release mode.
And your teammate also can use it to get these information.

Go to Project/Properties select Configuration
Properties/Build and set the following:

1.Optimize Code false
2.Generate Debugging Information true

After finishing getting your information, you can obfuscate your
application.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Ken Varn" <va***@diebold.com>
| From: "Ken Varn" <va***@diebold.com>
| References: <#w**************@TK2MSFTNGP11.phx.gbl>
<sq**************@cpmsftngxa06.phx.gbl>
<eL**************@tk2msftngp13.phx.gbl>
<8$**************@cpmsftngxa06.phx.gbl>
| Subject: Re: How can I get current line number, function name, etc. like
in C++?
| Date: Mon, 4 Aug 2003 09:48:07 -0400
| Lines: 179
| Organization: Diebold Inc.
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#z*************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 204.151.249.23
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:173974
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| In regards to obfuscation, I understand your point. However, as you
| stated, I am trying to keep others from reverse engineering my code, not
| myself. I still need the stack trace data to be able to troubleshoot
| problems in the field. If some of this information were available only at
| compile time (such as function, line number, etc..), then I would be the
| only one accessing it, not anyone else. I was able to accomplish this in
| C++ using __LINE__, __FILE__, and __FUNCTION__, but not in C#.
|
|
| --
| -----------------------------------
| Ken Varn
| Senior Software Engineer
| Diebold Inc.
| va***@diebold.com
| -----------------------------------
| "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
| news:8$**************@cpmsftngxa06.phx.gbl...
| >
| > Hi Ken,
| >
| > You can find the following remark in MSDN:
| >
| > "StackTrace information will be most informative with Debug build
| > configurations. By default, Debug builds include debug symbols, while
| > Release builds do 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 report as many method calls
as
| > expected, due to code transformations that occur during optimization."
| >
| > If you 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, the point of it is 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
| >
| > Hope this helps.
| >
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | Reply-To: "Ken Varn" <va***@diebold.com>
| > | From: "Ken Varn" <va***@diebold.com>
| > | References: <#w**************@TK2MSFTNGP11.phx.gbl>
<sq**************@cpmsftngxa06.phx.gbl>
| > | Subject: Re: How can I get current line number, function name, etc.
like
in C++?
| > | Date: Wed, 30 Jul 2003 11:48:21 -0400
| > | Lines: 94
| > | Organization: Diebold Inc.
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <eL**************@tk2msftngp13.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: 204.151.249.23
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:172994
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | I have tried using this with some success, but it does not seem to
work
well
| > | 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.
| > |
| > | --
| > | -----------------------------------
| > | Ken Varn
| > | Senior Software Engineer
| > | Diebold Inc.
| > | va***@diebold.com
| > | -----------------------------------
| > | "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
| > | news:sq**************@cpmsftngxa06.phx.gbl...
| > | >
| > | > Hi Ken,
| > | >
| > | > In C#, you can use System.Diagnostics.StackTrace class to get the
code's
| > | > line number, function name, filename and other information.
| > | > My sample code was listed below:
| > | >
| > | > using System;
| > | > using System.Diagnostics ;
| > | >
| > | > namespace getline
| > | > {
| > | > class Class1
| > | > {
| > | > [STAThread]
| > | > static void Main(string[] args)
| > | > {
| > | > StackTrace st=new StackTrace (0,true);
| > | >
| > | > StackFrame sf=new StackFrame ();
| > | > sf=st.GetFrame (0);
| > | > Console.WriteLine ("FileName: {0}",sf.GetFileName ());
| > | > Console.WriteLine ("Line Number:
| > | > {0}",sf.GetFileLineNumber ());
| > | > Console.WriteLine ("Function Name:
| > | > {0}",sf.GetMethod ());
| > | >
| > | > Console.Read ();
| > | > }
| > | > }
| > | > }
| > | >
| > | > Hope this helps.
| > | >
| > | > Jeffrey Tan
| > | > Microsoft Online Partner Support
| > | > Get Secure! - www.microsoft.com/security
| > | > This posting is provided "as is" with no warranties and confers no
rights.
| > | >
| > | > --------------------
| > | > | Reply-To: "Ken Varn" <va***@diebold.com>
| > | > | From: "Ken Varn" <va***@diebold.com>
| > | > | Subject: How can I get current line number, function name, etc.
like
in
C++?
| > | > | Date: Tue, 29 Jul 2003 16:02:50 -0400
| > | > | Lines: 13
| > | > | Organization: Diebold Inc.
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | > | Message-ID: <#w**************@TK2MSFTNGP11.phx.gbl>
| > | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | > | NNTP-Posting-Host: 204.151.249.23
| > | > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| > | > | Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:172768
| > | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > | > |
| > | > | I want to be able to determine my current line, file, and function
in
my
C#
| > | > | application. I know that C++ has the __LINE__, __FUNCTION__, and
__FILE___
| > | > | macros for getting this, but I cannot find a C# equivalent. Any
ideas?
| > | > |
| > | > | --
| > | > | -----------------------------------
| > | > | Ken Varn
| > | > | Senior Software Engineer
| > | > | Diebold Inc.
| > | > | va***@diebold.com
| > | > | -----------------------------------
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|

Nov 15 '05 #10

Hi cyost,

Are you sure you have followed my steps to close the code optimization and
add debug information?
On my machine, after these two steps, the release application worked well.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: cy***@charter.net (Chad Yost)
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: Re: How can I get current line number, function name, etc. like
in C++?
| Date: 6 Aug 2003 07:30:01 -0700
| Organization: http://groups.google.com/
| Lines: 240
| Message-ID: <29**************************@posting.google.com >
| References: <#w**************@TK2MSFTNGP11.phx.gbl>
<sq**************@cpmsftngxa06.phx.gbl>
<eL**************@tk2msftngp13.phx.gbl>
<8$**************@cpmsftngxa06.phx.gbl>
<#z*************@TK2MSFTNGP10.phx.gbl>
<AE**************@cpmsftngxa06.phx.gbl>
| NNTP-Posting-Host: 167.68.1.65
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1060180202 14222 127.0.0.1 (6 Aug 2003
14:30:02 GMT)
| X-Complaints-To: gr**********@google.com
| NNTP-Posting-Date: 6 Aug 2003 14:30:02 GMT
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!newsfee
d01.sul.t-online.de!t-online.de!newspeer1-gui.server.ntli.net!ntli.net!in.10
0proofnews.com!in.100proofnews.com!elnk-atl-nf1!newsfeed.earthlink.net!newsh
osting.com!news-xfer2.atl.newshosting.com!diablo.voicenet.com!tdsn et-transit
!newspeer.tds.net!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-09!supernews.com!post
news1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:174622
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Jeffrey,
|
| I have tried this, and I still can't get filename and line numbers in
| release builds. Is there something I am missing... BTW, this is an
| ASP.NET app.
|
| chad
|
| v-*****@online.microsoft.com (Jeffrey Tan[MSFT]) wrote in message
news:<AE**************@cpmsftngxa06.phx.gbl>...
| > Hi Ken,
| >
| > So I provided you a solution of turning off the code optimize, then you
can
| > use it in the release mode.
| > And your teammate also can use it to get these information.
| >
| > Go to Project/Properties select Configuration
| > Properties/Build and set the following:
| >
| > 1.Optimize Code false
| > 2.Generate Debugging Information true
| >
| > After finishing getting your information, you can obfuscate your
| > application.
| >
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | Reply-To: "Ken Varn" <va***@diebold.com>
| > | From: "Ken Varn" <va***@diebold.com>
| > | References: <#w**************@TK2MSFTNGP11.phx.gbl>
| > <sq**************@cpmsftngxa06.phx.gbl>
| > <eL**************@tk2msftngp13.phx.gbl>
| > <8$**************@cpmsftngxa06.phx.gbl>
| > | Subject: Re: How can I get current line number, function name, etc.
like
| > in C++?
| > | Date: Mon, 4 Aug 2003 09:48:07 -0400
| > | Lines: 179
| > | Organization: Diebold Inc.
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <#z*************@TK2MSFTNGP10.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: 204.151.249.23
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:173974
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | In regards to obfuscation, I understand your point. However, as you
| > | stated, I am trying to keep others from reverse engineering my code,
not
| > | myself. I still need the stack trace data to be able to troubleshoot
| > | problems in the field. If some of this information were available
only at
| > | compile time (such as function, line number, etc..), then I would be
the
| > | only one accessing it, not anyone else. I was able to accomplish
this in
| > | C++ using __LINE__, __FILE__, and __FUNCTION__, but not in C#.
| > |
| > |
| > | --
| > | -----------------------------------
| > | Ken Varn
| > | Senior Software Engineer
| > | Diebold Inc.
| > | va***@diebold.com
| > | -----------------------------------
| > | "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
| > | news:8$**************@cpmsftngxa06.phx.gbl...
| > | >
| > | > Hi Ken,
| > | >
| > | > You can find the following remark in MSDN:
| > | >
| > | > "StackTrace information will be most informative with Debug build
| > | > configurations. By default, Debug builds include debug symbols,
while
| > | > Release builds do 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 report as many method
calls
| > as
| > | > expected, due to code transformations that occur during
optimization."
| > | >
| > | > If you 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, the point of it is 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
| > | >
| > | > Hope this helps.
| > | >
| > | > Jeffrey Tan
| > | > Microsoft Online Partner Support
| > | > Get Secure! - www.microsoft.com/security
| > | > This posting is provided "as is" with no warranties and confers no
| > rights.
| > | >
| > | > --------------------
| > | > | Reply-To: "Ken Varn" <va***@diebold.com>
| > | > | From: "Ken Varn" <va***@diebold.com>
| > | > | References: <#w**************@TK2MSFTNGP11.phx.gbl>
| > <sq**************@cpmsftngxa06.phx.gbl>
| > | > | Subject: Re: How can I get current line number, function name,
etc.
| > like
| > in C++?
| > | > | Date: Wed, 30 Jul 2003 11:48:21 -0400
| > | > | Lines: 94
| > | > | Organization: Diebold Inc.
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | > | Message-ID: <eL**************@tk2msftngp13.phx.gbl>
| > | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | > | NNTP-Posting-Host: 204.151.249.23
| > | > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| > | > | Xref: cpmsftngxa06.phx.gbl
| > microsoft.public.dotnet.languages.csharp:172994
| > | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > | > |
| > | > | I have tried using this with some success, but it does not seem
to
| > work
| > well
| > | > | 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.
| > | > |
| > | > | --
| > | > | -----------------------------------
| > | > | Ken Varn
| > | > | Senior Software Engineer
| > | > | Diebold Inc.
| > | > | va***@diebold.com
| > | > | -----------------------------------
| > | > | "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in
message
| > | > | news:sq**************@cpmsftngxa06.phx.gbl...
| > | > | >
| > | > | > Hi Ken,
| > | > | >
| > | > | > In C#, you can use System.Diagnostics.StackTrace class to get
the
| > code's
| > | > | > line number, function name, filename and other information.
| > | > | > My sample code was listed below:
| > | > | >
| > | > | > using System;
| > | > | > using System.Diagnostics ;
| > | > | >
| > | > | > namespace getline
| > | > | > {
| > | > | > class Class1
| > | > | > {
| > | > | > [STAThread]
| > | > | > static void Main(string[] args)
| > | > | > {
| > | > | > StackTrace st=new StackTrace (0,true);
| > | > | >
| > | > | > StackFrame sf=new StackFrame ();
| > | > | > sf=st.GetFrame (0);
| > | > | > Console.WriteLine ("FileName: {0}",sf.GetFileName ());
| > | > | > Console.WriteLine ("Line
Number:
| > | > | > {0}",sf.GetFileLineNumber ());
| > | > | > Console.WriteLine ("Function
Name:
| > | > | > {0}",sf.GetMethod ());
| > | > | >
| > | > | > Console.Read ();
| > | > | > }
| > | > | > }
| > | > | > }
| > | > | >
| > | > | > Hope this helps.
| > | > | >
| > | > | > Jeffrey Tan
| > | > | > Microsoft Online Partner Support
| > | > | > Get Secure! - www.microsoft.com/security
| > | > | > This posting is provided "as is" with no warranties and confers
no
| > rights.
| > | > | >
| > | > | > --------------------
| > | > | > | Reply-To: "Ken Varn" <va***@diebold.com>
| > | > | > | From: "Ken Varn" <va***@diebold.com>
| > | > | > | Subject: How can I get current line number, function name,
etc.
| > like
| > in
| > C++?
| > | > | > | Date: Tue, 29 Jul 2003 16:02:50 -0400
| > | > | > | Lines: 13
| > | > | > | Organization: Diebold Inc.
| > | > | > | X-Priority: 3
| > | > | > | X-MSMail-Priority: Normal
| > | > | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | > | > | Message-ID: <#w**************@TK2MSFTNGP11.phx.gbl>
| > | > | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | > | > | NNTP-Posting-Host: 204.151.249.23
| > | > | > | Path:
| > cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| > | > | > | Xref: cpmsftngxa06.phx.gbl
| > microsoft.public.dotnet.languages.csharp:172768
| > | > | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > | > | > |
| > | > | > | I want to be able to determine my current line, file, and
function
| > in
| > my
| > C#
| > | > | > | application. I know that C++ has the __LINE__, __FUNCTION__,
and
| > __FILE___
| > | > | > | macros for getting this, but I cannot find a C# equivalent.
Any
| > ideas?
| > | > | > |
| > | > | > | --
| > | > | > | -----------------------------------
| > | > | > | Ken Varn
| > | > | > | Senior Software Engineer
| > | > | > | Diebold Inc.
| > | > | > | va***@diebold.com
| > | > | > | -----------------------------------
| > | > | > |
| > | > | > |
| > | > | > |
| > | > | >
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
|

Nov 15 '05 #11

Hi cyost,

Is there still any unclear?
Please feel free to let me know, I am glad I can help you.

Best regards,

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| X-Tomcat-ID: 132354918
| References: <#w**************@TK2MSFTNGP11.phx.gbl>
<sq**************@cpmsftngxa06.phx.gbl>
<eL**************@tk2msftngp13.phx.gbl>
<8$**************@cpmsftngxa06.phx.gbl>
<#z*************@TK2MSFTNGP10.phx.gbl>
<AE**************@cpmsftngxa06.phx.gbl>
<29**************************@posting.google.com >
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: v-*****@online.microsoft.com (Jeffrey Tan[MSFT])
| Organization: Microsoft
| Date: Thu, 07 Aug 2003 06:14:58 GMT
| Subject: Re: How can I get current line number, function name, etc. like
in C++?
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| Message-ID: <ju**************@cpmsftngxa06.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Lines: 280
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:174774
| NNTP-Posting-Host: TOMCATIMPORT2 10.201.218.182
|
|
| Hi cyost,
|
| Are you sure you have followed my steps to close the code optimization
and
| add debug information?
| On my machine, after these two steps, the release application worked well.
|
| Jeffrey Tan
| Microsoft Online Partner Support
| Get Secure! - www.microsoft.com/security
| This posting is provided "as is" with no warranties and confers no rights.
|
| --------------------
| | From: cy***@charter.net (Chad Yost)
| | Newsgroups: microsoft.public.dotnet.languages.csharp
| | Subject: Re: How can I get current line number, function name, etc.
like
| in C++?
| | Date: 6 Aug 2003 07:30:01 -0700
| | Organization: http://groups.google.com/
| | Lines: 240
| | Message-ID: <29**************************@posting.google.com >
| | References: <#w**************@TK2MSFTNGP11.phx.gbl>
| <sq**************@cpmsftngxa06.phx.gbl>
| <eL**************@tk2msftngp13.phx.gbl>
| <8$**************@cpmsftngxa06.phx.gbl>
| <#z*************@TK2MSFTNGP10.phx.gbl>
| <AE**************@cpmsftngxa06.phx.gbl>
| | NNTP-Posting-Host: 167.68.1.65
| | Content-Type: text/plain; charset=ISO-8859-1
| | Content-Transfer-Encoding: 8bit
| | X-Trace: posting.google.com 1060180202 14222 127.0.0.1 (6 Aug 2003
| 14:30:02 GMT)
| | X-Complaints-To: gr**********@google.com
| | NNTP-Posting-Date: 6 Aug 2003 14:30:02 GMT
| | Path:
|
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!newsfee
|
d01.sul.t-online.de!t-online.de!newspeer1-gui.server.ntli.net!ntli.net!in.10
|
0proofnews.com!in.100proofnews.com!elnk-atl-nf1!newsfeed.earthlink.net!newsh
|
osting.com!news-xfer2.atl.newshosting.com!diablo.voicenet.com!tdsn et-transit
|
!newspeer.tds.net!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-09!supernews.com!post
| news1.google.com!not-for-mail
| | Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:174622
| | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| |
| | Jeffrey,
| |
| | I have tried this, and I still can't get filename and line numbers in
| | release builds. Is there something I am missing... BTW, this is an
| | ASP.NET app.
| |
| | chad
| |
| | v-*****@online.microsoft.com (Jeffrey Tan[MSFT]) wrote in message
| news:<AE**************@cpmsftngxa06.phx.gbl>...
| | > Hi Ken,
| | >
| | > So I provided you a solution of turning off the code optimize, then
you
| can
| | > use it in the release mode.
| | > And your teammate also can use it to get these information.
| | >
| | > Go to Project/Properties select Configuration
| | > Properties/Build and set the following:
| | >
| | > 1.Optimize Code false
| | > 2.Generate Debugging Information true
| | >
| | > After finishing getting your information, you can obfuscate your
| | > application.
| | >
| | > Jeffrey Tan
| | > Microsoft Online Partner Support
| | > Get Secure! - www.microsoft.com/security
| | > This posting is provided "as is" with no warranties and confers no
| rights.
| | >
| | > --------------------
| | > | Reply-To: "Ken Varn" <va***@diebold.com>
| | > | From: "Ken Varn" <va***@diebold.com>
| | > | References: <#w**************@TK2MSFTNGP11.phx.gbl>
| | > <sq**************@cpmsftngxa06.phx.gbl>
| | > <eL**************@tk2msftngp13.phx.gbl>
| | > <8$**************@cpmsftngxa06.phx.gbl>
| | > | Subject: Re: How can I get current line number, function name, etc.

| like
| | > in C++?
| | > | Date: Mon, 4 Aug 2003 09:48:07 -0400
| | > | Lines: 179
| | > | Organization: Diebold Inc.
| | > | X-Priority: 3
| | > | X-MSMail-Priority: Normal
| | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| | > | Message-ID: <#z*************@TK2MSFTNGP10.phx.gbl>
| | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| | > | NNTP-Posting-Host: 204.151.249.23
| | > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| | > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:173974
| | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| | > |
| | > | In regards to obfuscation, I understand your point. However, as
you
| | > | stated, I am trying to keep others from reverse engineering my
code,
| not
| | > | myself. I still need the stack trace data to be able to
troubleshoot
| | > | problems in the field. If some of this information were available
| only at
| | > | compile time (such as function, line number, etc..), then I would
be
| the
| | > | only one accessing it, not anyone else. I was able to accomplish
| this in
| | > | C++ using __LINE__, __FILE__, and __FUNCTION__, but not in C#.
| | > |
| | > |
| | > | --
| | > | -----------------------------------
| | > | Ken Varn
| | > | Senior Software Engineer
| | > | Diebold Inc.
| | > | va***@diebold.com
| | > | -----------------------------------
| | > | "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
| | > | news:8$**************@cpmsftngxa06.phx.gbl...
| | > | >
| | > | > Hi Ken,
| | > | >
| | > | > You can find the following remark in MSDN:
| | > | >
| | > | > "StackTrace information will be most informative with Debug build
| | > | > configurations. By default, Debug builds include debug symbols,
| while
| | > | > Release builds do 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 report as many
method
| calls
| | > as
| | > | > expected, due to code transformations that occur during
| optimization."
| | > | >
| | > | > If you 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, the point of it is 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
| | > | >
| | > | > Hope this helps.
| | > | >
| | > | > Jeffrey Tan
| | > | > Microsoft Online Partner Support
| | > | > Get Secure! - www.microsoft.com/security
| | > | > This posting is provided "as is" with no warranties and confers
no
| | > rights.
| | > | >
| | > | > --------------------
| | > | > | Reply-To: "Ken Varn" <va***@diebold.com>
| | > | > | From: "Ken Varn" <va***@diebold.com>
| | > | > | References: <#w**************@TK2MSFTNGP11.phx.gbl>
| | > <sq**************@cpmsftngxa06.phx.gbl>
| | > | > | Subject: Re: How can I get current line number, function name,
| etc.
| | > like
| | > in C++?
| | > | > | Date: Wed, 30 Jul 2003 11:48:21 -0400
| | > | > | Lines: 94
| | > | > | Organization: Diebold Inc.
| | > | > | X-Priority: 3
| | > | > | X-MSMail-Priority: Normal
| | > | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| | > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| | > | > | Message-ID: <eL**************@tk2msftngp13.phx.gbl>
| | > | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| | > | > | NNTP-Posting-Host: 204.151.249.23
| | > | > | Path:
| cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| | > | > | Xref: cpmsftngxa06.phx.gbl
| | > microsoft.public.dotnet.languages.csharp:172994
| | > | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| | > | > |
| | > | > | I have tried using this with some success, but it does not seem
| to
| | > work
| | > well
| | > | > | 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.
| | > | > |
| | > | > | --
| | > | > | -----------------------------------
| | > | > | Ken Varn
| | > | > | Senior Software Engineer
| | > | > | Diebold Inc.
| | > | > | va***@diebold.com
| | > | > | -----------------------------------
| | > | > | "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in
| message
| | > | > | news:sq**************@cpmsftngxa06.phx.gbl...
| | > | > | >
| | > | > | > Hi Ken,
| | > | > | >
| | > | > | > In C#, you can use System.Diagnostics.StackTrace class to get
| the
| | > code's
| | > | > | > line number, function name, filename and other information.
| | > | > | > My sample code was listed below:
| | > | > | >
| | > | > | > using System;
| | > | > | > using System.Diagnostics ;
| | > | > | >
| | > | > | > namespace getline
| | > | > | > {
| | > | > | > class Class1
| | > | > | > {
| | > | > | > [STAThread]
| | > | > | > static void Main(string[] args)
| | > | > | > {
| | > | > | > StackTrace st=new StackTrace (0,true);
| | > | > | >
| | > | > | > StackFrame sf=new StackFrame ();
| | > | > | > sf=st.GetFrame (0);
| | > | > | > Console.WriteLine ("FileName: {0}",sf.GetFileName ());
| | > | > | > Console.WriteLine ("Line
| Number:
| | > | > | > {0}",sf.GetFileLineNumber ());
| | > | > | > Console.WriteLine ("Function
| Name:
| | > | > | > {0}",sf.GetMethod ());
| | > | > | >
| | > | > | > Console.Read ();
| | > | > | > }
| | > | > | > }
| | > | > | > }
| | > | > | >
| | > | > | > Hope this helps.
| | > | > | >
| | > | > | > Jeffrey Tan
| | > | > | > Microsoft Online Partner Support
| | > | > | > Get Secure! - www.microsoft.com/security
| | > | > | > This posting is provided "as is" with no warranties and
confers
| no
| | > rights.
| | > | > | >
| | > | > | > --------------------
| | > | > | > | Reply-To: "Ken Varn" <va***@diebold.com>
| | > | > | > | From: "Ken Varn" <va***@diebold.com>
| | > | > | > | Subject: How can I get current line number, function name,
| etc.
| | > like
| | > in
| | > C++?
| | > | > | > | Date: Tue, 29 Jul 2003 16:02:50 -0400
| | > | > | > | Lines: 13
| | > | > | > | Organization: Diebold Inc.
| | > | > | > | X-Priority: 3
| | > | > | > | X-MSMail-Priority: Normal
| | > | > | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| | > | > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| | > | > | > | Message-ID: <#w**************@TK2MSFTNGP11.phx.gbl>
| | > | > | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| | > | > | > | NNTP-Posting-Host: 204.151.249.23
| | > | > | > | Path:
| | > cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| | > | > | > | Xref: cpmsftngxa06.phx.gbl
| | > microsoft.public.dotnet.languages.csharp:172768
| | > | > | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| | > | > | > |
| | > | > | > | I want to be able to determine my current line, file, and
| function
| | > in
| | > my
| | > C#
| | > | > | > | application. I know that C++ has the __LINE__,
__FUNCTION__,
| and
| | > __FILE___
| | > | > | > | macros for getting this, but I cannot find a C# equivalent.

| Any
| | > ideas?
| | > | > | > |
| | > | > | > | --
| | > | > | > | -----------------------------------
| | > | > | > | Ken Varn
| | > | > | > | Senior Software Engineer
| | > | > | > | Diebold Inc.
| | > | > | > | va***@diebold.com
| | > | > | > | -----------------------------------
| | > | > | > |
| | > | > | > |
| | > | > | > |
| | > | > | >
| | > | > |
| | > | > |
| | > | > |
| | > | >
| | > |
| | > |
| | > |
| |
|
|

Nov 15 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: aa | last post by:
ftell($fp) returns the position os the cursor in a line Is there a function which returns the line number at which the cursor is?
2
by: David Abrahams | last post by:
Is there any way to determine the file and line number (if any) of a method's invocation from within its body? Many Thanks in advance, Dave -- David Abrahams Boost Consulting...
1
by: Marek Prerovsky | last post by:
Hello, I implemented some Python functions in my C/C++ code. I need to know the Python source file name and line number of just executed Python command which calls my function. How can I get...
5
by: Marge Inoferror | last post by:
Well, 2 questions, actually... I get an error when my form's window gets covered by another window. I can resize sucessfully all day, but when the window is covered by another window - it bombs ...
5
by: Mark Kamoski | last post by:
Hi Everyone-- How can one get the line number of where an error was thrown and/or caught? For example, note the following, for use at any given point in a piece of code: ....to get the...
1
by: scottrm | last post by:
I want to get the file name, line number etc. in error handling code in an asp.net application. I know how to trap errors in the Global.asax and I read that you can use the stack frame from the...
1
by: Scott McFadden | last post by:
C++ has some nice macros for obtaining the current function name, current source file, and current line number (__FUNCTION__, __FILE__, __LINE__). Does C# have any comparable MACROS?
0
by: Rohit111111 | last post by:
Hi, I'm trying to get the current Line Number from the Stack with stackFrame.GetFileLineNumber, how ever the method return 0. What's my error? Thanks in advance for any advice
22
by: petemul | last post by:
Hi all, I am having a problem with a PO database. I am trying to create a field that auto generates a PO line number. I have create a module (given in microsoft office help). When I try to use...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.