Exception shielding works but Logging does not

Topics: Exception Handling Application Block, Logging Application Block
Mar 29, 2011 at 11:04 AM

I have implemented ExceptionShielding on the WCF service and the following is the policy  being used, in case of exception the fault contract is returned but somehow the logging does not works at all. if i use the following policy (minus the faultContract part) in an ASP.Net application the logging works perfect but for WCF it does not. what is it that i am missing ?

 

<add name="MyServicePolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <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="System.ServiceModel" eventId="9000" severity="Error"
                title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                priority="5" />
              <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="Service error. Please contact your administrator."
                faultContractType="MyService.MyServiceFault, MyService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Fault Contract Exception Handler">
                <mappings>
                  <add source="{Guid}" name="FaultID" />
                  <add source="{Message}" name="FaultMessage" />
                </mappings>
              </add>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>

Mar 30, 2011 at 3:32 AM
Edited Mar 30, 2011 at 3:33 AM

Hi,

Your config looks fine. Can you post here your logging configuration?

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 30, 2011 at 3:39 AM

Just so you know, the logging works fine when i use the same configuration in a ASP.net application

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="System.ServiceModel">
    <listeners>
      <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
        source="Enterprise Library Logging" formatter="Text Formatter"
        log="Application" machineName="" traceOutputOptions="None" filter="Error" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
        template="Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}Severity: {severity}{newline}Title:{title}{newline}Machine: {localMachine}{newline}App Domain: {localAppDomain}{newline}ProcessId: {localProcessId}{newline}Process Name: {localProcessName}{newline}Thread Name: {threadName}{newline}Win32 ThreadId:{win32ThreadId}{newline}Extended Properties: {dictionary({key} - {value}{newline})}"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="System.ServiceModel">
        <listeners>
          <add name="Event Log Listener" />
        </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="Event Log Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
the following is the code for Service
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF
Imports Microsoft.Practices.EnterpriseLibrary.Logging
' NOTE: You can use the "Rename" command on the context menu to change the class name "CustomerService" in code, svc and config file together.
<ExceptionShielding("MyServicePolicy")> _
Public Class CustomerService
    Implements ICustomerService


    Public Function GetCustomers() As System.Collections.Generic.List(Of MyBusinessLayer.Customer) Implements ICustomerService.GetCustomers
        Return CustomerController.GetCustomers
    End Function

    Public Function CalculateProfit(ByVal sale As Integer, ByVal expenses As Integer) As Integer Implements ICustomerService.CalculateProfit
        Return CustomerController.CalculateProfit(sale, expenses)
    End Function
End Class
 
The following is the code for the controller class method which is called from the service and which throws the exception
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.Logging
Public Class CustomerController

       Public Shared Function CalculateProfit(ByVal sale As Integer, ByVal expenses As Integer) As Integer
        Dim a As Integer = (sale - expenses) / 0
        Return a
    End Function
End Class

Mar 30, 2011 at 3:57 AM

Here is the source code of the complete application. Interestingly, I am not getting the FaultContract also back from WCF service when calling it from the Client application. Its a very simple app with just 1 button and 1 function modeled around our applictaion architecture.

We want to integrate the complete enterprise library in our application and this sample is just a pilot in that direction.

Mar 30, 2011 at 5:06 AM
Edited Mar 30, 2011 at 5:10 AM

You seems encounter the same problem just like on this thread. Can you try changing your listener to Flat File and see if it still can't log. Also, you can send me a repro project so I can test it on my end.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 30, 2011 at 5:09 AM

Here is the repro project. In the meantime, i will try Flat File listener as well. However, if listener is a problem then ideally even the ASP.net logging should not work whereas that works but WCF Service logging does not works with the same configuration.

Mar 30, 2011 at 6:09 AM

I tried the repro project you provided. The logging works at my end. The issue might be something environment specific. I suggest you configure your logging block to catch logging errors and point it to a Flat File (default is Event Log) so we can analyze the cause of your problem.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 30, 2011 at 6:30 AM

Hey Noel,

Did you try "Calculate Profit - Service Call" ? and did the WCF log the error in Event Log ? Let me try configuring logging block to catch logging errors.

 

Mar 30, 2011 at 6:39 AM

Yes, I also stepped into your code to make sure that it is calling your WCF service.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 30, 2011 at 6:43 AM

I just tried to catch the exception in loggin using a Flat File Listener but again nothing at all. Here is the updated repro project.

Mar 30, 2011 at 7:09 AM

Does it log anything? I just noticed that your Logging and Logging Error categories are pointing to a common Flat File Listener. I recommend you to try these one:

- Point your Logging to Event Log Listener and your Logging Error to a Flat File. (Does it catch any logging errors and properly logged it into the specified Flat File?)

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Mar 30, 2011 at 7:14 AM

Hiya, i had made that "Everything logging to Flat File" change to test the flat file logging. I just made the change as suggested by you, but it still does not logs anything in event viewer and does not even creates a Flat file for the logging. Following is the updated section of config file.

<categorySources>
      <add switchValue="All" name="System.ServiceModel">
        <listeners>
          <add name="Event Log Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Flat File Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Flat File Trace Listener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>

 

Mar 30, 2011 at 7:35 AM

Since I can't repro your problem here at my end, I suggest you step into the EntLib source code and see what it really happens on the background. You can refer to this thread on how to debug Enterprise Library source.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 30, 2011 at 8:31 AM

Hey Noel,

The interesting part is the moment I setup debugging for enterprise blocks logging (as per the link sent by you), i did not even have to step into enterprise library and the logging worked for Event Viewer & Flat File as well. I did not change anything in the config or code.

Any idea, what could have made it to work ?

 

 

 

 

Mar 30, 2011 at 8:54 AM

Its good to hear that your logging is already working :). Honestly, I don't have any idea on why it works suddenly. I don't think it has something to do with pdb files but who knows?!

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com