473,410 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

Trace.Listeners.Add

According to http://www.15seconds.com/issue/020910.htm I am doing this
in the c'tor of a 'logfile' class:

objStream = new System.IO.FileStream(logFilename,
System.IO.FileMode.OpenOrCreate);
objTraceListener = new TextWriterTraceListener(objStream);
Trace.Listeners.Add(objTraceListener);

So, now all Debug.Write and Trace.Write get logged to the file. In
the d'tor I am doing:

Trace.Flush();
Trace.Listeners.Remove(objTraceListener);
objStream.Close();

(I added the Trace.Listeners.Remove part myself, since the tutorial
didn't mention it. Without Trace.Listeners.Remove, any calls to
Debug.Write will fail in the tutorial after the the stream is closed.)

But, in the d'tor, if I call Debug.Write or Trace.Write *before* the
above, it'll complain that I am writing to a closed file. It makes no
sense. If I run the debugger and step through it, it doesn't
complain, just as it shouldn't.

When / how do I close the file?

Zytan

Mar 5 '07 #1
6 3935
Trace.Flush();
Trace.Listeners.Remove(objTraceListener);
objStream.Close();
When / how do I close the file?

Zytan
It is a bit hard to answer without seeing your source, but don't you need to
close objTraceListener instead of objStream? The next small example seems to
work properly:

class LogFile : IDisposable
{
private FileStream m_objStream;
private TextWriterTraceListener m_objTraceListener;
public LogFile(string logFilename)
{
m_objStream = new FileStream(logFilename, FileMode.Append);
m_objTraceListener = new TextWriterTraceListener(m_objStream) ;
Trace.Listeners.Add(m_objTraceListener) ;
}
public void Dispose()
{
m_objTraceListener.Flush();
Trace.Listeners.Remove(m_objTraceListener);
m_objTraceListener.Close();
}
}

FYI: tracing can be managed in the Web.config/App.config too. You don't need
to write any code then. See
http://msdn2.microsoft.com/en-us/library/1txedc80.aspx for details.

Kind regards,
Anne
Mar 5 '07 #2
It is a bit hard to answer without seeing your source, but don't you need to
close objTraceListener instead of objStream? The next small example seems to
work properly:
Well, my source is basically the source you have below. I really
don't know what I have to close or when. I see you are using
IDisposable. This is better than making a destructor (Finalize)?

Since my log needs formatting, I think I am going to implement my
own. I already have richtextbox functions, so I'll just make a
RichTextBox, and write to that, and output an .rtf file from it at the
end.

When should this code, that gets the rtf code from the RichTextBox and
writes it to a file, run? Should I make the class IDisposable, as you
did above? Or should I run it in a destructor? I guess this is a
different topic altogether. I just don't get what the difference is
between Dispose and Finalize.

Thanks, Anne

Zytan

Mar 5 '07 #3
When should this code, that gets the rtf code from the RichTextBox and
writes it to a file, run? Should I make the class IDisposable, as you
did above? Or should I run it in a destructor? I guess this is a
different topic altogether. I just don't get what the difference is
between Dispose and Finalize.
http://www.ondotnet.com/pub/a/dotnet...arp_traps.html
says under the 'Trap #2: Finalize versus Dispose' part:

"If you do handle precious unmanaged resources (such as file handles)
that you want to close and dispose of as quickly as possible, you
ought to implement the IDisposable interface. The IDisposable
interface requires its implementers to define one method, named
Dispose(), to perform whatever cleanup you consider to be crucial. The
availability of Dispose() is a way for your clients to say, "Don't
wait for the destructor to be called; do it right now.""

So, I assume by dealing with files, I am using unmanaged resources.

But, since NOW, I will not be writing to a file, but instead to a
RichTextBox, it doesn't really matter. I'll only write to the file
when the program is done, so I guess my question is different. I want
to know if I should make the open-write-close file code in Dispose?

I don't like my solution completely, since if the program crashed, and
then I get no logging data (it's still all in the RichTextBox), so
it's not perfect. If there was a RTF format that just allowed
appending without reconfiguring the entire file each time, this would
be a lot easier. I could implement this myself (writing actual RTF
code manually), but then i'm getting my hands dirty when the NET
framework is supposed to do this for me. Maybe that's the best way
for a simple log file:

http://www.gamedev.net/reference/pro...atures/rtflog/

Zytan

Mar 5 '07 #4
I'll only write to the file
when the program is done, so I guess my question is different. I want
to know if I should make the open-write-close file code in Dispose?
I don't see any reason I should. IDisposable is so I can call Dispose
explicitly to release a resource, right? And I don't have to do
this. I just want the GC to run the destructor (Finalize) whenever it
wants to. At this time, the RichTextBox's RTF codes will be witten to
a file via open-write-close. That's proper, right?

Zytan

P.S. Do C#'ers use 'destructors' or 'finalizers'? What's the proper
term? If we use constructor, it seems that destructor should be ok.

Mar 5 '07 #5
I just don't get what the difference is
between Dispose and Finalize.
The Finalizer (C++'s destructor) is private and it can only called by the
GC: by this you don't know when the Finalizer is called.
The Dispose() is public and it can only be called by your code (not by the
GC): by this you know when Dispose() is called.
So both have their advantages and disadvantages. Combining both the
Finalizer and Dispose using the the "dispose pattern" is probably the best
way if you are dealing with unmanged resources (see
http://www.codeproject.com/useritems/idisposable.asp for some examples).
One big advantage of contracting your class to IDisposable is that you can
use the using statement which "implements" a hidden try..finally block and
calls Dispose() for you. For example I can use it with the class from my
previous message:

static void Main()
{
using (LogFile log = new LogFile(@"C:\test.log"))
{
Trace.WriteLine("Traceline 1");
Debug.WriteLine("Traceline 2");
}
}

this is the same as:

static void Main()
{
LogFile log;
try
{
log = new LogFile(@"C:\test.log"))
Trace.WriteLine("Traceline 1");
Debug.WriteLine("Traceline 2");
}
finally
{
if (null != log)
log.Dispose();
}
}

It is obvious that the 'using' way is more readable and more robust (i.e.
you cannot accidently forget something). There are a lot of example about
the using statement
(http://codebetter.com/blogs/john.pap.../01/60984.aspx for
example).

Regards,
Anne
Mar 5 '07 #6
The Finalizer (C++'s destructor) is private and it can only called by the
GC: by this you don't know when the Finalizer is called.
The Dispose() is public and it can only be called by your code (not by the
GC): by this you know when Dispose() is called.
So, that's the main thing. Dispose is when you want to 'dispose' or
sort of destruct (but not really, it's really just a 'shutdown' phase,
but the GC ultimately destructs it) your object when you decide, not
when the GC decides.
So both have their advantages and disadvantages. Combining both the
Finalizer and Dispose using the the "dispose pattern" is probably the best
way if you are dealing with unmanged resources (seehttp://www.codeproject.com/useritems/idisposable.aspfor some examples).
Ok, I'll take a look.
One big advantage of contracting your class to IDisposable is that you can
use the using statement which "implements" a hidden try..finally block and
calls Dispose() for you. For example I can use it with the class from my
previous message:
Right, so this way, not only can I call Dispose on my own terms, but
using 'using' I can actually force it to be called without even typing
it in. I now get why the using-statment is the way it is. It's just
a quicker (less error prone) way of forcing your object to shutdown
(close files, etc) immediately after the resource is finished being
used.

Thanks, Anne

Zytan

Mar 6 '07 #7

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

Similar topics

8
by: Geopsaros | last post by:
Hi! I have created a custom trace Listener class, called "DBTraceListener" it works fine when i add it manually in code : (eg. Trace.listeners.add(new DBTraceListener("myDBListener",...
2
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...
2
by: Wilfried Hoermann | last post by:
Presumably a trivial question... I want to write trace information from a web service to a log file using the Trace Class. Is this possible without closing and opening the Trace in every single...
2
by: S. Han | last post by:
I use Trace.Assert() through out my app and it's such a great way to debug a program. My question is is there any way to redirect the assertion pop-up to the command line output in my console...
3
by: Jens Alenius | last post by:
I've been looking at the Trace and TraceSwitch Classes i C# and I thrying to make it act more like java log4j. What I want is to add two different tracelisteners (like EventLogTraceListener and...
5
by: guy | last post by:
Where do I get trace output? Trace.Write("Hello World");
1
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...
3
by: Absolon | last post by:
Hi, Can anyone tell the advantages/dis-advantages of adding a listener to the listeners collection? In MSDN it says that listeners not in the collection do not recieve all trace output. What...
0
by: msnews.microsoft.com | last post by:
Hi, I am tring to use Trace Listener in asp.net 1.1 application as mentoned here. But i am getting the following error, Configuration Error Description: An error occurred during the processing...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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
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...

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.