Custom Logging to Database

Nov 11, 2009 at 7:58 AM

Hi,

I have implemented custom logging using the Logging application Blocks 4.1.

The custom logging works fine. But, I have a problem.

I have an AuditTrailLogEntry  class which is derrived from LogEntry.

I have an AuditTrailTraceListener class derrived from CustomTraceListener class.

I have an AuditTrail category that uses AuditTrailTraceListener as a tracelistener.

I have a General category that uses Formatted EventLog TraceListener trace listener.

When I create an instance of AuditTrailLogEntry class and set AuditTrail as its category, everything seems to work fine.

When I create an instance of LogEntry class and set General as its category, it still goes into the code of my derrived AuditTrailTraceListener  and tries to log to the custom database.

I am using FileConfigurationSource and not the SystemConfigurationSource.

Could you please help me out here?

Regards,

Parag.

 

AuditTrailLogEntry

namespace CBSBLL.Logging
{
    public class AuditTrailLogEntry : LogEntry
    {
        public AuditTrailLogEntry()
            : base()
        {
            this.EntryDate = DateTime.Now;

        }
        string _BankID;
        public string BankID
        {
            get { return _BankID; }
            set { _BankID = value; }
        }
        string _BranchID;
        public string BranchID
        {
            get { return _BranchID; }
            set { _BranchID = value; }
        }
        short _EntryTypeID;
        public short EntryTypeID
        {
            get { return _EntryTypeID; }
            set { _EntryTypeID = value; }
        }
        byte _OperationTypeID;
        public byte OperationTypeID
        {
            get { return _OperationTypeID; }
            set { _OperationTypeID = value; }
        }
        string _SearchKey;
        public string SearchKey
        {
            get { return _SearchKey; }
            set { _SearchKey = value; }
        }
        public readonly DateTime EntryDate = DateTime.Now;
        string _UserID;
        public string UserID
        {
            get { return _UserID; }
            set { _UserID = value; }
        }
        string _OldRecordDetails;
        public string OldRecordDetails
        {
            get { return _OldRecordDetails; }
            set { _OldRecordDetails = value; }
        }
        string _NewRecordDetails;
        public string NewRecordDetails
        {
            get { return _NewRecordDetails; }
            set { _NewRecordDetails = value; }
        }

    }

    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class AuditTrailTraceListener : CustomTraceListener
    {

        public AuditTrailTraceListener()
            : base()
        {

        }

        public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data)
        {
            base.TraceData(eventCache, source, eventType, id, data);
           
            if ((data != null) && (data.GetType() == typeof(AuditTrailLogEntry)))
           
            {
                if (data is AuditTrailLogEntry)
                {
                    AuditTrailLogEntry ATLE = data as AuditTrailLogEntry;
                    Database db = AppHelper.GetDB(CBSBLL.Constants.ApplicationBlocks.ConnectionStrings.AUDIT_TRAIL_CONNECTIONSTRING_NAME);
                    using (System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("InsertAuditTrail"))
                    {
                        db.AddInParameter(cmd, "BankID", System.Data.DbType.String, ATLE.BankID);
                        db.AddInParameter(cmd, "BranchID", System.Data.DbType.String, ATLE.BranchID);
                        db.AddInParameter(cmd, "EntryTypeID", System.Data.DbType.Int16, ATLE.EntryTypeID);
                        db.AddInParameter(cmd, "OperationTypeID", System.Data.DbType.Byte, ATLE.OperationTypeID);
                        db.AddInParameter(cmd, "SearchKey", System.Data.DbType.String, ATLE.SearchKey);
                        db.AddInParameter(cmd, "EntryDate", System.Data.DbType.DateTime, ATLE.EntryDate);
                        db.AddInParameter(cmd, "UserID", System.Data.DbType.String, ATLE.UserID);
                        db.AddInParameter(cmd, "OldRecordDetails", System.Data.DbType.String, ATLE.OldRecordDetails);
                        db.AddInParameter(cmd, "NewRecordDetails", System.Data.DbType.String, ATLE.NewRecordDetails);
                        db.ExecuteNonQuery(cmd);
                    }

                }
            }
        }

 

        public override void Write(string message)
        {

        }

        public override void WriteLine(string message)
        {

        }
    }
}

 

 

AppHelper

namespace CBSBLL.Logging
{
    public static partial class AppHelper
    {

        static FileConfigurationSource _CBSppBlocksSConfigurationSource;

        private static FileConfigurationSource CBSppBlocksSConfigurationSource
        {
            get
            {
                if (AppHelper._CBSppBlocksSConfigurationSource == null)
                {
                    AppHelper._CBSppBlocksSConfigurationSource = new FileConfigurationSource(Constants.ApplicationBlocks.CONFIGURATION_FILE_NAME);
                }
                return AppHelper._CBSppBlocksSConfigurationSource;
            }

        }


        public static Database GetDB(string connStr)
        {
            DatabaseProviderFactory CBSDatabaseProviderFactory = new DatabaseProviderFactory(CBSppBlocksSConfigurationSource);
            Database db = CBSDatabaseProviderFactory.Create(connStr);
            return db;
        }

 

        public static void WriteLog(LogEntry ale)
        {
            LogWriterFactory CBSLogWriterFactory = new LogWriterFactory(AppHelper.CBSppBlocksSConfigurationSource);
            LogWriter lw = CBSLogWriterFactory.Create();
            lw.Write(ale);
           
        }


    }
}

 

Nov 11, 2009 at 9:13 AM

Hi,

Could you post your config?

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