I'm trying to build a logging library that uses Enterprise Library 5.0 Logging Code Block and I've been able to create a CustomTraceListener that works when the configuration comes from the config file but when I try to configure it via the Configuration Source Builder I get the following error:
"Unable to find appropriate 0 argument constructor for CustomTraceListener"
When I try to find the error using the EL5.0 code base it is specifically having a problem building the construction string for CustomTraceListenerData.
Here is the code:
using System;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
namespace AHCALoggerLibrary
{
[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class ServiceTraceListener : CustomTraceListener
{
public ServiceTraceListener()
: base() {}
public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data)
{
base.TraceData(eventCache, source, eventType, id, data);
}
public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, params object[] data)
{
base.TraceData(eventCache, source, eventType, id, data);
}
public override void Write(string message)
{
throw new NotImplementedException();
}
public override void WriteLine(string message)
{
throw new NotImplementedException();
}
}
public class AHCALogger
{
private static AHCALogger _logger = new AHCALogger();
private LogWriter _Service = null;
public static LogWriter Service { get { return _logger._Service;} }
private AHCALogger() { }
public static void Init()
{
var builder = new ConfigurationSourceBuilder();
builder.ConfigureLogging()
.WithOptions
.DoNotRevertImpersonation()
.LogToCategoryNamed("General")
.WithOptions.SetAsDefaultCategory()
.SendTo.Custom<CustomTraceListener>("ServiceTraceListener")
.FormatWith(new FormatterBuilder()
.TextFormatterNamed("Text Formatter")
.UsingTemplate("Timestamp: {timestamp}...{newline})}"))
.SendTo.EventLog("Formatted EventLog TraceListener")
.FormatWithSharedFormatter("Text Formatter")
.ToLog("Application");
var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
_logger._Service = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
}
}
}
Then to use the logger:
AHCALogger.Init();
AHCALogger.Service.Write("hello");
I suspect that I am configuring it wrong because it does work when I use the config file settings and call:
LogWriter ahcaLogger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
ahcaLogger.Write("hello");
I've tried to find more information on how to use the ConfigurationSourceBuilder but to no avail.
Can anyone help me with this?