AOP and IOC in WCF 4.0 with Enterprise Library 5 and AppFabric, part 2

In my past post we created a WCF service with IoC and AoP capabilities.

Now we will create a custom entlib trace listener that will write to the AppFabric monitoring.

In order to use that I took a Microsoft class, WCFUserEventProvider, from the examples and created a wrapper inheriting from the CustomTraceListener Entlib class.

  /// <summary>
    /// Trace listener for writting into AppFabric
    /// </summary>
    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class AppFabricTraceListener : CustomTraceListener
    {
        //provider for writting into appfabric
        private static WCFUserEventProvider eventProvider = new WCFUserEventProvider();


        public override void Write(string message)
        {

          
        }

        public override void WriteLine(string message)
        {
           
        }

        /// <summary>
        /// Writes trace information, a data object and event information to the listener specific output.
        /// </summary>
        /// <param name="eventCache">A <see cref="T:System.Diagnostics.TraceEventCache"/> object that contains the current process ID, thread ID, and stack trace information.</param>
        /// <param name="source">A name used to identify the output, typically the name of the application that generated the trace event.</param>
        /// <param name="eventType">One of the <see cref="T:System.Diagnostics.TraceEventType"/> values specifying the type of event that has caused the trace.</param>
        /// <param name="id">A numeric identifier for the event.</param>
        /// <param name="data">The trace data to emit.</param>
        /// <PermissionSet>
        /// 	<IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
        /// 	<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode"/>
        /// </PermissionSet>
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if (!(data is LogEntry))
                return;

            var dataInfo = data as LogEntry;

            switch (eventType)
            {
                case TraceEventType.Critical:
                    WriteErrorToAppFabric(dataInfo);
                    break;
                case TraceEventType.Error:
                    WriteErrorToAppFabric(dataInfo);
                    break;
                case TraceEventType.Information:
                    WriteInformationToAppFabric(dataInfo);
                    break;
                case TraceEventType.Resume:
                    WriteInformationToAppFabric(dataInfo);
                    break;
                case TraceEventType.Start:
                    WriteInformationToAppFabric(dataInfo);
                    break;
                case TraceEventType.Stop:
                    WriteInformationToAppFabric(dataInfo);
                    break;
                case TraceEventType.Suspend:
                    WriteInformationToAppFabric(dataInfo);
                    break;
                case TraceEventType.Transfer:
                    WriteInformationToAppFabric(dataInfo);
                    break;
                case TraceEventType.Verbose:
                    WriteInformationToAppFabric(dataInfo);
                    break;
                case TraceEventType.Warning:
                    WriteWarningToAppFabric(dataInfo);
                    break;
                default:
                    break;
            }


        }


        /// <summary>
        /// Writes the error to app fabric.
        /// </summary>
        /// <param name="dataInfo">The data info.</param>
        private static void WriteErrorToAppFabric(LogEntry dataInfo)
        {
            eventProvider.WriteErrorEvent(dataInfo.Title, dataInfo.Message);
        }


        /// <summary>
        /// Writes the warning to app fabric.
        /// </summary>
        /// <param name="dataInfo">The data info.</param>
        private static void WriteWarningToAppFabric(LogEntry dataInfo)
        {
            eventProvider.WriteWarningEvent(dataInfo.Title, dataInfo.Message);
        }


        /// <summary>
        /// Writes the information to app fabric.
        /// </summary>
        /// <param name="dataInfo">The data info.</param>
        private static void WriteInformationToAppFabric(LogEntry dataInfo)
        {
            eventProvider.WriteInformationEvent(dataInfo.Title, dataInfo.Message);
        }
    }

Our next step is to configure the policy injection for using this logging in the exception handling.

Here is the code.

Advertisements

2 comments

  1. Hi Pablo,
    This is good stuff! I’d love to see part 3 where you configure the policy injection!

    Regards,
    Josh

    1. Josh, thanks for the comment!

      I think it is configured in the example. In my free time I am totally focused in a personal project in which I am involved, so I will not blog for a long time.

      Regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: