Exceptional Handling isn't sending email and logging in SharePoint 2010 application under w3wp process

Topics: Exception Handling Application Block
Oct 17, 2012 at 10:24 PM

Hi,

I have a SharePoint 2010 web application which is using Microsoft enterprise library 5.0 logging, data, and exceptional handling block.

Data and Logging Block are working fine but Exception handling block isn't working for w3wp process. It's neither logging in the file system nor sending an email. The w3wp process account has full access to the folder. I used FILE API to confirm the write permission on the folder, FILE API is working fine and Enterprise Logging is also working fine but when it comes for exception handling it's not.

I am using ExceptionPolicy.HandleException (exceptionToHandle, policyName) API for handling exception.

e.g: ExceptionPolicy.HandleException (ex, "SiteExceptionPolicy") // ex is the exception that occurred.

This same configuration and code is used for some console application and SharePoint timer job on the same server, they are working fine. Whenever an exception occurs we get the email and see the log entry in the logs for console and time jobs. 

Below are the configuration. For security reasons, I have not shared the actual server names or email addresses.

Highly appreciate your immediate response.

Thanks,

Oct 17, 2012 at 10:29 PM
Edited Oct 17, 2012 at 10:30 PM

Config Sections:

<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" /> <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" /> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" /> 
Oct 17, 2012 at 10:32 PM

Logging Block:

<loggingConfiguration name="NNGLogging" tracingEnabled="true" defaultCategory="SiteErrorCategory">

 

    <listeners>

 

      <add name="Error_EmailListener" 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="abcd@gmail.com " fromAddress="abcd@gmail.com" subjectLineStarter="Site 443 " subjectLineEnder="" smtpServer="mailhost.gmail.com" smtpPort="25" formatter="ErrorFormatter" authenticationMode="None" userName="" password="" traceOutputOptions="None" filter="All" />

 

      <add name="SiteError_RollingListener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" fileName="D:\Logs\443\SiteRolling.log" footer="****************************************" formatter="ErrorFormatter" header="****************************************" rollFileExistsBehavior="Increment" rollInterval="Day" rollSizeKB="1024" maxArchivedFiles="8" traceOutputOptions="None" filter="All" />

 

      <add name="Trace_RollingListener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" fileName="D:\Logs\443\TraceRolling.log" footer="++++++++++++++++++++++++++++++++++++++++" formatter="TraceFormatter" header="++++++++++++++++++++++++++++++++++++++++" rollFileExistsBehavior="Increment" rollInterval="Day" rollSizeKB="1024" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" filter="All" />

 

      <add name="TimerJobError_RollingListener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" fileName="D:\Logs\TimerJobs\TimerJobError_rolling.log" footer="********************" formatter="ErrorFormatter" header="********************" rollFileExistsBehavior="Increment" rollInterval="Day" rollSizeKB="1024" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />

 

    </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(local)}{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="ErrorFormatter" />

      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Timestamp: {timestamp(local)}, Machine: {localMachine}, Process Name: {localProcessName}&#xA;Message: {message}{newline}" name="TraceFormatter" />

    </formatters>

 

    <logFilters>

      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="DisableTracing">

        <categoryFilters>

          <add name="Info" />

        </categoryFilters>

      </add>

    </logFilters>

 

    <categorySources>

      <add switchValue="All" name="SiteErrorCategory">

        <listeners>

          <add name="Error_EmailListener" />

          <add name="SiteError_RollingListener" />

        </listeners>

      </add>

      <add switchValue="Warning" name="Warning">

        <listeners>

          <add name="Trace_RollingListener" />

        </listeners>

      </add>

      <add switchValue="Information" name="Info">

        <listeners>

          <add name="Trace_RollingListener" />

        </listeners>

      </add>

      <add switchValue="Verbose" name="Trace">

        <listeners>

          <add name="Trace_RollingListener" />

        </listeners>

      </add>

      <add switchValue="All" name="TimerJobErrorCategory">

        <listeners>

          <add name="Error_EmailListener" />

          <add name="TimerJobError_RollingListener" />

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


Oct 17, 2012 at 10:33 PM

Exception Handling Block:

<exceptionHandling>

    <exceptionPolicies>

      <add name="SiteExceptionPolicy">

        <exceptionTypes>

          <add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="None">

            <exceptionHandlers>

              <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" logCategory="SiteErrorCategory" eventId="100" severity="Error" title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="0" />

            </exceptionHandlers>

          </add>

        </exceptionTypes>

      </add>

 

      <add name="TimerJobExceptionPolicy">

        <exceptionTypes>

          <add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="None">

            <exceptionHandlers>

              <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" logCategory="TimerJobErrorCategory" eventId="100" severity="Error" title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="0" />

            </exceptionHandlers>

          </add>

        </exceptionTypes>

      </add>

    </exceptionPolicies>

  </exceptionHandling>


Oct 18, 2012 at 2:34 AM

You are probably encountering an error sending an email (assuming that the exception handler is being invoked).  You should configure the errors special source to report on these errors.  E.g. modify the configuration to this:

    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="SiteError_RollingListener" />
        </listeners>
      </errors>
    </specialSources>

Also, I'm not sure if the posted configuration is just sample data or if it is the actual configuration you are using to send to Gmail.  Gmail settings that work for me are:

      <add name="Error_EmailListener" 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="abcd@gmail.com " fromAddress="abcd@gmail.com" subjectLineStarter="Site 443 "
        subjectLineEnder="" smtpServer="smtp.gmail.com" smtpPort="587"
        formatter="ErrorFormatter" authenticationMode="UserNameAndPassword" useSSL="true" userName="abcd@gmail.com" 
        password="thepassword" traceOutputOptions="None" filter="All" />

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Oct 18, 2012 at 3:48 AM

Thanks Randy. Tomorrow morning, I will try adding "SiteError_RollingListener" listener for error in SpecialSources and share my findings. SMTP settings and email addresses are just the sample not actual ones.

Oct 18, 2012 at 5:17 PM

I got some errors in SiteRolling.log after making your defined changes but it stopped again and now it's not working. Below is the log errors. Please take a look and advise.

Oct 18, 2012 at 5:33 PM

Note: I have put the EXPECTED/UNEXPECTED ERROR START AND END TAGS in the below error details just to make it more understanding. EXPECTED is the error that i intentionally cause something to generate but unexpected is unknown why it's happening. Secondly, for privacy reasons I have replaced the important information with abcd.

My smtp settings are fine. I am getting error email and error logging in other processes like TimerJobs and console application for same setting but different folders path from the site web.config. Please take a look and advise !

<UNEXPECTED ERROR START>

Timestamp: 10/18/2012 10:21:52 AM

Message: Tracing to LogSource 'SiteErrorCategory' failed. Processing for other sources will continue. See summary information below for more information. Should this problem persist, stop the service and check the configuration file(s) for possible error(s) in the configuration of the categories and sinks.


Summary for Enterprise Library Distributor Service:
======================================
-->

<UNEXPECTED ERROR END>

<EXPECTED ERROR START>

Message:
Timestamp: 10/18/2012 3:21:52 PM
Message: HandlingInstanceID: 3a4b995a-ce28-412f-94cd-71885d921cd9
An exception of type 'System.InvalidCastException' occurred and was caught.
---------------------------------------------------------------------------
10/18/2012 10:21:52
Type : System.InvalidCastException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Conversion from string "/2011" to type 'Date' is not valid.
Source : Microsoft.VisualBasic
Help link :
Data : System.Collections.ListDictionaryInternal
TargetSite : System.DateTime ToDate(System.String)
Stack Trace :    at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value)
   at abcd.abcd.abcd.abcd.getStartDate(String dates, Char[] splitChar, String beginningReportSeason)

