Jaa


ASP.NET instrumentation (tracing) to a file

It's often helpful to instrument your code to help with troubleshooting, etc.  Instrumentation is really just a fancy word for tracing.

Here's an example of tracing to a file from ASP.NET.

<%@ Page Language="C#" CompilerOptions="/d:TRACE" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
                System.Diagnostics.Trace.WriteLine(String.Format("{0},{1}", DateTime.Now, "Hello world!"));
}
</script>

<configuration>
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="mytrace" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\temp\mytrace.csv" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

If you'd like to avoid the CompilerOptions Page directive, an alternate technique is to add the following additional web.config entries.  Note, this is equivilent to adding the directive to every page in your ASP.NET application.

<configuration>
  <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" />

    </compilers>
  </system.codedom>
</configuration>

Comments