Email Trace listener

Topics: Exception Handling Application Block
May 13, 2009 at 5:47 PM

Hi,

Here i have one requirement

I need to use webservice to send email in Email TraceListener.Is it Possible

 

Thanks

Gummadi

 

May 14, 2009 at 3:17 AM

Yes it is.  What are your concerns?  Let us know if you encounter any trouble with it.

 

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

May 14, 2009 at 3:25 AM
Can you please explain the procedure how to do that ?
 
and can i have multiple handlers for the single exception?
 
here i have the scenario:
 
      1.if i got an error then i need to log that error in log file,
                                   i need to mail that error,
                                   I need to log that in event logger
      2.According to sevearity i need to take action.
                          if it is error i should mail it
                          if it is warning i will just log in to log file.
 
Can you please answer all my questions?

On Wed, May 13, 2009 at 11:17 PM, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

Yes it is.  What are your concerns?  Let us know if you encounter any trouble with it.

 

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

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


May 14, 2009 at 3:44 AM

You would do that just like how you would for any type of application.  Add your configuration file for your web service.  Add the Logging Application Block and add an EmailTraceListener.  And yes, you can have multiple handlers for a single exception. 

1.  Add an EmailTraceListener and configure the required properties.  Add a FormattedEventLog TraceListener as well.  You can choose to create a single Category for them or have them separated.  Add an Exception Policy and select the type of Exception you want/  If you created a single Category for the 2 tracelisteners, let's say "General" category, add a LoggingHandler under the ExceptionType and set it's LogCategory property to "General".   If you separated them to 2 different categories, add 2 LoggingHandlers and set each LogCategory property.

2.  Set the Filter property of the EmailTraceListener to Error.  Set the Filter property of the FormattedEventLog Tracelistener to Warning.

 

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

 

May 14, 2009 at 1:02 PM

I didnot understand your explanation for Email Trace listener.

once i add EmailTrace listener it will add port and smtp server.with out using that i want to give  webservice directly to email trace listener?Can you please explain me in this scenario.If you provide any code sample that will be helpfull for me.

Thanks In Advance

 

 

May 14, 2009 at 2:17 PM
Edited May 14, 2009 at 8:06 PM

I confifured Eventlog for information and flat file for error.After the execution error is logging in both (flat file and event logger).Can you please check the code and let me know?

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="C:\Program Files\Lib\trace.log" header="----------------------------------------"
        footer="----------------------------------------" formatter=""
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="Error" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="FlatFile TraceListener" />
      <add source="Enterprise Library Logging" formatter="Text Formatter"
        log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="Information" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Formatted EventLog TraceListener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="Flat">
        <listeners>
          <add name="FlatFile TraceListener" />
        </listeners>
      </add>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
          <add name="FlatFile TraceListener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="General Policy">
        <exceptionTypes>
          <add type="System.Exception, mscorlib, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow" name="Exception">
            <exceptionHandlers>
              <add logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Logging Handler" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
</configuration>

May 15, 2009 at 8:25 AM

So.. you want to pass the port # and the server. I think you can continue adding the Email Trace Listener, then update your config at runtime. You can use the below code to update the setting of the Email trace listener at runtime. The config that you posted seems good, the only thing that's missing is the Email trace listener.

 

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

            fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config";

            Configuration entLibConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

            LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);

            EmailTraceListenerData data = loggingSettings.TraceListeners.Get("Email TraceListener") as EmailTraceListenerData;

            data.SmtpPort = port;

            data.SmtpServer = host;

            entLibConfig.Save();

 

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

Jul 21, 2009 at 3:00 PM

Hi ,

My requirement is logging should be done through Emails in a webservice.

I have added Email Trace Listener using Enterprise Library4.1(Formatter: TextFormatter, EmailTraceListener with valid details added a category(EmailCategory) which point to created Email Trace Listerner)

After adding this my loggingConfiguration in web.config  look as follows:

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add toAddress="valid @email.com" fromAddress="valid @email.com"
        subjectLineStarter="--" subjectLineEnder="--" smtpServer="valid smtp server"
        smtpPort="25" formatter="TextFormatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="DateTime" filter="Error" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="EmailTraceListener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="TextFormatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="EmailCategory">
        <listeners>
          <add name="EmailTraceListener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings" />
    </specialSources>
  </loggingConfiguration>

and I have used the following code :

                LogEntry le = new LogEntry();
                le.Categories.Add("EmailCategory");
                le.Message = "Testing our Email Logging";
                le.EventId = 1234;
                le.Title = "Email Message";
               Logger.Write(le);

 

Am not getting any exceptions while running and also am going to use that webservice am NOT RECEIVING any mails.

Pls. help to resolve this..

Thanks,

Madhu.

 

 

Jul 22, 2009 at 8:36 AM

Hi,

Is your smtpServer on a remote computer? If it is, ensure that you have the right to access it and to send emails. The config you've posted seems good, just double check that the smtpServer address that is specified is correct. Also, to make sure that there are not exception that happened during the sending of the mail, you might want to add a trace listener reference in the "Logging Errors & Warnings" in the Special Sources of the LAB. Say, you've added a FormattedEventLogTraceListener, after the logging, see the event log to check if a exception occur during the process.

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