Additional Info:

MachineName : abc
TimeStamp : 10/18/2012 3:21:52 PM
FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
AppDomainName : /LM/W3SVC/736956694/ROOT-6-129950456342322189
ThreadIdentity : abcd
WindowsIdentity : abcd

Category: SiteErrorCategory
Priority: 0
EventId: 100
Severity: Error
Title:Enterprise Library Exception Handling
Machine: abcd
App Domain: /LM/W3SVC/736956694/ROOT-6-129950456342322189
ProcessId: 5040
Process Name: c:\windows\system32\inetsrv\w3wp.exe
Thread Name:
Win32 ThreadId:3732
Extended Properties:
--> MachineName: abcd
--> TimeStamp: 10/18/2012 3:21:52 PM
--> FullName: Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
--> AppDomainName: /LM/W3SVC/736956694/ROOT-6-129950456342322189
--> WindowsIdentity: abcd

<EXPECTED ERROR ENDS>


<UNEXPECTED ERROR START>

Exception Information Details:
======================================
Exception Type: System.ArgumentException
Message: The parameter 'address' cannot be an empty string.
Parameter name: address
ParamName: address
Data: System.Collections.ListDictionaryInternal
TargetSite: Void .ctor(System.String, System.String, System.Text.Encoding)
HelpLink: NULL
Source: System

StackTrace Information Details:
======================================
   at System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding)
   at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailMessage.CreateMailMessage()
   at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailMessage.Send()
   at Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.TraceListenerWrapper.TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, Object data)
   at Microsoft.Practices.EnterpriseLibrary.Logging.LogSource.TraceData(TraceEventType eventType, Int32 id, LogEntry logEntry, TraceListenerFilter traceListenerFilter, TraceEventCache traceEventCache)
   at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterImpl.ProcessLog(LogEntry log, TraceEventCache traceEventCache)

