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

Variable for initializeData in web.config

I have created a trace listener and would like to give it a variable so
that when it is installed on a machine I have no control over it can
still find its logfile. Right now they are hardcoded as
"C:\Inetpub\wwwroot\website\log1.txt" is there a way to insert
something like "~/logfile.txt")

blizzardice

<code>
<sharedListeners>
<add name="FileLogListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\Inetpub\wwwroot\website\log1.tx t"/>

</code>

Aug 1 '06 #1
3 8211

gs***@blizzardice.com wrote:
I have created a trace listener and would like to give it a variable so
that when it is installed on a machine I have no control over it can
still find its logfile. Right now they are hardcoded as
"C:\Inetpub\wwwroot\website\log1.txt" is there a way to insert
something like "~/logfile.txt")

blizzardice

<code>
<sharedListeners>
<add name="FileLogListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\Inetpub\wwwroot\website\log1.tx t"/>

</code>

Another Option I could possibly do is add the tracelistener in the
application_Start. However I can't seem to add it to the sourcetrace
listener only the trace. Does someone have an idea for that?

<code>
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' ' Code that runs on application startup
' 'Set the path to the directory for the log files, Shapefiles,
and other misc files. Basically its the application startupPath
try
dim fs as System.IO.FileStream
dim filenamestring as String
filenamestring =
system.Configuration.ConfigurationSettings.AppSett ings("TraceLog")
'Todo Get this to get the path from the config file
'fs = new
System.IO.FileStream(system.Configuration.Configur ationSettings.AppSettings("TraceLog"),system.IO.fi lemode.OpenOrCreate,IO.FileAccess.ReadWrite,IO.Fil eShare.ReadWrite)
fs = new
System.IO.FileStream(server.MapPath(filenamestring ),system.IO.filemode.OpenOrCreate,IO.FileAccess.Re adWrite,IO.FileShare.ReadWrite)

dim sw as System.IO.StreamWriter
sw = new System.IO.StreamWriter(fs,system.Text.Encoding.UTF 8)
dim txtListener as system.Diagnostics.TextWriterTraceListener
txtlistener = new
System.Diagnostics.TextWriterTraceListener(sw,"txt _Listener")
system.Diagnostics.Trace.Listeners.Add(txtlistener )
system.Diagnostics.Trace.AutoFlush = True
dim ts as System.Diagnostics.TraceSource
ts = new System.Diagnostics.TraceSource("DefaultSource2")
ts.Listeners.Add(txtlistener)

catch ex as Exception
throw

my.Log.WriteException(ex,Diagnostics.TraceEventTyp e.Error,"Application_Start",-12)

finally

end try
End Sub

</Code>

<Config>
<system.diagnostics>

<sources>
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLogListener"/>
<add name="EventLogListener"/>
</listeners>
</source>
</sources>
<!--Setup logging verboseness-->
<switches>
<add name="DefaultSwitch" value="verbose"/>
</switches>
</config>

Aug 1 '06 #2
Here is some sample code from a similar post by Steven Cheng of MS that works:

protected void Application_Start(Object sender, EventArgs e)
{
FileStream fs = new
FileStream(Server.MapPath("~/logfiles/traceLog.txt"),FileMode.OpenOrCreate,F
ileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.UTF8);

System.Diagnostics.TextWriterTraceListener txtListener = new
System.Diagnostics.TextWriterTraceListener(sw, "txt_listener");

System.Diagnostics.Trace.Listeners.Add(txtListener );

System.Diagnostics.Trace.AutoFlush = true;
}

You could modify this to use your Server.MapPath arrangement if needed.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"gs***@blizzardice.com" wrote:
>
gs***@blizzardice.com wrote:
I have created a trace listener and would like to give it a variable so
that when it is installed on a machine I have no control over it can
still find its logfile. Right now they are hardcoded as
"C:\Inetpub\wwwroot\website\log1.txt" is there a way to insert
something like "~/logfile.txt")

blizzardice

<code>
<sharedListeners>
<add name="FileLogListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\Inetpub\wwwroot\website\log1.tx t"/>

</code>


Another Option I could possibly do is add the tracelistener in the
application_Start. However I can't seem to add it to the sourcetrace
listener only the trace. Does someone have an idea for that?

<code>
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' ' Code that runs on application startup
' 'Set the path to the directory for the log files, Shapefiles,
and other misc files. Basically its the application startupPath
try
dim fs as System.IO.FileStream
dim filenamestring as String
filenamestring =
system.Configuration.ConfigurationSettings.AppSett ings("TraceLog")
'Todo Get this to get the path from the config file
'fs = new
System.IO.FileStream(system.Configuration.Configur ationSettings.AppSettings("TraceLog"),system.IO.fi lemode.OpenOrCreate,IO.FileAccess.ReadWrite,IO.Fil eShare.ReadWrite)
fs = new
System.IO.FileStream(server.MapPath(filenamestring ),system.IO.filemode.OpenOrCreate,IO.FileAccess.Re adWrite,IO.FileShare.ReadWrite)

dim sw as System.IO.StreamWriter
sw = new System.IO.StreamWriter(fs,system.Text.Encoding.UTF 8)
dim txtListener as system.Diagnostics.TextWriterTraceListener
txtlistener = new
System.Diagnostics.TextWriterTraceListener(sw,"txt _Listener")
system.Diagnostics.Trace.Listeners.Add(txtlistener )
system.Diagnostics.Trace.AutoFlush = True
dim ts as System.Diagnostics.TraceSource
ts = new System.Diagnostics.TraceSource("DefaultSource2")
ts.Listeners.Add(txtlistener)

