【发布时间】:2010-12-25 20:53:15
【问题描述】:
是否可以在不使用整个 Enterprise Library Logging AB 的情况下使用 Enterprise Library 4.1 TraceListeners?我更喜欢简单地使用 .NET 诊断跟踪,但想设置一个侦听器来发送有关错误事件的电子邮件。我想我可以使用 Enterprise Library EmailTraceListener。但是,我最初配置它的尝试失败了。这是我希望的工作:
<system.diagnostics>
<trace autoflush="false" />
<sources>
<source name="SampleSource" switchValue="Verbose" >
<listeners>
<add name="textFileListener" />
<add name="emailListener" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="textFileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="..\trace.log" traceOutputOptions="DateTime">
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Verbose" />
</add>
<add name="emailListener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging" toAddress="to@example.com" fromAddress="from@example.com" smtpServer="mail.example.com" >
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Verbose" />
</add>
</sharedListeners>
</system.diagnostics>
但是我得到了
[ArgumentException: The parameter 'address' cannot be an empty string.
Parameter name: address]
System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding) +1098157
System.Net.Mail.MailAddress..ctor(String address) +8
Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailMessage.CreateMailMessage() +256
Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailMessage.Send() +39
Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener.Write(String message) +96
System.Diagnostics.TraceListener.WriteHeader(String source, TraceEventType eventType, Int32 id) +184
System.Diagnostics.TraceListener.TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, String format, Object[] args) +63
System.Diagnostics.TraceSource.TraceEvent(TraceEventType eventType, Int32 id, String format, Object[] args) +198
System.Diagnostics.TraceSource.TraceInformation(String message) +14
这让我相信 .NET 跟踪代码并不关心我为 emailListener 提供的“非标准”配置属性。我还尝试添加适当的 LAB configSection 声明并:
<loggingConfiguration>
<listeners>
<add toAddress="to@example.com" fromAddress="from@example.com" smtpServer="mail.example.com" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging" name="emailListener" />
</listeners>
</loggingConfiguration>
这也会导致相同的异常。我认为可以以编程方式配置 EmailTraceListener,但我更喜欢它是配置驱动的。我也明白我可以实现我自己的 TraceListener 派生。
那么,是否可以在不使用整个 Ent Lib LAB 的情况下使用 Ent Lib TraceListeners,并从配置文件中对其进行配置?
更新:检查代码后,我发现这是不可能的。尽管 .NET TraceListener 文档中有建议,但 Ent Lib TraceListeners 实际上并没有利用它们在覆盖 TraceListener.GetSupportedAttributes() 时指定的配置属性。已提交错误。
【问题讨论】:
-
轻量级和EntLib在同一个句子中? ;-)
-
哈哈,原来我问的是不可能的事。
标签: .net logging enterprise-library