Converting Email sending functionality from 1.1 to 4.1

Topics: Building and extending application blocks, Exception Handling Application Block, Logging Application Block
Oct 6, 2009 at 6:32 PM

Folks:

I am in the process of converting about 100 VS2003 console applications which use the older 1.1 version of the application blocks. I will convert to VS2008 using the 4.1 (October 2008) Applications Blocks (AB) The AB functionality of is quite simple: report any error that ocurrs in the code as an email, and log some events. Here is the code that is used:

Code in the exceptionhandlingconfiguration.config xml file:

<?xml version="1.0" encoding="utf-8"?>
<exceptionHandlingConfiguration>
  <xmlSerializerSection type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null">
    <enterpriseLibrary.exceptionHandlingSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/exceptionhandling">
      <exceptionPolicies>
        <exceptionPolicy name="GeneralException">
          <exceptionTypes>
            <exceptionType name="Exception" type="System.Exception, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="NotifyRethrow">
              <exceptionHandlers>
                <exceptionHandler xsi:type="LoggingExceptionHandlerData" name="Logging Handler" defaultLogCategory="General" defaultEventID="100" defaultSeverity="Error" defaultTitle="CommercialEOBFileExporter Error" formatterTypeName="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null" minimumPriority="0" />
              </exceptionHandlers>
            </exceptionType>
          </exceptionTypes>
        </exceptionPolicy>
      </exceptionPolicies>
    </enterpriseLibrary.exceptionHandlingSettings>
  </xmlSerializerSection>
</exceptionHandlingConfiguration>

Code in the loggingconfiguration.config xml file:

<?xml version="1.0" encoding="utf-8"?>
<loggingConfiguration>
  <xmlSerializerSection type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null">
    <enterpriseLibrary.loggingSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" loggingEnabled="true" tracingEnabled="false" categoryFilterMode="AllowAllExceptDenied" distributionStrategy="In Process" minimumPriority="0" name="Client Settings" xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/logging">
      <categoryFilters />
      <distributionStrategies>
        <distributionStrategy xsi:type="InProcDistributionStrategyData" name="In Process" />
      </distributionStrategies>
    </enterpriseLibrary.loggingSettings>
  </xmlSerializerSection>
</loggingConfiguration>

An finally the contents of the loggingdistributorconfiguration.config xml file

<?xml version="1.0" encoding="utf-8"?>
<loggingDistributorConfiguration>
  <xmlSerializerSection type="Microsoft.Practices.EnterpriseLibrary.Logging.Distributor.Configuration.DistributorSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null">
    <enterpriseLibrary.loggingDistributorSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" defaultCategory="General" defaultFormatter="Text Formatter" xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/loggingdistributor">
      <sinks>
        <sink xsi:type="EmailSinkData"    name="Email Sink" toAddress="my_email@my_domain.com" fromAddress="applicationerror@my_domain.com" subjectLineStarter="HIPAA 835 File Processor" subjectLineEnder="" smtpServer="MYSMPTSERVER" />
        <sink xsi:type="EventLogSinkData" name="Event Log Sink" eventLogName="HIPAA 835 File Processor" eventSourceName="HIPAA 835 File Processor" />
        <sink xsi:type="FlatFileSinkData" name="Flat File Sink" fileName="trace.log" header="----------------------------------------" footer="----------------------------------------" />
      </sinks>
      <categories>
        <category name="EventLogOnly">
          <destinations>
            <destination name="EventLogLogger" sink="Event Log Sink" format="Text Formatter" />
          </destinations>
        </category>
        <category name="General">
          <destinations>
            <destination name="Email Destination" sink="Email Sink" format="Text Formatter" />
            <destination name="Event Log Destination" sink="Event Log Sink" format="Text Formatter" />
          </destinations>
        </category>
        <category name="Trace">
          <destinations>
            <destination name="Flat File Destination" sink="Flat File Sink" format="Text Formatter" />
          </destinations>
        </category>
      </categories>
      <formatters>
        <formatter xsi:type="TextFormatterData" name="Text Formatter">
          <template><![CDATA[Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}]]></template>
        </formatter>
      </formatters>
    </enterpriseLibrary.loggingDistributorSettings>
  </xmlSerializerSection>
