Variable for initializeData in web.config

  • Thread starter Thread starter gslim
  • Start date Start date
G

gslim

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.txt"/>

</code>
 
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.txt"/>

</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.AppSettings("TraceLog")
'Todo Get this to get the path from the config file
'fs = new
System.IO.FileStream(system.Configuration.ConfigurationSettings.AppSettings("TraceLog"),system.IO.filemode.OpenOrCreate,IO.FileAccess.ReadWrite,IO.FileShare.ReadWrite)
fs = new
System.IO.FileStream(server.MapPath(filenamestring),system.IO.filemode.OpenOrCreate,IO.FileAccess.ReadWrite,IO.FileShare.ReadWrite)

dim sw as System.IO.StreamWriter
sw = new System.IO.StreamWriter(fs,system.Text.Encoding.UTF8)
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.TraceEventType.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>
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top