Migrating to 5.0 version and LoggingInstrumentationProvider

Topics: General discussion, Logging Application Block
Oct 26, 2011 at 2:36 PM

 

 

I migrating my custom CustomTraceListener, and now I get error in this line:


instrumentationProvider = new LoggingInstrumentationProvider();

 

'Microsoft.Practices.EnterpriseLibrary.Logging.Instrumentation.LoggingInstrumentationProvider' does not contain a constructor that takes 0 arguments


any alternative implementation for entlib 5.0 ?

All code here,

 

 

 

 

 

 

 

 

[

ConfigurationElementType(typeof(CustomTraceListenerData

))]

//[DebuggerNonUserCode()]

public

class HtmlEmailTraceListener :

CustomTraceListener

{

string toAddress = String

.Empty;

string fromAddress = String

.Empty;

string subjectLineStarter = String

.Empty;

string subjectLineEnder = String

.Empty;

string smtpServer = String

.Empty;

int

smtpPort = 25;

private bool

initialized;

public void

Initialize()

{

if

(initialized)

return

;

this.toAddress = this.Attributes["toAddress"

];

this.fromAddress = this.Attributes["fromAddress"

];

this.subjectLineStarter = this.Attributes["subjectLineStarter"

];

this.subjectLineEnder = this.Attributes["subjectLineEnder"

];

this.smtpServer = this.Attributes["smtpServer"

];

string smtpPortSt = this.Attributes["smtpPort"

];

//si ponen puerto lo cambiamos

if (!string

.IsNullOrEmpty(smtpPortSt))

{

int

port;

if (!int.TryParse(smtpPortSt, out

port))

throw new ConfigurationErrorsException("smtpPort no válido: "

+ smtpPortSt);

this

.smtpPort = port;

}

initialized =

true

;

}

private void

CheckValidParams()

{

if (string.IsNullOrEmpty(this

.toAddress))

{

throw new ConfigurationErrorsException("toAddress"

);

}

if (string.IsNullOrEmpty(this

.fromAddress))

{

throw new ConfigurationErrorsException("fromAddress"

);

}

if (string.IsNullOrEmpty(this

.smtpServer))

{

throw new ConfigurationErrorsException("smtpServer"

);

}

}

public HtmlEmailTraceListener() : base

()

{

instrumentationProvider =

new LoggingInstrumentationProvider

();

initialized =

false

;

}

 

public override void Write(string

message)

{

HtmlMailMessage mailMessage = new HtmlMailMessage(toAddress, fromAddress, subjectLineStarter, subjectLineEnder, smtpServer, smtpPort, message, base

.Formatter);

mailMessage.Send();

}

public override void WriteLine(string

message)

{

Write(message);

}

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

data)

{

if (data is LogEntry

)

{

Initialize();

CheckValidParams();

HtmlMailMessage mailMessage = new HtmlMailMessage(toAddress, fromAddress, subjectLineStarter, subjectLineEnder, smtpServer, smtpPort, data as LogEntry, base

.Formatter);

mailMessage.Send();

InstrumentationProvider.FireTraceListenerEntryWrittenEvent();

}

else if (data is string

)

{

Write(data);

}

else

{

base

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

}

}

protected override string

[] GetSupportedAttributes()

{

return new string[7] { "formatter", "toAddress", "fromAddress", "subjectLineStarter", "subjectLineEnder", "smtpServer", "smtpPort"

};

}

private ILogFormatter

formatter;

private LoggingInstrumentationProvider

instrumentationProvider;

protected LoggingInstrumentationProvider

InstrumentationProvider

{

get { return

instrumentationProvider; }

}

public override bool

IsThreadSafe

{

get

{

return true

;

}

}

public ILogFormatter

Formatter

{

get

{

return this

.formatter;

}

set

{

this.formatter = value

;

}

}

public object

GetInstrumentationEventProvider()

{

return

instrumentationProvider;

}

}

Oct 27, 2011 at 12:29 AM

As you have found out the constructor has changed since 4.1.  The new constructor looks like:

/// <summary>
/// Initializes a new instance of the <see cref="LoggingInstrumentationProvider"/> class.
/// </summary>
/// <param name="performanceCountersEnabled"><code>true</code> if performance counters should be updated.</param>
/// <param name="eventLoggingEnabled"><code>true</code> if event log entries should be written.</param>
/// <param name="applicationInstanceName">The application instance name.</param>
public LoggingInstrumentationProvider(bool performanceCountersEnabled,
                                      bool eventLoggingEnabled,
                                      string applicationInstanceName)

Luckily you don't actually have to worry about that!  Instead of creating your own instrumentation provider it would be better to retrieve the instrumentation provider from the container:

ILoggingInstrumentationProvider instrumentationProvider = 
    EnterpriseLibraryContainer.Current.GetInstance<ILoggingInstrumentationProvider>();

This way you don't have to worry about constructing the object.  Plus you get the added benefit that it is already hooked into the Enterprise Library configuration
so it will reflect all of the application settings around instrumentation (e.g. enable/disable).

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com