catch ex as Exception
throw

my.Log.WriteException(ex,Diagnostics.TraceEventTyp e.Error,"Application_Start",-12)

finally

end try
End Sub

</Code>

<Config>
<system.diagnostics>

<sources>
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLogListener"/>
<add name="EventLogListener"/>
</listeners>
</source>
</sources>
<!--Setup logging verboseness-->
<switches>
<add name="DefaultSwitch" value="verbose"/>
</switches>
</config>

Aug 1 '06 #3
Yes it works (make sure the logfiles directory is already created) ,
however I'd also like my trace entries to have a timestamp so I figured I
could set the properties on the trace listener to accomplish this but THAT
that doesn't see to work .

txtListener.TraceOutputOptions = TraceOptions.DateTime

but log entries don't get any time info when written. I'm certain there is
something else needs to be done but really can't imagine what it is .

Any ideas ?
thanks


"Peter Bromberg [C# MVP]" wrote:
Here is some sample code from a similar post by Steven Cheng of MS that works:

protected void Application_Start(Object sender, EventArgs e)
{
FileStream fs = new
FileStream(Server.MapPath("~/logfiles/traceLog.txt"),FileMode.OpenOrCreate,F
ileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.UTF8);

System.Diagnostics.TextWriterTraceListener txtListener = new
System.Diagnostics.TextWriterTraceListener(sw, "txt_listener");

System.Diagnostics.Trace.Listeners.Add(txtListener );

System.Diagnostics.Trace.AutoFlush = true;
}

You could modify this to use your Server.MapPath arrangement if needed.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"gs***@blizzardice.com" wrote:

gs***@blizzardice.com wrote:
I have created a trace listener and would like to give it a variable so
that when it is installed on a machine I have no control over it can
still find its logfile. Right now they are hardcoded as
"C:\Inetpub\wwwroot\website\log1.txt" is there a way to insert
something like "~/logfile.txt")
>
blizzardice
>
<code>
<sharedListeners>
<add name="FileLogListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\Inetpub\wwwroot\website\log1.tx t"/>
>
</code>

Another Option I could possibly do is add the tracelistener in the
application_Start. However I can't seem to add it to the sourcetrace
listener only the trace. Does someone have an idea for that?

<code>
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' ' Code that runs on application startup
' 'Set the path to the directory for the log files, Shapefiles,
and other misc files. Basically its the application startupPath
try
dim fs as System.IO.FileStream
dim filenamestring as String
filenamestring =
system.Configuration.ConfigurationSettings.AppSett ings("TraceLog")
'Todo Get this to get the path from the config file
'fs = new
System.IO.FileStream(system.Configuration.Configur ationSettings.AppSettings("TraceLog"),system.IO.fi lemode.OpenOrCreate,IO.FileAccess.ReadWrite,IO.Fil eShare.ReadWrite)
fs = new
System.IO.FileStream(server.MapPath(filenamestring ),system.IO.filemode.OpenOrCreate,IO.FileAccess.Re adWrite,IO.FileShare.ReadWrite)

dim sw as System.IO.StreamWriter
sw = new System.IO.StreamWriter(fs,system.Text.Encoding.UTF 8)
dim txtListener as system.Diagnostics.TextWriterTraceListener
txtlistener = new
System.Diagnostics.TextWriterTraceListener(sw,"txt _Listener")
system.Diagnostics.Trace.Listeners.Add(txtlistener )
system.Diagnostics.Trace.AutoFlush = True
dim ts as System.Diagnostics.TraceSource
ts = new System.Diagnostics.TraceSource("DefaultSource2")
ts.Listeners.Add(txtlistener)

catch ex as Exception
throw

my.Log.WriteException(ex,Diagnostics.TraceEventTyp e.Error,"Application_Start",-12)

finally

end try
End Sub

</Code>

<Config>
<system.diagnostics>

<sources>
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLogListener"/>
<add name="EventLogListener"/>
</listeners>
</source>
</sources>
<!--Setup logging verboseness-->
<switches>
<add name="DefaultSwitch" value="verbose"/>
</switches>
</config>
Aug 21 '06 #4

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

Similar topics

7
by: Billy Jacobs | last post by:
I am having a problem with my session variable being set to Null for no apparent reason. I am declaring it like the following when the user logs in. dim objUserInfo as new clsUserInfo 'Set...
11
by: Paul D.Smith | last post by:
Can Python create a variable "on-the-fly". For example I would like something like... make_variable('OSCAR', 'the grouch'); print OSCAR; ....to output... the grouch
17
by: Mike L | last post by:
This is for a Win form. Currently I have several connection strings that are identical through out my application. Is a global variable the best choice for this situation? If so, what is the...
7
by: Greg Collins [MVP] | last post by:
Hi, I couldn't find what I was looking for by searching the newsgroup, but perhaps these have already been discussed somewhere. This is a bit long with a lot of interrelated questions. What I've...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
2
by: simon | last post by:
hello, what i'm looking to do is store the path of the app on a the server for reuse in the site. my thoughts so far are... -make a key in the web.config file -retrieve the value in globals.asax...
12
by: Chris Allen | last post by:
Hello fellow pythoneers. I'm stumped on something, and I was hoping maybe someone in here would have an elegant solution to my problem. This is the first time I've played around with packages, so...
2
by: | last post by:
I set up a TextWriterTraceListener in the App.config file. I'd like to be able to tell, programmatically, which file it's using. However, I don't seem to be able to get at the initializeData...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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
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...
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...

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.