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

Home Posts Topics Members FAQ

TraceListener issue

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:\MyAppsTrace Log.log" because it is
being used by another process."

Remind that it's ok when i click the button once. the problem arises when
clicking at the second time.

my coding is:
private void button1_Click(o bject sender, System.EventArg s e)

{

// Create a TextWriteTraceL istener to

// capture all Debug and Trace messages

// Define our tracing log file

System.IO.FileS tream traceLog = new
System.IO.FileS tream(@"C:\MyAp psTraceLog.log" ,

System.IO.FileM ode.OpenOrCreat e);

// Instantiate a new TextWriterTrace Listener and specify the output location

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

// Add Listener to the Listeners collection

Trace.Listeners .Add(traceListe ner);

// Send our tracing messages

Trace.WriteLine ("Trace Message 1");

Debug.WriteLine ("You are here.");

Trace.WriteLine ("Trace Message 2");

// Flush the listners buffer and close the output

Trace.Flush();

// if remark => file is accessed by another process

// traceLog.Close( )

traceLog.Close( );

}
Nov 15 '05 #1
3 6862
Hi,

You can try closing the stream by using the Trace's Close method, not the
FileStream's one.
You don't need an explicit call to Flush then.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Mullin Yu" <mu*******@ctil .com> wrote in message
news:uj******** ******@TK2MSFTN GP12.phx.gbl...
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:\MyAppsTrace Log.log" because it is
being used by another process."

Remind that it's ok when i click the button once. the problem arises when
clicking at the second time.

my coding is:
private void button1_Click(o bject sender, System.EventArg s e)

{

// Create a TextWriteTraceL istener to

// capture all Debug and Trace messages

// Define our tracing log file

System.IO.FileS tream traceLog = new
System.IO.FileS tream(@"C:\MyAp psTraceLog.log" ,

System.IO.FileM ode.OpenOrCreat e);

// Instantiate a new TextWriterTrace Listener and specify the output location
TextWriterTrace Listener traceListener = new
TextWriterTrace Listener(traceL og);

// Add Listener to the Listeners collection

Trace.Listeners .Add(traceListe ner);

// Send our tracing messages

Trace.WriteLine ("Trace Message 1");

Debug.WriteLine ("You are here.");

Trace.WriteLine ("Trace Message 2");

// Flush the listners buffer and close the output

Trace.Flush();

// if remark => file is accessed by another process

// traceLog.Close( )

traceLog.Close( );

}


Nov 15 '05 #2
i tried Trace.Close();

but, still having error when clicking second tme of the button.

the error is:
Cannot write to a closed TextWriter

therefore, it seems that i can't close it.

thanks!

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om> wrote
in message news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hi,

You can try closing the stream by using the Trace's Close method, not the
FileStream's one.
You don't need an explicit call to Flush then.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Mullin Yu" <mu*******@ctil .com> wrote in message
news:uj******** ******@TK2MSFTN GP12.phx.gbl...
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:\MyAppsTrace Log.log" because it is being used by another process."

Remind that it's ok when i click the button once. the problem arises when clicking at the second time.

my coding is:
private void button1_Click(o bject sender, System.EventArg s e)

{

// Create a TextWriteTraceL istener to

// capture all Debug and Trace messages

// Define our tracing log file

System.IO.FileS tream traceLog = new
System.IO.FileS tream(@"C:\MyAp psTraceLog.log" ,

System.IO.FileM ode.OpenOrCreat e);

// Instantiate a new TextWriterTrace Listener and specify the output

location

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

// Add Listener to the Listeners collection

Trace.Listeners .Add(traceListe ner);

// Send our tracing messages

Trace.WriteLine ("Trace Message 1");

Debug.WriteLine ("You are here.");

Trace.WriteLine ("Trace Message 2");

// Flush the listners buffer and close the output

Trace.Flush();

// if remark => file is accessed by another process

// traceLog.Close( )

traceLog.Close( );

}

Nov 15 '05 #3
i tried Trace.Close();

but, still having error when clicking second tme of the button.

the error is:
Cannot write to a closed TextWriter

therefore, it seems that i can't close it.

thanks!

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om> wrote
in message news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hi,

You can try closing the stream by using the Trace's Close method, not the
FileStream's one.
You don't need an explicit call to Flush then.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Mullin Yu" <mu*******@ctil .com> wrote in message
news:uj******** ******@TK2MSFTN GP12.phx.gbl...
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:\MyAppsTrace Log.log" because it is being used by another process."

Remind that it's ok when i click the button once. the problem arises when clicking at the second time.

my coding is:
private void button1_Click(o bject sender, System.EventArg s e)

{

// Create a TextWriteTraceL istener to

// capture all Debug and Trace messages

// Define our tracing log file

System.IO.FileS tream traceLog = new
System.IO.FileS tream(@"C:\MyAp psTraceLog.log" ,

System.IO.FileM ode.OpenOrCreat e);

// Instantiate a new TextWriterTrace Listener and specify the output

location

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

// Add Listener to the Listeners collection

Trace.Listeners .Add(traceListe ner);

// Send our tracing messages

Trace.WriteLine ("Trace Message 1");

Debug.WriteLine ("You are here.");

Trace.WriteLine ("Trace Message 2");

// Flush the listners buffer and close the output

Trace.Flush();

// if remark => file is accessed by another process

// traceLog.Close( )

traceLog.Close( );

}

Nov 15 '05 #4

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.
7
3303
by: George Hester | last post by:
Please take a look at this google artcle: http://groups.google.com/groups?hl=en&lr=&frame=right&th=55d6f4b50f5f9382&seekm=411f370d%241%40olaf.komtel.net#link9 The op was having trouble with access denied using resizeTo. I am having the same issue but the explanations in this article don't seem to apply here. I am not trying to resize a window with content from a different server. This issue lies here. What I do is make a popup...
2
2468
by: Anthony Cuttitta Jr. | last post by:
We have an application that outputs several different graphs from data downloaded from our AS400. The application has worked without (this) issue for several months now, but just recently, the workstation it is normally run from has been upgraded to WinXP from WinNT 5.0. The issue is that the graphs will either display the data within the recordsource (a query), OR it will display the "temp" data in the chart's datasheet (from initial...
2
2000
by: John Batdorf | last post by:
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("ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.Write); static TextWriterTraceListener traceListener = new
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.
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...
13
3521
by: SAL | last post by:
Hello, I'm trying to include a popup in the ItemTemplate of a gridview row. The ItemTemplate for the field contains a textbox and when the user clicks in the textbox I want a popup panel to show allowing the user to make a selection from the popup window. I have enabled AJAX extensions and have a working sample outside of a gridview. However, when I click in the textbox of a gridview row, all I see is a really small square instead of the...
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...
0
10250
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
7564
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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?
1
4139
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 we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.