Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
how to write __FILE__ and __LINE__ in C#
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.


news.microsoft.com
GOOD ANSWER
Hi.

Can somebody tell me is there anything in the C# precompiler like __FILE__
and __LINE__ like in ANSI C?
So how can I print logs including the actual filename and linenumber?

Thanks,
Steve Albert

Reply to this message...
Vote that this is a GOOD answer... (3 votes from other users already)
 
 
    
Darren Shou [MSFT]
GOOD ANSWER
The special names __LINE__ and __FILE__ are simply 2 of 10 predefined ANSI
C macros recognized by the compiler. They can be used anywhere (including in
macros) just as any other defined name. You can write your own macro to do
the same for C# and then just like the C preprocessor directive #line, the
C# preprocessor also includes a #line directive which can be used to alter
the name and line number information that is output in compiler messages.

Darren

--

Please do not send email directly to this alias. This is my online account
name for newsgroup participation only. This posting is provided "AS IS" with
no warranties, and confers no rights. You assume all risk for your use. ©
2001 Microsoft Corporation. All rights reserved.

"news.microsoft.com" <Click here to reveal e-mail address> wrote in message
news:#0ZT4kOUBHA.1712@tkmsftngp03...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
István Albert
GOOD ANSWER
Thank you for reply. I see that #line can be used to alter the line number. But how can I query the current line number. For example: I'd like to write an assertion (or throw an exception or emit trace info), indicating the current file and line number. Is it possible in C#?

thanks, bye
Steve

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Anders Dalvander
GOOD ANSWER
I dunno, but look at StackTrace, it may help...

// Anders

"István Albert" <Click here to reveal e-mail address> wrote in message
news:OF$vFksXBHA.716@tkmsftngp05...
Thank you for reply. I see that #line can be used to alter the line number.
But how can I query the current line number. For example: I'd like to write
an assertion (or throw an exception or emit trace info), indicating the
current file and line number. Is it possible in C#?

thanks, bye
Steve

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Biff Gaut
GOOD ANSWER
StackFrame will also help you out. Once created, it will give source file,
line number and alot of other good stuff. Here's a sample construction-

using System.Diagnostics;

StackFrame CallStack = new StackFrame(0, true);

The 'true' indicates that source information should be included, the 0
indicates the number of frames up the call stack that context will
represent, for instance, if you put a 1 here you will get the context
information for the line that called the procedure you are now in. This
allows you to extract this information in an error handler without
explicitly passing it and without having extract it in your business code.
Here's a quasi-accurate example-

class SomeClass
{
public void DoSomething()
{
ReportError("This is an error description");
}

private void ReportError()
{
StackFrame CallStack = new StackFrame(1, true);

// This will report the line number and file name of the original
ReportError call!
MyWriteToLog("Error: " + Message + ", File: " + CallStack.GetFileName()
+ ", Line: " + CallStack.GetFileLineNumber());
}

private void MyWriteToLog(string LogMessage)
{
// Write LogMessage to some persistent log
}

}

Hope this helps,

Biff
Click here to reveal e-mail address

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
István Albert
GOOD ANSWER
Thanks. Unfortunately it doesn't work with release versions - stack info is extracted from debug information.

Anyway, it's a really cool feature of the CLR. Unfortunately, the simple constants __FILE__ and __LINE__ seem to be missing - it could be a simple preprocessor macro as in ANSI C without any runtime overhead. I hope they will whip it in the next release.

thanks again for the answers,

Steve

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Biff Gaut
GOOD ANSWER
Debug only? Of course, that makes sense. Sure screws up my day, though.
MSFT, if you're listening, count us as another strong vote for a
__LINE__-like compiler directive.

Thanks for the tip.

"István Albert" <Click here to reveal e-mail address> wrote in message
news:#jL2IZcZBHA.1868@tkmsftngp04...
> Thanks. Unfortunately it doesn't work with release versions - stack info
is extracted from debug information.
[Original message clipped]

preprocessor macro as in ANSI C without any runtime overhead. I hope they
will whip it in the next release.
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Biff Gaut
GOOD ANSWER
I saw someone point out StackTrace, which helps, but I think StackFrame will
give you more of what you want. We ran into this same issue and were
cursing the demise of __LINE__, but then stumbled on StackFrame. Here's the
code to use it-

using System.Diagnostics;

StackFrame CallStack = new StackFrame(0, true);
string SourceFile = CallStack.GetFileName(),
int SourceLine = CallStack.GetFileLineNumber(),

The 'true' argument in the constructor tells the object include source code
information. The zero tells how many frames on the stack to skip. This
comes in very handy if you are writing an error logger. Placing a 1 here
will get the line number of the call into the error handling code, like so:

class SomeClass
{
public int DoSomething()
{
ReportError("Here's an error message");
return 0;
}

private void ReportError(string Message)
{
// Get the frame one step up the call tree
StackFrame CallStack = new StackFrame(1, true);

// These will now show the file and line number of the ReportError
call in the DoSomething() method
string SourceFile = CallStack.GetFileName(),
int SourceLine = CallStack.GetFileLineNumber(),
MyWriteToFile("Error: " + Message + " - File: " + SourceFile + "
Line: " + SourceLine.ToString());
}
}

Biff Gaut
Click here to reveal e-mail address

"Darren Shou [MSFT]" <Click here to reveal e-mail address> wrote in message
news:OW$HztnXBHA.2176@tkmsftngp03...
> The special names __LINE__ and __FILE__ are simply 2 of 10 predefined
ANSI
> C macros recognized by the compiler. They can be used anywhere (including
in
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Michael Kennedy
GOOD ANSWER
Hey Biff.

Excellent info. I was wondering how you'd do this as well.

Thanks,
Michael

Michael Kennedy
United Binary Software
http://www.unitedbinary.com

"Biff Gaut" <Click here to reveal e-mail address> wrote in message
news:#Lt##1LYBHA.1748@tkmsftngp05...
> I saw someone point out StackTrace, which helps, but I think StackFrame
will
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
 
System.Diagnostics.StackFrame
System.Diagnostics.StackTrace




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734