我正在尝试构建一个使用Enterprise Library 5.0 Logging Code Block的日志库,我已经能够创建一个CustomTraceListener,当配置来自配置文件但我尝试通过配置进行配置时Source Builder我收到以下错误:
"Unable to find appropriate 0 argument constructor for CustomTraceListener"
当我尝试使用EL5.0代码库查找错误时,特别是在为CustomTraceListenerData构建构造字符串时遇到问题。
以下是代码:
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>();
}
}
}
然后使用记录器:
AHCALogger.Init();
AHCALogger.Service.Write("hello");
我怀疑我配置错误,因为当我使用配置文件设置并调用时它确实有效:
LogWriter ahcaLogger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
ahcaLogger.Write("hello");
我试图找到有关如何使用ConfigurationSourceBuilder的更多信息,但无济于事。
任何人都可以帮我吗?
答案 0 :(得分:1)
您需要替换
.SendTo.Custom<CustomTraceListener>("ServiceTraceListener")
带
.SendTo.Custom<ServiceTraceListener>("ServiceTraceListener")
错误消息告诉您基本上无法实例化抽象类。你需要传递具体的课程。你不需要[ConfigurationElementType(typeof(CustomTraceListenerData))]因为它没有效果。
要将自定义配置数据传递给跟踪侦听器,您需要知道数据存储在TraceListener基类的Attributes集合中。这意味着您无法在第一次日志调用时初始化ctor中的侦听器。
我已经清理了一些静态记录器,以使其更容易。
我在调试这个烦人的 SynchronizationLockException
时注意到了“从非同步的代码块调用了对象同步方法。”
在Entlib5 yuck 中似乎是bug。除了自己重新编译并修复它之外,我还没有找到解决方法。
以下是包含自定义数据的工作记录器的代码。
using System;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using System.Linq;
using System.Collections.Specialized;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
class Program
{
static void Main(string[] args)
{
AHCALogger.Service.Write(new LogEntry() { Message = "Hi" });
}
}
public static class AHCALogger
{
static Lazy<LogWriter> _Service = new Lazy<LogWriter>(Init);
public static LogWriter Service
{
get { return _Service.Value; }
}
static LogWriter Init()
{
var builder = new ConfigurationSourceBuilder();
var serviceConfig = new NameValueCollection();
serviceConfig.Add("Key", "data");
builder.ConfigureLogging()
.LogToCategoryNamed("General")
.WithOptions.SetAsDefaultCategory()
.SendTo.Custom<ServiceTraceListener>("ServiceTraceListener", serviceConfig);
var configSource = new DictionaryConfigurationSource();
configSource.Add(LoggingSettings.SectionName, builder.Get(LoggingSettings.SectionName));
var cont = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
return cont.GetInstance<LogWriter>();
}
}
public class ServiceTraceListener : CustomTraceListener
{
public ServiceTraceListener() { }
public override void Write(string message)
{
Console.WriteLine("Custom trace Listener Data {0}",
String.Join(Environment.NewLine, base.Attributes.Keys.OfType<string>().Select(key => "[" + key + "]: " + base.Attributes[key])));
Console.WriteLine("message: {0}", message);
}
public override void WriteLine(string message)
{
Write(message + Environment.NewLine);
}
}