Vaibhav Gaikwad
Microsoft .NET developer
Bla Code! Bla Code!

HTTP logging using CustomTraceListener from Enterprise library

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!



Feedback

# Pc games And SoftwaRES

Awsome. Let me check this out and see if it works for me. Thanks for the info. 11/30/2016 9:25 PM | Malik Boss

# re: HTTP logging using CustomTraceListener from Enterprise library

eally very informational post also very helpful blog pc games and softwares blogspot download 11/30/2016 9:32 PM | I am Boss

# re: HTTP logging using CustomTraceListener from Enterprise library

Great post visit 12/9/2016 4:28 PM | Mike

# re: HTTP logging using CustomTraceListener from Enterprise library

What the editorial was not detailed about was the realization of the venture library in your test development. NeedPaperHelp.com 12/9/2016 4:32 PM | Daniel Jorden

# re: HTTP logging using CustomTraceListener from Enterprise library

This helps organize Enterprise Library, because all the XML for these logging frameworks can be relatively inflexible to study. Brilliantwritings.com 12/9/2016 4:34 PM | Daniel Jorden

# re: HTTP logging using CustomTraceListener from Enterprise library

This is the perfect post to scrutinize. I'm uncommonly enchanted to find this site. Much gratitude to you for sharing. Ensuing to examining this post I understand the structure of article creating.Fair Play Leather Jacket. 1/16/2017 5:48 PM | Susan Fernando

# re: HTTP logging using CustomTraceListener from Enterprise library

Thanx for sharing such a valuable code. than againdatabase sql books
2/10/2017 11:29 AM | Mukesh Nanda

# re: HTTP logging using CustomTraceListener from Enterprise library

Studies have also found a link between high levels of https://spanish982.wordpress.com/ 2/10/2017 3:30 PM | deepak

# graybar-syndications case study analysis

I appreciate your efforts in preparing this post. I really like your blog articles.
graybar-syndications case study analysis
3/8/2017 12:02 PM | graybar-syndications case study

# http://www.bingn.org/best-wireless-routers-latest/

the best wireless router for gaming.The RT N66U increases transmission speed by 50%, to up to 900 Mbps for both bands together. 4/1/2017 2:49 PM | best wireless routers under 100

# Andrew Lincoln jacket

The content of this article is not wordy because writer explains his complete idea in few points. 6/8/2017 11:06 AM | joyce santiago

# http://www.spazeapparel.com/devon-jones-d475-mens-sweater.html

What the editorial was not detailed about was the realization of the venture library in your test development. 9/8/2017 5:15 PM | Devon & Jones D475 Sweater

# re: HTTP logging using CustomTraceListener from Enterprise library

https://theessaypro.com/euthanasia-essays/ we love this content so much. 9/24/2017 3:16 AM | https://theessaypro.com/

# re: HTTP logging using CustomTraceListener from Enterprise library

Follow this link of the most entertaining card game solitairetimes.com and play for free and online solitaire times game.You love to play this most popular card game like me.Many many thanks for the awesome multiplayer card game. 9/26/2017 1:34 AM | Ashwin

# re: HTTP logging using CustomTraceListener from Enterprise library

very useful post. thanks for sharing.
Click here
1/13/2018 12:34 PM | Samuel David

# re: HTTP logging using CustomTraceListener from Enterprise library

thanks for sharing. Malayalam recipes
2/7/2018 12:24 PM | Harry

# American Flag Leather Jacket

This is a funky style leather jacket. It has a US flag color all over it which gives the jacket a really cool look. It is made up of 100% real leather. I has a slim fit look with button closure. It is best for college boys and bikers. All sizes are available. Grab one for you right now!
American Flag Leather Jacket
3/22/2018 12:50 PM | MauveTree

Post a comment