System.Diagnostics.Trace in ASP.NET- how not to IISRESET

G

Guest

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 this??):
System.Diagnostics.Trace.WriteLine(System.DateTime.Now.ToLongTimeString()+
"--" + dirException.ToString());
System.Diagnostics.Trace.Flush();
((System.Diagnostics.TraceListener)
System.Diagnostics.Trace.Listeners[0]).Close();


This is my Session_Start:
protected void Session_Start(Object sender, EventArgs e)
{

// Create a file for output named TestFile.txt.
if (!
System.IO.File.Exists(System.Configuration.ConfigurationSettings.AppSettings["TraceLog"]))
{
myFile =
System.IO.File.Create(System.Configuration.ConfigurationSettings.AppSettings["TraceLog"]);
}
else
{
myFile =
System.IO.File.Open(System.Configuration.ConfigurationSettings.AppSettings["TraceLog"],System.IO.FileMode.Append);
}

/* Create a new text writer using the output stream, and add it to
* the trace listeners. */
System.Diagnostics.TextWriterTraceListener myTextListener = new
System.Diagnostics.TextWriterTraceListener(myFile);
System.Diagnostics.Trace.Listeners.Add(myTextListener);


}


This is my Web.Config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="TraceLog" value="c:\\dev\\test.log" />
</appSettings>

<system.web>

<!-- DYNAMIC DEBUG COMPILATION
Set compilation debug="true" to enable ASPX debugging. Otherwise,
setting this value to
false will improve runtime performance of this application.
Set compilation debug="true" to insert debugging symbols (.pdb
information)
into the compiled page. Because this creates a larger file that
executes
more slowly, you should set this value to true only when debugging
and to
false at all other times. For more information, refer to the
documentation about
debugging ASP .NET files.
-->
<compilation
defaultLanguage="c#"
debug="true"
/>


<!-- CUSTOM ERROR MESSAGES
Set customError mode values to control the display of user-friendly
error messages to users instead of error details (including a
stack trace):

"On" Always display custom (friendly) messages
"Off" Always display detailed ASP.NET error information.
"RemoteOnly" Display custom (friendly) messages only to users not
running
on the local Web server. This setting is recommended for security
purposes, so
that you do not display application detail information to remote
clients.
-->
<customErrors mode="Off"
/>

<!-- AUTHENTICATION
This section sets the authentication policies of the application.
Possible modes are "Windows", "Forms",
"Passport" and "None"
-->
<authentication mode="Windows" />

<identity impersonate="false" />

<!-- APPLICATION-LEVEL TRACE LOGGING
Application-level tracing enables trace log output for every page
within an application.
Set trace enabled="true" to enable application trace logging. If
pageOutput="true", the
trace information will be displayed at the bottom of each page.
Otherwise, you can view the
application trace log by browsing the "trace.axd" page from your
web application
root.
-->
<trace
enabled="false"
requestLimit="10"
pageOutput="false"
traceMode="SortByTime"
localOnly="true"
/>

<!-- SESSION STATE SETTINGS
By default ASP .NET uses cookies to identify which requests belong
to a particular session.
If cookies are not available, a session can be tracked by adding a
session identifier to the URL.
To disable cookies, set sessionState cookieless="true".
-->
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;user id=sa;password="
cookieless="false"
timeout="20"
/>

<!-- GLOBALIZATION
This section sets the globalization settings of the application.
-->
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
/>

</system.web>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="myTextListener"
type="System.Diagnostics.TextWriterTraceListener,System, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089,
initializeData=c:\dev\test.log"/>
</listeners>
</trace>
</system.diagnostics>


</configuration>
 
S

Steven Cheng[MSFT]

Hi Welcome to ASPNET newsgroup.

As for the locking file problem you mentioned in ASP.NET web application,
based on my experience, it is likely caused by the FileStream's ShareMode,
when creating such file based log file, we are recommended to create the
FileStream as "ReadWrite" share. For example, when creating the FileStream,
we can use the following code:

FileStream fs = new
FileStream("path",FileMode.OpenOrCreate,FileAccess.ReadWrite,
FileShare.ReadWrite);

Also, I'm not sure why you put the code in SessionStart since session is
per user specific, you should put the application's trace handler
registering code in Application wide event. For example, here is the test
code I used which execute in Application's Start event and I can correctly
read the logfile outside when the application running:

====================
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;
}
====================

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top