开发者

using `mailSettings` section of App.Config with Enterprise Library 5 Logging Block

开发者 https://www.devze.com 2023-03-13 16:28 出处:网络
I\'m trying to set up some console applications to use the Enterprise Library 5 Logging Block. I\'ve defined the necessary elements in my app.config file and I know they work because the Event Log tr

I'm trying to set up some console applications to use the Enterprise Library 5 Logging Block.

I've defined the necessary elements in my app.config file and I know they work because the Event Log trace listener is working correctly. The issue is with my email trace listeners. Essentially I'd rather it make use of the configuration\system.net\mailSettings\smtp configuration section rather than individually configuring the multiple email listeners (also so I can have the DEBUG version "sending" emails to a local directory rather than an actual email). My configuration looks something like this:

<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;ProcessI开发者_StackOverflowd: {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>

When I change deliveryMethod="Network" to deliveryMethod=SpecifiedPickupDirectory all the emails are written out to the configured directory as expected. But when it's set to Network no emails are sent out.

I verified with a web application that we have set up nearly identically that it works with the Network configuration just fine. I'm not seeing any additional event log messages about not being able to send emails (either it really isn't being logged, or it's being swallowed...). I'm at a loss for the next steps to get this working. Has anybody else ran into this issue?


Figured out the issue. Apparently if you want it to use the SMTP host configured in configuration\system.net\mailSettings\smtp you still need to specify a blank string for the smtpServer attribute of the trace listener. So instead of this:

<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" />

You need to use this:

<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" />

Apparently leaving the attribute off doesn't mean it will pass a null string for the host to the SmtpClient like I initially thought.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号