我正在尝试构建一个使用 Enterprise Library 5.0 日志记录代码块的日志库,并且我已经能够创建一个 CustomTraceListener,当配置来自配置文件但我尝试通过配置源生成器配置它时我得到以下错误:
"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 的更多信息,但无济于事。
谁能帮我这个?