473,799 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TraceListener in win32 application?

Ugh. I obviously don't understand the correct way to use this thing!
I have several #if(DEBUG) code blocks to write to a file ErrorLog.txt
to troubleshoot why and what exceptions are being encountered.

So in my class this is defined:

static FileStream traceLog = new FileStream("Err orLog.txt",
FileMode.OpenOr Create, FileAccess.Writ e);

static TextWriterTrace Listener traceListener = new
TextWriterTrace Listener(traceL og);

then in the same class I have this method

private static void Logger(string errorMessage)
{
// Create a text file to write messages to
// Create a TextWriterTrace Listener
//TextWriterTrace Listener traceListener = new
TextWriterTrace Listener(traceL og);
// Add the traceListener to the Trace.Listeners collection
Trace.Listeners .Add(traceListe ner);
Trace.WriteLine (errorMessage);
//Trace.Listeners .Remove(traceLi stener);
Trace.Flush();
}

At various places in my program... I make this call:

Logger("Excepti on thrown from Logout()");
Logger(ex.Messa ge.ToString());

And here's the problem! My file, the first time Logger is called no
problem errorMessage is written to the file.
the second time it's called, it creates two entries of the
errorMessage to the file. The third time... you get the point.

Here's a sample file: (each of the items only happened once, but was
written multiple times)
MAILSERVER=some configfilesetti ng
Connection Made
Connection Made
Login Success
Login Success
Login Success
MailCount = 0
MailCount = 0
MailCount = 0
MailCount = 0
Ideas? I can't figure out why this is happening... when stepping into
the debugger, I don' see this data replicated, and I'm not seeing it
written duplicate
Nov 15 '05 #1
2 2000

"John Batdorf" <jo*********@ho tmail.com> wrote in message
news:a9******** *************** ***@posting.goo gle.com...
Ugh. I obviously don't understand the correct way to use this thing!
I have several #if(DEBUG) code blocks to write to a file ErrorLog.txt
to troubleshoot why and what exceptions are being encountered.

So in my class this is defined:


You only add the TraceListener once.
If you add it a second time, then Trace.WriteLine will call it twice.

Looks like this is really a code organization problem. You need to run some
setup code before you static methods will work. You need a static
constructor:
class MyType()
{
static MyType()
{
System.IO.Strea mWriter sw =
new
System.IO.Strea mWriter("Errors .txt",false,Sys tem.Text.Encodi ng.ASCII);
sw.AutoFlush = True;
TextWriterTrace Listener tl = new TextWriterTrace Listener(sw);
Trace.Listeners .Add(tl);
}
private static void Logger(string errorMessage)
{
Trace.WriteLine (errorMessage);
}

}
David
Nov 15 '05 #2


Thanks I realized this, and actually ended up implementing a different
way, but I really appreciate the quick responses.

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

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

Similar topics

1
2209
by: Daylor | last post by:
hi. i have application with 5 threads. can i use the trace and tracelistener, so if i trace in thread1 , it will write to file1, and if i trace to thread2 if will write to file2. hope im clear, have a nice day.
0
1295
by: Schorschi | last post by:
Anyone create a custom tracelistener that redirects to a database? Or event to a memory stream rather than a file stream? If an example of a memory stream, when the Write or WriteLine methods get called I could redirect/parse the text to a database? Not sure of the best way to do this, any suggestions welcome.
3
6862
by: Mullin Yu | last post by:
i have a problem with TraceListener. when i close the FileStream, i will have the error: "Cannot access a closed file" but, if i remarks the statement => no closing the FileStream, i will have another error: "The process cannot access the file "C:\MyAppsTraceLog.log" because it is being used by another process." Remind that it's ok when i click the button once. the problem arises when
8
4772
by: Tony Johansson | last post by:
Hello! I just wonder what the difference is between a native win32-app and MFC. What I know is that you can use Win32 API in both cases. I think an MFC application is connected with a GUI which a win32 is not. You may correct me if I'm wrong on this point. //Tony
3
1517
by: Gil | last post by:
I need to create a web page in which its content should be controlled by some win32 application. this application may add or remove some gif images from the page and it should be smooth as possible as can. Can someone here direct me how to start that. Thanks in advance Gil
0
1096
by: bonk | last post by:
I have an application that initializes a lot of tracesources at runtime. The names of these tracesources are only known at runtime. How can I add one tracelistener to all tracesources using the config file (since I do not own the sourcecode for some of the assemblies that generate these tracelisteners)? If that is not possible, how can I collect all tracemessages into one file? I am using .net framework 2.0.
0
1931
by: =?Utf-8?B?Q29saXZpZXI=?= | last post by:
If anyone can help me with this I would really appreciate it: I have an assembly into which I have linked a manifest file as a Win32 resource. This is necessary since I want to use a class in this assembly with registration-free COM interop from a Win32 application. I also want to use this assembly with a .Net application that I want to deploy using ClickOnce. However when I try to install the application from the deployment location I get...
4
6527
by: =?Utf-8?B?YXJjaHVsZXRhMzc=?= | last post by:
I want to send trace messages to a textbox in my winforms application using C# 2.0. I have a custom TraceListener class (see below) that is very basic, but I'v got an error implementing it that I don't understand. To instantiate my class I have: TextboxTraceListener TextBoxTracer = new TextboxTraceListener(txtLog); Visual Studio underlines txtLog in blue and says: "Error 2 A field initializer cannot reference the nonstatic field,...
7
1906
by: =?Utf-8?B?VGhlIE1hbiBGcm9tIFNRTA==?= | last post by:
I'm having the darndest problem and I can't seem to recreate it anywhere, and I'm looking for someone with a little insight. I've created a custom EmailTraceListener which implements the TraceListener abstract class. The EmailTraceListener stores all of the messages in a StringBuilder internally and then writes the StringBuilder.ToString() to an email as a text message. Here's the problem I'm encountering. I load up several...
0
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
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
10475
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...
1
10222
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
10026
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
9068
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
6805
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
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.