Category:

Priority: -1

EventId: 6352

Severity: Error

Title:

Machine: abcd

App Domain: /LM/W3SVC/736956694/ROOT-6-129950456342322189

ProcessId: 5040

Process Name: c:\windows\system32\inetsrv\w3wp.exe

Thread Name:

Win32 ThreadId:3732

Extended Properties:
****************************************

<UNEXPECTED ERROR END>

 

Oct 18, 2012 at 11:40 PM

The message says: "Message: The parameter 'address' cannot be an empty string."  This means that toAddress or fromAddress is empty in the configuration file.  You should verify that the configuration is correct and deployed to the proper location.  If it looks OK, I would check for other web.config files that may be overriding settings.  You could search for any file that contains "EmailTraceListenerData".

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Oct 19, 2012 at 3:53 AM

Randy,  I have double checked both toAddress and fromAddress are correctly specified in the web.config. I don't know why I am getting address cannot be empty string.

Secondly, I am seeing the below error after putting the specialSource as per your defined configuration. Maybe, it's the real cause. Can you please guide why I am seeing this.

Error:

Message: Tracing to LogSource 'SiteErrorCategory' failed. Processing for other sources will continue. See summary information below for more information. Should this problem persist, stop the service and check the configuration file(s) for possible error(s) in the configuration of the categories and sinks.

Oct 19, 2012 at 4:43 AM

That message is the standard message when logging fails.  The root cause is the address being empty as per the error message.  You can try to track down if there are any other possible config files that are being used.  

You can also output the configuration information to see what it is set to:

var logSettings = ConfigurationManager.GetSection(LoggingSettings.SectionName) as LoggingSettings;
var emailListener = logSettings.TraceListeners.Single(
    listener => listener.Name == "Error_EmailListener") as EmailTraceListenerData;

System.Diagnostics.Trace.WriteLine("ToAddress: " + emailListener.ToAddress);
System.Diagnostics.Trace.WriteLine("FromAddress: " + emailListener.FromAddress);

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Oct 19, 2012 at 6:08 AM

I have pulled toAddress and fromAddress using your above mentioned code. It's containing the correct addresses which aren't empty at all. But I am still seeing these errors. Please advise.

1. Tracing to LogSource 'SiteErrorCategory' failed.

2. The parameter 'address' cannot be an empty string.

Error:

Timestamp: 10/19/2012 12:55:50 AM

Message: Tracing to LogSource 'SiteErrorCategory' failed. Processing for other sources will continue. See summary information below for more information. Should this problem persist, stop the service and check the configuration file(s) for possible error(s) in the configuration of the categories and sinks.


Summary for Enterprise Library Distributor Service:
======================================
-->

Exception Information Details:
======================================
Exception Type: System.ArgumentException
Message: The parameter 'address' cannot be an empty string.
Parameter name: address
ParamName: address
Data: System.Collections.ListDictionaryInternal
TargetSite: Void .ctor(System.String, System.String, System.Text.Encoding)
HelpLink: NULL
Source: System

StackTrace Information Details:
======================================
   at System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding)
   at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailMessage.CreateMailMessage()
   at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailMessage.Send()
   at Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.TraceListenerWrapper.TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, Object data)
   at Microsoft.Practices.EnterpriseLibrary.Logging.LogSource.TraceData(TraceEventType eventType, Int32 id, LogEntry logEntry, TraceListenerFilter traceListenerFilter, TraceEventCache traceEventCache)
   at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterImpl.ProcessLog(LogEntry log, TraceEventCache traceEventCache)

Category:

Priority: -1

EventId: 6352

Severity: Error

Title:

Machine: abcd

App Domain: /LM/W3SVC/736956694/ROOT-1-129950997364332257

ProcessId: 3788

Process Name: c:\windows\system32\inetsrv\w3wp.exe

Thread Name:

Win32 ThreadId:3580

Extended Properties: 


Oct 19, 2012 at 6:38 AM

I would recommend creating a self-contained project that can reproduce your issue.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Oct 19, 2012 at 5:07 PM

Thanks Randy and appreciate your prompt responses. I figured out the issue after looking into the Enterprise Library Code.

It was failing because I had multiple email addresses separated by semicolon and the last address has ending semicolon which caused the empty string address when split and added to the Recipient list in Enterprise Library code.

Once I removed that last semicolon it start working. I think it would be good to TRIM and check for empty toAddress in the code after split but prior to add to MailMessage recipient list. It's in the CreateMailMessage method of EmailMessage.cs in Logging.2010 project.