使用App.Config的`mailSettings`部分和Enterprise Library 5 Logging Block

时间:2011-06-16 15:04:30

标签: logging enterprise-library app-config

我正在尝试设置一些控制台应用程序以使用Enterprise Library 5 Logging Block。

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

<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=SpecifiedPickupDirectory时,所有电子邮件都会按预期写入配置的目录。但是当它设置为网络时,不会发送任何电子邮件。

我验证了一个Web应用程序,我们已经设置了几乎完全相同的网络配置。我没有看到任何关于无法发送电子邮件的其他事件日志消息(无论是真的没有被记录,还是被吞没......)。我为接下来的步骤而感到茫然。还有其他人遇到过这个问题吗?

1 个答案:

答案 0 :(得分:1)

找出问题所在。显然,如果您希望它使用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。