Unable to write events to the log file in website model (ASP.NET 2.0)

I was working with one of my customers on the other day and found something very informative

Background

==========

My customer is trying to log all possible transactions that happen in their web application to a log file. They are using the system.diagnostics namespace in the web.config to accomplish this.

Steps to reproduce the behavior

========================

1. Add the following in the web.config.

  <system.diagnostics>

  <switches>

  <add name="TraceTest" value="4"/>

  </switches>

  <trace autoflush="true" indentsize="4">

  <listeners>

  <add name="WebPageTraceListener" type="System.Web.WebPageTraceListener, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

  <add name="traceLog" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:/logfile/tracetest.log" />

  </listeners>

  </trace>

  </system.diagnostics>

2. Create a folder at c:\logfile and grant the process account modify access.

3. Add the following to the Page_Load function.

protected void Page_Load(object sender, EventArgs e)

{

    System.Diagnostics.Trace.Write("Inside Page_Load Function");

}

4. Add the same lines to any section of code that requires logging.

          System.Diagnostics.Trace.Write("UserControl.Page_Load, ");

5. Build and run your application. The logfile should be created automatically at c:\logfile and the events should be written to the log file.

 

What was happening??

=================

We tried to create sample web applications using Visual Studio .NET 2003 and Visual Studio 2005. We created both website model project and WAP in Visual Studio 2005. We were able to log the events automatically if we use 1.1 or 2.0 (WAP model), but it looks like that we were not able to log any events in 2.0 (website model) and this was happening while using both HTTP and File System.

How we resolved the issue

=====================

We added a compilers tag to the web.config file as follows

<system.codedom>

  <compilers>

  <compiler language="c#;cs;csharp"

  extension=".cs"

  compilerOptions="/d:TRACE"

  type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="1" />

  <compiler language="VB"

  extension=".vb"

  compilerOptions="/d:Trace=true"

  type="Microsoft.VisualBasic.VBCodeProvider, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

  </compilers>

  </system.codedom>

When I make these changes, logging happens fine. It looks like the compiler is behaving differently in the website model. I guess it is happening because of the difference in the compilation model in 2.0 web site model.

We need to add the <compilers> tag in the web.config file because of the difference in the compilation model in 2.0 web site model. When we build the application, there's a defined set of actions that take place and the two models (website model and the WAP) have different settings. For Eg: Consider the usage of debug attribute in the web.config file. And in order to make it work, we need certain compilation switches turned on, which are not turned on by default in the website model. That's why we need to put them in there explicitly.

References:

Walkthrough: Integrating ASP.NET Tracing with System.Diagnostics Tracing
https://msdn2.microsoft.com/en-us/library/b0ectfxd.aspx