473,791 Members | 2,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trace/Logging Question

Hi,

I would like to output the source code module name and line number in some of my Trace.Writeline () calls. The information that I need is in the Environment.Sta ckTrace string however I DO NOT want to issue a full stack trace and then parse a string every time I wish to Trace source code info!!!!!!!! For you ex-C++ now C# coders I wanna do something like this:

Trace.WriteLine (__FILE__ + "-" + __LINE__ + ": " + msg);

Is there a better way to get this info - say through reflection?? - without issuing a full stack trace every time I log information?

For extra credit does anybody know how to get the class & method name without issuing a full stack trace/parse????

--Richard

Nov 16 '05 #1
2 2102
Richard,

This will get you the current method and line number....

System.Diagnost ics.StackTrace stack = new System.Diagnost ics.StackTrace( );
System.Diagnost ics.StackFrame frame = stack.GetFrame( 0);
Console.WriteLi ne("method name: " + frame.GetMethod ().Name );
Console.WriteLi ne("line number: " + frame.GetFileLi neNumber() );

Hope this helps,

~harris

"Richard" wrote:
Hi,

I would like to output the source code module name and line number in some of my Trace.Writeline () calls. The information that I need is in the Environment.Sta ckTrace string however I DO NOT want to issue a full stack trace and then parse a string every time I wish to Trace source code info!!!!!!!! For you ex-C++ now C# coders I wanna do something like this:

Trace.WriteLine (__FILE__ + "-" + __LINE__ + ": " + msg);

Is there a better way to get this info - say through reflection?? - without issuing a full stack trace every time I log information?

For extra credit does anybody know how to get the class & method name without issuing a full stack trace/parse????

--Richard

Nov 16 '05 #2
Here is another possibility...

string theGoodStuff = System.Reflecti on.MethodBase.G etCurrentMethod ().DeclaringTyp e.FullName.ToSt ring();

good luck,

~harris

"Richard" wrote:
Hi,

I would like to output the source code module name and line number in some of my Trace.Writeline () calls. The information that I need is in the Environment.Sta ckTrace string however I DO NOT want to issue a full stack trace and then parse a string every time I wish to Trace source code info!!!!!!!! For you ex-C++ now C# coders I wanna do something like this:

Trace.WriteLine (__FILE__ + "-" + __LINE__ + ": " + msg);

Is there a better way to get this info - say through reflection?? - without issuing a full stack trace every time I log information?

For extra credit does anybody know how to get the class & method name without issuing a full stack trace/parse????

--Richard

Nov 16 '05 #3

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

Similar topics

2
2387
by: Ken | last post by:
I would like to start using EventLogTraceListener, and am running into a couple of significant limitations: 1) I have found that there is no way to write EventLog entries with different EventLogEntryTypes. It seems that EventLogTraceListener is only capable of writing entries with EventLogEventType.Informat­ion, even for Trace.Fail. Is this assessment correct? 2) EventLogTraceListener.Flush (and the idea of buffering writes) is not...
6
1990
by: latosca68 | last post by:
I need to demostrate, in a forensic job, that I can change (insert, update, delete) records in a table of an access database without trace. How can I do this ? I plan to make the queries or export to excel, make the changes and import again. Does access logs this change. Very important: Exists any kind of date/time that access manage internaly. Something like MSysObects ? If exists how can I change? Thanks for your time !!! Alf
4
2312
by: Jazz | last post by:
Hello, I have a question about trace as MSDN seems confuses me.(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDiagnosticsTraceClassTopic.asp). My answer to my question is NO, as it's decided at compile time. Basically, from what I understand is if you define the Trace tag(by default it is defined in Release build) and you build the project( in release build), those lines containing the Trace...
1
9683
by: Champika Nirosh | last post by:
Hi, What is the different between System.Diagnostic.Trace and LogforNet.. if Trace is there to track error why this open source library is there.. Please clarify.. Nirosh.
5
2937
by: who be dat? | last post by:
Hello all. I'm writing an application that is writing trace information that can be viewed in trace.axd. I would like to rename this and use a different name specific to my application. I know the name of this is set in machine.config. I was hoping it would be possible to change this in web.config. I got it to work, kind of. Good news is I can change the name in my web.config file. Bad news is that trace.axd still works meaning I can...
1
4372
by: Patrick | last post by:
When Tracing in ASP.NET, the IIS process (on IIs5.1) is locking on the Trace file, and I can't read the trace file without restarting the IIS: Even the following does NOT work (how could I fix this??): System.Diagnostics.Trace.WriteLine(System.DateTime.Now.ToLongTimeString()+ "--" + dirException.ToString()); System.Diagnostics.Trace.Flush(); ((System.Diagnostics.TraceListener) System.Diagnostics.Trace.Listeners).Close();
1
1182
by: Pete Smith | last post by:
Hi, I'm looking for a Trace Class that logs errors to the Event Log and can send error reports to me via http. Each Trace Entry should contain the call stack, exception stack and line number of the faulting method. Any suggestions?
8
2150
by: Bryan | last post by:
Does anyone have an example of an application that can connect to a running process and capture Trace.WriteLine calls like in SQL Server Profiler? I know that we can inherit from a TraceListener class to write to files, databases, etc..., but I would really like to attach to running .NET processes and get the results without any logging and instead capture the real-time results when troubleshooting.
7
9335
by: amitos | last post by:
Hi! I consider using the Logging Application Block, Enterprise Library, Jan 2006 in a major project. To date, I've found that some of its features are not very well documented. One important thing is logging to a database. I tried configuring a database trace listener in the "Enterprise Library Configuration". There I'm stuck. Stored procedures? Which ones exactly? What parameters do they take? I'm pretty confused. It also seems it...
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.