Getting Error in Custom Database Trace Listener

Topics: Enterprise Library Core, Logging Application Block
Sep 8, 2011 at 8:49 PM
Edited Sep 8, 2011 at 9:21 PM

Hi,

I am trying to create custom trace listener for logging.

I have created following so far and getting below error -

An unhandled exception of type 'System.StackOverflowException' occurred in Microsoft.Practices.EnterpriseLibrary.Logging.DLL

 

// I have added one new method to Logger.cs

public static void Write(string errorText, System.DateTime errorDate, string memberCode, int reservationId, int transactionId, int messageId, string submittedXml, string originAddress, string errorCode, string transTypeCode, string bookingResId, string appName, string appProcessId)
        {
            Writer.Write(errorText, errorDate, memberCode, reservationId, transactionId, messageId, submittedXml, originAddress, errorCode, transTypeCode, bookingResId, appName, appProcessId);
        }

// Added the write method to LogWriter.cs
 public void Write(string errorText, System.DateTime errorDate, string memberCode, int reservationId, int transactionId, int messageId, string submittedXml, string originAddress, string errorCode, string transTypeCode, string bookingResId, string appName, string appProcessId)
        {
            
                this.Write(errorText, errorDate, memberCode, reservationId, transactionId, messageId, submittedXml, originAddress, errorCode, transTypeCode, bookingResId, appName, appProcessId);
           
        }

 

 

 

After that I have created a custom class as per one another thread.

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Configuration;

namespace Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners
{
    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    class MyTraceListener : CustomTraceListener
    {
        public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data)
        {

            try
            {
                Database db = DatabaseFactory.CreateDatabase(ConfigurationManager.ConnectionStrings["Logging"].ConnectionString);
                SqlCommand cmd = new SqlCommand("usp_WriteErrorlog");
                cmd.CommandType = CommandType.StoredProcedure;
                ExtendedLogEntry le = (ExtendedLogEntry)data;

                        db.AddInParameter(cmd, "ErrorText", DbType.String, le.ErrorText);
                        db.AddInParameter(cmd, "ErrorDate", DbType.DateTime,  le.ErrorDate);
                        db.AddInParameter(cmd, "MemberCode", DbType.String, le.MemberCode);
                        db.AddInParameter(cmd, "ReservationID", DbType.Int64, le.ReservationId);
                        db.AddInParameter(cmd, "TransactionID", DbType.Int64, le.TransactionId);
                        db.AddInParameter(cmd, "MessageID", DbType.Int64, le.MessageId);
                        db.AddInParameter(cmd, "SubmittedXml", DbType.String, le.SubmittedXml);
                        db.AddInParameter(cmd, "OrigAddress", DbType.String, le.OriginAddress);
                        db.AddInParameter(cmd, "ErrorCode", DbType.String, le.ErrorCode);
                        db.AddInParameter(cmd, "TransTypeCode", DbType.String, le.TransTypeCode);
                        db.AddInParameter(cmd, "BookingResID", DbType.String, le.BookingResponseId);
                        db.AddInParameter(cmd, "AppName", DbType.String, le.AppName);
                        db.AddInParameter(cmd, "ProcessID", DbType.String, le.ProcessId);    
                        db.ExecuteNonQuery(cmd);
                   
            }
            catch (Exception ex)
            {
                throw ex;
            }

           
        }

        public override void Write(string message)
        {
            throw new NotImplementedException();
        }

        public override void WriteLine(string message)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Extended properties for Error Logging
    /// </summary>
    public class ExtendedLogEntry : LogEntry
    {
        /// <summary>
        /// Error Test
        /// </summary>
        public string ErrorText { get; set; }
        /// <summary>
        /// Error Test
        /// </summary>
        public DateTime ErrorDate { get; set; }
        /// <summary>
        /// Error Test
        /// </summary>
        public string MemberCode { get; set; }
        /// <summary>
        /// Error Test
        /// </summary>
      
        public int ReservationId { get; set; }
        /// <summary>
        /// Error Test
        /// </summary>
        public int TransactionId { get; set; }
        /// <summary>
        /// Error Test
        /// </summary>
        public int MessageId { get; set; }
        /// <summary>
        /// Error Test
        /// </summary>
        public string SubmittedXml { get; set; }
        /// <summary>
        /// Error Test
        /// </summary>
        public string OriginAddress { get; set; }
        /// <summary>
        /// Error Test
        /// </summary>
        public string ErrorCode { get; set; }
        /// <summary>
        /// Error Test
        /// </summary>
        public string TransTypeCode { get; set; }
        /// <summary>
        /// Error Test
        /// </summary>
        public string BookingResponseId { get; set; }
        /// <summary>
        /// Error Test
        /// </summary>
        public string AppName { get; set; }
        /// <summary>
        /// Error Test
        /// </summary>
        public string AppProcessId { get; set; }
       
    }


}

This is my config file 

<?xml version="1.0"?>
<configuration>

  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral" requirePermission="true" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral" requirePermission="true" />
  </configSections>
  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral"
        databaseInstanceName="Mingus" writeLogStoredProcName="usp_WriteErrorLog"
        addCategoryStoredProcName="AddCategory" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral"
        template="Timestamp: {timestamp}{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="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <dataConfiguration defaultDatabase="Mingus" />
  <connectionStrings>
    <add name="Mingus" connectionString="data source=localhost;Initial Catalog=Logging;Integrated Security=SSPI;"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>


 

When i try to run this it is giving me below error -

An unhandled exception of type 'System.StackOverflowException' occurred in Microsoft.Practices.EnterpriseLibrary.Logging.DLL

Sep 9, 2011 at 3:58 AM

Hi,

It seems that there is an infinite loop happening somewhere in your code but I can't see it to your post above. Are you using threading here? Also I noticed in your configuration that you are not actually using the custom trace listener.

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

Sep 9, 2011 at 1:32 PM

Noel,

Could you send me the code for custom database trace listener?

I already emailed on the email address.

Sep 11, 2011 at 12:17 PM

Solution sent, let us know if you already received it.

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

Sep 12, 2011 at 1:36 PM

I have received it.

Thanks