Create Custom Trace Listener

Jul 31, 2007 at 9:28 AM
Hello,

I have made a custom listener, but when i want to add it into project, I have this error : There were no types found in the assembly 'SMSListener' that implement or inherit from the base type 'Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.CustomTraceListener'

I use the Enterprise Library Configuration Tool.

This is my Code :
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.ObjectBuilder;

namespace SMSListener
{
ConfigurationElementType(typeof(SMSTraceListenerData))
public class SMSTraceListener : FormattedTraceListenerBase
{
// Fields
private string mt;

// Methods
public SMSTraceListener()
{
this.mt = string.Empty;
}

public SMSTraceListener(ILogFormatter formatter)
: base(formatter)
{
this.mt = string.Empty;
}


public SMSTraceListener(string mt)
{
this.mt = mt;
}

public SMSTraceListener(string mt, ILogFormatter formatter)
: base(formatter)
{
this.mt = mt;
}

protected override string[] GetSupportedAttributes()
{
return new string[] { "formatter", "mt"};
}

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

base.TraceData(eventCache, source, eventType, id, data);
}

public override void Write(string message)
{
SMSService.Service smsServ = new SMSListener.SMSService.Service();
smsServ.PutSMS("", "SI LOG", this.mt, message, "SMSCTEST");
}

public override void WriteLine(string message)
{
this.Write(message);
}
}

Assembler(typeof(SMSTraceListenerAssembler))
public class SMSTraceListenerData : TraceListenerData
{
// Fields
private const string formatterNameProperty = "formatter";
private const string mtProperty = "mt";

// Methods
public SMSTraceListenerData()
{
}

public SMSTraceListenerData(string mt, string formatterName)
: this("unnamed", mt, formatterName)
{
}

public SMSTraceListenerData(string name, string mt, string formatterName)
: this(name, mt, formatterName, TraceOptions.None)
{
}

public SMSTraceListenerData(string name, string mt, string formatterName, TraceOptions traceOutputOptions)
: base(name, typeof(EmailTraceListener), traceOutputOptions)
{
this.MT = mt;
this.Formatter = formatterName;
}

// Properties
ConfigurationProperty("formatter", IsRequired = false)
public string Formatter
{
get
{
return (string)base"formatter";
}
set
{
base"formatter" = value;
}
}

ConfigurationProperty("mt", IsRequired = true)
public string MT
{
get
{
return (string)base"mt";
}
set
{
base"mt" = value;
}
}
}

public class SMSTraceListenerAssembler : TraceListenerAsssembler
{
// Methods
public override TraceListener Assemble(IBuilderContext context, TraceListenerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
{
SMSTraceListenerData data = (SMSTraceListenerData)objectConfiguration;
return new SMSTraceListener(data.MT, base.GetFormatter(context, data.Formatter, configurationSource, reflectionCache));
}

}
}

Can you give me help please ?

Thank You
Jul 31, 2007 at 2:53 PM
Hi,

What do you mean by "add it to the project"? Is it when updating the configuration file?

Fernando
Aug 1, 2007 at 7:36 AM


fsimonazzi wrote:
Hi,

What do you mean by "add it to the project"? Is it when updating the configuration file?

Fernando


Hi Fernando,

When I open the app.config with Enterprise library tool.
After in Logging "Application Block "/ "Trace Listener", I add "New" > "Custom Trace Listener".
In properties tab, I click on "Type" parameter. A windows is shown an I click on "Load an Assembly".

I choose my assembly and after, on validate I have the error...
Aug 1, 2007 at 1:32 PM
Hi,

The problem is that your trace listener is not really a "custom" in the sense that it defines its own configuration. Proper custom trace listeners do not define configuration, so they rely on a limited configuration support that consists of untyped name/value pairs.

You will need to add design time support for your class, so your trace listener will show up as a new item on the "New trace listener" menu. If you used the app block factory to create your listener, then it should be really simple to add such support; otherwise you can use the Database Trace Listener design time support code for an example on how to hook it up.

Regards,
Fernando
Oct 12, 2007 at 3:34 AM
Edited Oct 12, 2007 at 3:35 AM
I am having the same issue is there an example somewhere of what I need to do to create a custom Listener.

Thanks,

Joe
Oct 12, 2007 at 2:40 PM
Hi,


jjoravec wrote:
I am having the same issue is there an example somewhere of what I need to do to create a custom Listener.


This thread discusses some details http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=15736. You can also look at the MockCustomTraceListeners used in the unit tests.

Hope this helps,
Fernando