473,379 Members | 1,302 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,379 software developers and data experts.

how to obtain filename behind trace listener using TraceSource

I am using a TraceSource object configured via app.config. In order to delete
the old file before starting the new trace, I need to obtain the filename
from the listener. What am I doing wrong in the following sample:

Dim trc As New TraceSource("HappySource")
‘filename returns Nothing:
Dim filename As String = _
trc.Listeners.Item("myTextListener").Attributes("i nitializeData")

app.config part:
<sources>
<source name="HappySource" switchName="HappySwitch">
<listeners>
<add name="myTextListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="TextWriterOutput.txt"
traceOutputOptions="DateTime" />
<remove name="Default"/>
</listeners>
</source>

thank you very much. herbert
Nov 2 '06 #1
1 6352
Hi Herbert,

1. You can load the config file into an XmlDocument object and traverse the
elements or use XPath to extract the value.
2. You can store the file name in application Settings and extract it at
runtime - construct the TextWriterTraceListener at runtime as well
3. I haven't tried this myself but it may work (C#, because I forget how to
properly cast in VB):

// get a strong-typed reference to the listener
TextWriterTraceListener textWriterTL =
(TextWriterTraceListener) trc.Listeners["myTextListener"];

// get the derived StreamWriter (note: if this doesn't work then look at a
// debugger watch window to determine the actual Type of the Writer
StreamWriter writer = (StreamWriter) textWriterTL.Writer;

// get the FileStream from the writer
FileStream stream = (FileStream) writer.BaseStream;

// get the name of the file that was passed into the FileStream
string fileName = stream.Name;

It's quite possible that the last line won't work if the FileStream was
constructed from a file handle, so you'll have to test it and hope for the
best.

It's also possible that you won't be able to clear the file if it has already
been opened by the TextWriterTraceListener, in which case option #2 will be
your best choice.

GL

--
Dave Sexton

"herbert" <he*****@discussions.microsoft.comwrote in message
news:93**********************************@microsof t.com...
>I am using a TraceSource object configured via app.config. In order to delete
the old file before starting the new trace, I need to obtain the filename
from the listener. What am I doing wrong in the following sample:

Dim trc As New TraceSource("HappySource")
'filename returns Nothing:
Dim filename As String = _
trc.Listeners.Item("myTextListener").Attributes("i nitializeData")

app.config part:
<sources>
<source name="HappySource" switchName="HappySwitch">
<listeners>
<add name="myTextListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="TextWriterOutput.txt"
traceOutputOptions="DateTime" />
<remove name="Default"/>
</listeners>
</source>

thank you very much. herbert

Nov 3 '06 #2

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: Jason | last post by:
Hi, I'm a little confused on using a trace listener. I'm doing this (winforms app btw not web app) TextWriterTraceListener listener = new TextWriterTraceListener("TraceOutput.txt",...
0
by: martin | last post by:
Hi, I have created an asp.net application as a project, and also another project that is just a class library. My asp.net application calls a function in the class library. I am attempting to...
1
by: Atara | last post by:
Can I view Trace output when launching a win forms application OUTSIDE of the ide? e.g. using switches in the configuration file? Must I declare a trace listener? Thanks Atara.
2
by: Jason L James | last post by:
Hi all, can anyone help me with some tracing I am trying to do in my application. I have a configuration file containing the XML as detailed below: <?xml version="1.0" encoding="utf-8" ?>...
0
by: Blake | last post by:
I am trying to make a trace listener textbox that behaves like the output window in VS, in that it shows trace messages from all trace sources. I created my own listener to write to a textbox...
1
by: Wayne | last post by:
Hi, I wonder how I should use dotNets TraceSource logging class globally within my C# application. All examples I have seen only use the TraceSource class in the Main method but I need to use it...
0
by: boldtbanan | last post by:
I've created a custom trace listener which I'd like to initialize in the web.config file of my application. I noticed that I can use the initializeData attribute to pass a string to the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.