Skip to main content

Customize Trace in ASP.NET 2.0

By default, ASP.NET 2.o provides 3 trace listeners:
  1. DefaultTraceListener: Used in Visual Studio Environment
  2. TextWriterTraceListener: redirect tracing output to an instance of the TextWriter class or to any object that is a Stream class, such as a log file, network stream, or console
  3. EventLogTraceListener: redirect tracing messages to Windows event log
You can derive your customized listener from these listeners or from the abstract base class TraceListener. There are several methods you can override, but the most important ones are Write and WriteLine.

Trace configuration:

Trace can be controlled using the configuration file. The following configuration has configured an event log trace listener. All the trace information will be routed to event log named EventLogName.

<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="2">
<listeners>
<remove name="Default"/>
<add name="EventLogListener"
type="System.Diagnostics.EventLogTraceListener,System"
initializeData="MyEventLog"/>
</listeners>
</trace>
</system.diagnostics>
</configuration>

Trace switch:
Trace switch is used to filter out unwanted trace information. There are 5 levels of trace:

























Off0Outputs no messages to Trace Listeners
Error1Outputs only error messages to Trace Listeners
Warning2Outputs error and warning messages to Trace Listeners
Info3Outputs informational, warning and error messages to Trace Listeners
Verbose4Outputs all messages to Trace Listeners

Trace switch configuration:
<system.diagnostics>
<switches>
<add name="MyTraceSwitch" value="3" />
</switches>
<trace autoflush="true" indentsize="2">
<listeners>
<add name="myListener" type="CommonLib.TextWriterTraceListener, CommonLib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=92dae50c46d5d3c2" initializeData="c:\temp\CommomLog${0:yyyy-MM-dd}.txt" />
</listeners>
</trace>
</system.diagnostics>

Comments

Popular posts from this blog

Manage IIS 7 remotely using PowerShell and AppCmd

We can use  Windows PowerShell remoting features  to manage IIS 7 websites remotely.  Currently, remoting is supported on Windows Vista with Service Pack 1 or later, Windows 7, Windows Server 2008, and Windows Server 2008 Release 2.  Start Windows PowerShell as an administrator by right-clicking the Windows PowerShell shortcut and selecting Run As Administrator .  Enable PowerShell Remoting with Enable-PSRemoting -Force Starting a Remote Session using:  Enter-PSSession -ComputerName <COMPUTER> -Credential <USER> Now the PowerShell connected to the remote server. Any commands issued with work against the remote server. We can use the Appcmd.exe command line tool to manage remote server just as what we do locally. For example, to add an application pool: c:\windows\system32\inetsrv\appcmd add apppool /name:"Contoso" /managedPipelineMode:Integrated /managedRuntimeVersion:"v4.0" /enable32BitAppOnWin64:true To change application p...

Entity framework code first error: OriginalValues cannot be used for entities in the Added state

When I was using Entity framework code first, I encountered an error when I tried to create an entity into database. The entity is: [ Table (" EmployeeProfile ")]     public partial class EmployeeProfile     {         [ Key ]         [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]         public int EmployeeProfileID { get; set; }         [ ForeignKey ("Employee")]         public int EmployeeID { get; set; }         public virtual Employee Employee { get; set; }         [ ForeignKey (" Profile ")]         public int ProfileID { get; set; }         public virtual Profile Profile { get; set; }       ...

X509Certificate2: The system cannot find the file specified.

When I use the new X509Certificate2(fileName, password, X509KeyStorageFlags.DefaultKeySet) to create certificate from certificate file containing private key in my web application, I got following error message: System . Security . Cryptography . CryptographicException : The system cannot find the file specified . at System . Security . Cryptography . CryptographicException . ThrowCryptogaphicException ( Int32 hr ) at System . Security . Cryptography . X509Certificates . X509Utils . _LoadCertFromBlob ( Byte [] rawData , IntPtr password , UInt32 dwFlags , Boolean persistKeySet , SafeCertContextHandle & pCertCtx ) at System . Security . Cryptography . X509Certificates . X509Certificate . LoadCertificateFromBlob ( Byte [] rawData , Object password , X509KeyStorageFlags keyStorageFlags ) at System . Security . Cryptography . X509Certificates . X509Certificate2 .. ctor ( Byte [] rawData , String password , X509KeyStorageFlags keyStorageFlags ) In orde...