</loggingDistributorConfiguration>


This is how the logger code is called in the applications, using a simple function:

    
private static void LogToEventLog(string message)
        {
            try
            {
                // Set Log Entry
                LogEntry logEntry = new LogEntry();
                logEntry.Message = message;
                logEntry.Category = "EventLogOnly";
                logEntry.Severity = Severity.Information;
                logEntry.ProcessName = "HIPAA 835 File Processor";
                
                // Log Event
                Logger.Write(logEntry);
                Console.WriteLine(message);
             }
            catch(Exception ex)
            {
                throw new Exception("Error in method LogToEventLog.", ex);
            }
        }       
     
And the code where an email is sent when an error occurs:  

			catch(Exception ex)
			{
				
				LogToEventLog(string.Format("GeneralException. {0}", ex.ToString()));
				if(ExceptionPolicy.HandleException(ex, "GeneralException"))
					LogToEventLog(ex.Message);
				else 
					LogToEventLog(ex.Message);
			}			


What I want to do is to convert the email-sending functionality of the application blocks from 1.1 to 4.1, According to the documentation that I have read so far for 4.1, this has to be manually coded as there is no built-in email sink functionality.

Anyone of you who has already done this that can point me to the correct direction or has sample source code on how to do it?

 

Any help will be really,really,really appreciated.

Javier

 

Oct 7, 2009 at 6:16 AM

Hi,

Here is a FAQ item that answers your question about upgrading your config file http://entlib.codeplex.com/Wiki/View.aspx?title=Is%20it%20possible%20to%20upgrade%20a%20version%201.x%20Enterprise%20Library%20configuration%20file%20to%20any%20newer%20versions%3f&referringTitle=Home

On the code side, base on the code snippet, I think that code will not be change at all, your change will be just in the config.

 

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 
Oct 16, 2009 at 8:42 PM
AvanadeSupport wrote:

Hi,

Here is a FAQ item that answers your question about upgrading your config file http://entlib.codeplex.com/Wiki/View.aspx?title=Is%20it%20possible%20to%20upgrade%20a%20version%201.x%20Enterprise%20Library%20configuration%20file%20to%20any%20newer%20versions%3f&referringTitle=Home

On the code side, base on the code snippet, I think that code will not be change at all, your change will be just in the config.

 

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Valiant: Thanks for the reply. In your experience, where do you suggest to change the config to allow email to be sent when a exception is thrown? I have been following the tutorials and looking at the souce code but something is eluding me. 

javier

Oct 19, 2009 at 4:20 AM

Hi,

As the suggestion from the FAQ item, you would just need to recreate the same config settings in you 4.1 config. I would suggest that you recreate it using the config tool.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Oct 20, 2009 at 6:29 PM

Valiant.

I found that if I tried to modify the 1.1 config code using the 4.1 tool I was unable to see all sections so I just started from scratch and was able to (finally) have a fully functional code. As you said before,
most of the changes were done at the configuration level and not the actual c# code. Now I can write to the Event Log and send an email to our tech support people when an application error is thrown.

By the way, I used the book Definitive Guide to the Microsoft Enterprise Library by Keenan Newton, although it was written for the January 2006 version.

 

Javier

Sep 22, 2010 at 8:37 PM

Javier,

Will you be able to post the configuration file for 2.0.  after you had migrated those changes ?

Hari 

Sep 24, 2010 at 7:48 AM

Do you mean EntLib 2.0 or the .NET Framework 2.0?  I asked because I'm confused why you're asking Javier to post the configuration when he used entlib 4.1 version which uses .net 3.5.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com