Sunday, November 27, 2016 5:12 PM
This was more like a debugging related topic for me. The overall knowledge is helpful to understand how the class CustomTraceListener can be useful to build you own tracing mechanism.
My use-case was the trace all the HTTP requests in and out of my application. One way to do that was using HttpModule but as I never intended to do any re-routing or change in processing etc. I did not find using HttpModule was needed. I was looking for a more silent way of doing things on the background.
So here it is:
1. You need the have Enterprise library for logging
https://www.nuget.org/packages/EnterpriseLibrary.Logging/
2. For writing to file better use the log4net
Develop a simple library project with the follwing code inside it
using log4net;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace TraceLib
{
//[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class DebugTraceListener : CustomTraceListener
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public DebugTraceListener()
: base()
{ }
public override void Write(string message)
{
log.Debug(message);
}
public override void WriteLine(string message)
{
log.Debug(message);
}
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
{
if (data is LogEntry && this.Formatter != null)
{
this.WriteLine(this.Formatter.Format(data as LogEntry));
}
else
{
this.WriteLine(data.ToString());
}
}
}
}
This will be your tracing code, build it and reference it your web-project like a normal "add reference"
The configuration to add within your web.config is as follows:
<configuration>
......
<system.diagnostics>
<sharedListeners>
<add name="MyTrace" type="TraceLib.DebugTraceListener,TraceLib" />
</sharedListeners>
<sources>
<source name="System.Net" >
<listeners>
<add name="MyTrace"/>
</listeners>
</source>
</sources>
<switches>
<add name="System.Net" value="Information" />
</switches>
</system.diagnostics>
</configuration>
Mostly your done, configure you log4net part in the web.config so that you can put the data to some file.
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs/rolling.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
Run the web-app and fire a web-service or some code that triggers an http call. You should be able to see all the relevant data inside the "logs/rolling.log" file inside of your virtual directory.
Hope this helps to make some more experimenting towards tracing to database etc. Cheers!