entlib Invalid TraceListenerData type
I've created a custom listener for Enterprise Library 5 Logging Block, which is recognized by the Configuration Editor, but throws a run-time configuration exception:
Attempt to Use the Custom Logger
static IUnityContainer _container;
static LogWriter _writer;
static IServiceLocator _locator;
public static void Inf(string message)
{
if (_container == null)
{
// Create the container
_container = new UnityContainer();
// Configurator will read Enterprise Library configuration
// and set up the container
var configurator = new UnityContainerConfigurator(_container);
// Configuration source holds the new configuration we want to use
// load this in your own code
IConfigurationSource configSource = new SystemConfigurationSource(true);
// Configure the container
EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource);
// Wrap in ServiceLocator
_locator = new UnityServiceLocator(_container);
}
if (_writer == null)
{
_writer = _locator.GetInstance<LogWriter>();
}
if (_writer != null && _container != null)
{
LogEntry log = new LogEntry();
log.Message = message;
log.Categories.Add("Information");
log.Severity = TraceEventType.Information;
_writer.Write(log);
}
}
Own TraceListener in dll
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
namespace CustomLogger
{
[ConfigurationElementType(typeof(LoggerCustomData))]
public class LoggerCustom : TraceListener //CustomTraceListener
{
readonly ILogFormatter _formatter;
public LoggerCustom()
: this(string.Empty, null)
{
}
public LoggerCustom(string name)
: this(name, null)
{
}
public LoggerCustom(string name, ILogFormatter formatter)
: base(name)
{
this._formatter = formatter;
}
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
{
if ((Filter == null) || Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
{
if (data is LogEntry)
{
if (_formatter != null)
{
WriteLine(_formatter.Format(data as LogEntry));
} else
{
base.TraceData(eventCache, source, eventType, id, data);
}
} else
{
base.TraceData(eventCache, source, eventType, id, data);
}
}
}
public override void Write(string message)
{
Trace.Write(message);
}
public override void WriteLine(string message)
{
Trace.WriteLine(message);
}
}
}
Custom TraceListenerData in dll
using System;
using System.Configuration;
using System.Diagnostics;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Common.Properties;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
namespace CustomLogger
{
[ResourceDisplayName(typeof(Resources), "CustomLoggerDataDisplayName")]
[ResourceDescription(typeof(Resources), "CustomLoggerDataDescription")]
public class LoggerCustomData : TraceListenerData
{
private const string FormatterNameProperty = "formatter";
public LoggerCustomData()
: this("unnamed", null, TraceOptions.None)
{
}
public LoggerCustomData(string name)
: this(name, null, TraceOptions.None)
{
}
public LoggerCustomData(string name, string formatterName)
: this(name, formatterName, TraceOptions.None)
{
}
protected LoggerCustomData(string name, string formatterName, TraceOptions traceOutputOptions)
: base(name, typeof(LoggerCustom), traceOutputOptions, SourceLevels.All)
{
ListenerDataType = typeof(LoggerCustomData);
Formatter = formatterName;
}
[ConfigurationProperty(FormatterNameProperty, IsRequired = false),
Reference(typeof(NameTypeConfigurationElementCollection<FormatterData, CustomFormatterData>), typeof(FormatterData)),
ResourceDisplayName(typeof(Resources), "CustomLoggerDataFormatterDisplayName"),
ResourceDescription(typeof(Resources), "CustomLoggerDataFormatterDescription")]
public string Formatter
{
get { return (string)base[FormatterNameProperty]; }
set { base[FormatterNameProperty] = value; }
}
protected override Expression<Func<TraceListener>> GetCreationExpression()
{
return () =>
new LoggerCustom(Name,
开发者_如何学Go Container.ResolvedIfNotNull<ILogFormatter>(Formatter));
}
}
}
app.config addition generated by ConfEditor
<add name="CustomLoggerDataDisplayName" type="CustomLogger.LoggerCustom, CustomLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
listenerDataType="CustomLogger.LoggerCustomData, CustomLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
traceOutputOptions="None" filter="All" formatter="Text Formatter Plain" />
Exception at .ConfigureContainer(configurator, configSource)
Invalid TraceListenerData type in configuration 'listenerDataType="CustomLogger.LoggerCustomData, CustomLogger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"'.
Why does it not work ? :( :(
p.s. this post is an evolution of entlib CustomTraceListener unresolved
In my case defaultCategory="Important" I have changed to defaultCategory="" in the Configuration first line, then worked fine.
精彩评论