Logging 将App.Config的'mailSettings'部分与Enterprise Library 5日志块一起使用

Logging 将App.Config的'mailSettings'部分与Enterprise Library 5日志块一起使用,logging,enterprise-library,app-config,Logging,Enterprise Library,App Config,我正在尝试设置一些控制台应用程序来使用EnterpriseLibrary5日志块 我已经在app.config文件中定义了必要的元素,我知道它们可以工作,因为事件日志跟踪侦听器工作正常。问题在于我的电子邮件跟踪侦听器。基本上,我希望它使用configuration\system.net\mailSettings\smtp配置部分,而不是单独配置多个电子邮件侦听器(这样我就可以让调试版本“发送”电子邮件到本地目录,而不是实际的电子邮件)。我的配置如下所示: <configuration>

我正在尝试设置一些控制台应用程序来使用EnterpriseLibrary5日志块

我已经在app.config文件中定义了必要的元素,我知道它们可以工作,因为事件日志跟踪侦听器工作正常。问题在于我的电子邮件跟踪侦听器。基本上,我希望它使用
configuration\system.net\mailSettings\smtp
配置部分,而不是单独配置多个电子邮件侦听器(这样我就可以让调试版本“发送”电子邮件到本地目录,而不是实际的电子邮件)。我的配置如下所示:

<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Email Trace Listener"
           type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           toAddress="some@address"
           fromAddress="some@address"
           subjectLineStarter="APPLICATION -- "
           subjectLineEnder=" -- Release"
           filter="Information"
           formatter="Text Formatter" />
      <add name="Informational Email"
           type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           toAddress="some@address"
           fromAddress="some@address"
           subjectLineStarter="APPLICATION -- "
           subjectLineEnder=" -- Release"
           formatter="Minimalist Text Formatter" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
           name="Text Formatter" />
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           template="Timestamp: {timestamp}{newline}&#xA;Title:{title}{newline}&#xA;Message: {message}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
           name="Minimalist Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Email Trace Listener" />
        </listeners>
      </add>
      <add switchValue="Information" name="Email">
        <listeners>
          <add name="Informational Email" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Event Log Listener" />
        </listeners>
      </notProcessed>
      <errors switchValue="Error" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Email Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="valid.local.SMTP.server"/>
        <specifiedPickupDirectory pickupDirectoryLocation="C:\Temp"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

当我将
deliveryMethod=“Network”
更改为
deliveryMethod=specifiedpickupddirectory
时,所有电子邮件都会按预期写入配置的目录。但当设置为网络时,不会发送电子邮件


我用一个web应用程序验证了我们的设置几乎相同,它可以很好地与网络配置一起工作。我没有看到任何关于无法发送电子邮件的其他事件日志消息(要么它真的没有被记录,要么它被吞没了…)。我不知道下一步该怎么做。还有其他人遇到过这个问题吗?

解决了这个问题。显然,如果您希望它使用在
configuration\system.net\mailSettings\SMTP
中配置的SMTP主机,您仍然需要为跟踪侦听器的smtpServer属性指定一个空字符串。因此,与此相反:

<add name="Email Trace Listener"
     type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
     listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
     toAddress="some@address"
     fromAddress="some@address"
     subjectLineStarter="APPLICATION -- "
     subjectLineEnder=" -- Release"
     filter="Information"
     formatter="Text Formatter" />

您需要使用以下选项:

<add name="Email Trace Listener"
     type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
     listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
     toAddress="some@address"
     fromAddress="some@address"
     subjectLineStarter="APPLICATION -- "
     subjectLineEnder=" -- Release"
     filter="Information"
     smtpServer=""
     formatter="Text Formatter" />


显然,关闭该属性并不意味着它会像我最初认为的那样将主机的空字符串传递给SmtpClient。

Hmm,这种方法似乎在企业库6中不再有效,如果
smtpServer
的值为空字符串,则创建日志编写器会引发异常。有人知道版本6中的解决方案吗?您可以通过编程方式使用,如和