Help in customizing Logging App Block (CustomTraceListener,LogEntry)

Topics: Building and extending application blocks, Enterprise Library Core, General discussion, Logging Application Block
Jul 23, 2009 at 11:02 AM

I'm developing an application which has a class called MessageLogEntry which inherits from LogEntry.I've added my custom properties to it.Please see the code of my MessageLogEntry below.

namespace SystemCore.LoggingService.Entities

{

[Serializable]

public class MessageLogEntry : Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry

{ public MessageLogEntry() { }

public string AccountInfo

{ get ; set ; }

public string MessageType

{ get ;set ; } } }

 Now I've added another class called DLSMessageHandler which inherits from CustomTraceListener.Below is the code

namespace SystemCore.LoggingService.MessageLogger

{

 [ConfigurationElementType(typeof(CustomTraceListenerData))]

public class DLSMessageHandler : Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.CustomTraceListener {

public DLSMessageHandler() : base()

{  }

 public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)

{  if (data is LogEntry && this.Formatter != null) { LogEntry entry = (LogEntry)data; }

if (data is MessageLogEntry && this.Formatter != null) {

MessageLogEntry msgLog = (MessageLogEntry)data;

Utilities util = new Utilities();

DLSMessage dlsMessage = new DLSMessage();

dlsMessage = TransformMessage(msgLog);

RelayDLSMessage(dlsMessage); } }

if (data is LogEntry && this.Formatter != null)

{ LogEntry entry = (LogEntry)data;

Utilities util = new Utilities();

DLSMessage dlsMessage = new DLSMessage();

dlsMessage = TransformMessage(entry);

RelayDLSMessage(dlsMessage); } } }

 public override void Write(string message)

{ }

The app.config of the above ClassLibrary project looks like this :

<configSections >

<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

</configSections>

<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">

<listeners >

<add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a "traceOutputOptions="None" type="SystemCore.LoggingService.MessageLogger.DLSMessageHandler, SystemCore.LoggingService.MessageLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=32d33aaa0dc55020 " name="DLSLogger" initializeData="" formatter="DLSFormatter" />

<add source="Enterprise Library Logging" formatter="Text Formatter" log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Formatted EventLog TraceListener" />

</listeners >

<formatters>

<add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)} " type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="DLSFormatter" />

<add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)} " type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Text Formatter" />

</formatters>

<categorySources>

<add switchValue="All" name="General">

<listeners>

<add name="Formatted EventLog TraceListener" />

</listeners>

</add>

</categorySources>

<specialSources>

<allEvents switchValue="All" name="All Events" />

<notProcessed switchValue="All" name="Unprocessed Category" />

<errors switchValue="All" name="Logging Errors &amp; Warnings">

<listeners>

<add name="Formatted EventLog TraceListener" />

</listeners>

</errors>

</specialSources>

</loggingConfiguration>

 Now I created a stub application from which i'm calling my classes.Below is the code of my stub application.

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{ }

protected void btnLogActivity_Click(object sender, EventArg e)

{ string serializedMsgData = FillActivityDetails();

MessageLogEntry log = new MessageLogEntry();

log.AccountInfo = "B7K86YYI";

log.MessageType = "Act_Xyz";

try { Logger.Write(log); }

catch (Exception ex)

{ throw ex; } }

protected void btnLogActivityA_Click(object sender, EventArgs e)

{ Logger.Write("abc"); } } }

When I say  Logger.Write(log); or Logger.Write("abc"); I'm not being able to move to overridden tracedata(......) method, I'm not even getting any error.When I'm debugging i'm simply moving in and out of Logger.Write method without any errors.What wrong could i've possibly done ? Please do help me.

Following is the code of my web.config file of my stub application

<configSections>

<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

</sectionGroup>

</configSections>

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">

<listeners>

<add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"traceOutputOptions="None" type="SystemCore.LoggingService.MessageLogger.DLSMessageHandler, SystemCore.LoggingService.MessageLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=32d33aaa0dc55020"name="DLSLogger" initializeData="" formatter="DLSFormatter" />

<add source="Enterprise Library Logging" formatter="Text Formatter"log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"name="Formatted EventLog TraceListener" />

</listeners>

<formatters>

<add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"name="DLSFormatter" />

<add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}

"type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

"name="Text Formatter" />

</formatters>

<categorySources>

<add switchValue="All" name="General">

<listeners>

<add name="Formatted EventLog TraceListener" />

</listeners>

</add>

</categorySources>

<specialSources>

<allEvents switchValue="All" name="All Events" />

<notProcessed switchValue="All" name="Unprocessed Category" />

<errors switchValue="All" name="Logging Errors &amp; Warnings">

<listeners>

<add name="Formatted Event Log TraceListener" />

</listeners>

</errors>

</specialSources>

</loggingConfiguration>

Jul 23, 2009 at 11:19 AM
Edited Jul 23, 2009 at 11:19 AM

Why do you have an app.config in your class library?  Class libraries don't own configuration files.  And why did you post your code for the Customtracelistener?  I can't see the part where you used it, you only have one Category named "General" and under it, you only have the Formatted EventLog TraceListener.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 23, 2009 at 12:01 PM

Hai Sarah,

                I'm actually developing a component to use custom LAB using MSEL as per my client's requirements which will be referenced bya an application similar to my stub application.So I added the app.conifg file in  my classlibrary project and used this dll in my stub application.As you've mentioned above I didn't configure the CustomTraceListener under the category general.Now that you pointed out, I tried it and now I'm able to step into my TraceData(....) method.

                Thanks a lot for your immediate reply.

Vinod.