Custom logging to a database with Logging Application Blocks

Topics: Logging Application Block
Nov 2, 2009 at 8:52 AM

Hi,

Can anyone guide me as to how to log custom information to a custom database using Logging Application Blocks?

What i mean is my 'LogEntry' Class will have custom properties which will be saved to the database. 

Regards,

Parag.

Nov 3, 2009 at 4:10 AM

Hi,

First, you would create your custom tables and stored procedures.

Next, You would need to extend the LogEntry class adding your custom properties:

    public class ExtendedLogEntry : LogEntry
    {
        public ExtendedLogEntry()
            : base()
        { 

        }

        public string Property1 { get; set; }
        public string Property2 { get; set; }
        public string Property3 { get; set; }
    }

Next is to create your custom Database trace listener which is able to handle and insert the information from your extended log entry:

    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class ExtendedDatabaseTraceListener : CustomTraceListener
    {
        private const string dbKey = "dbaseName";
        private string actionSproc = "extendedSproc";
        Database database;

        public ExtendedDatabaseTraceListener()
        { 
            
        }

        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(ExtendedLogEntry)))
            {
                //implement a logic that will insert the ExtendedLogEntry to the database
            }
        }



        public override void Write(string message)
        {
            
        }

        public override void WriteLine(string message)
        {
            
        }
    }
Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Nov 3, 2009 at 11:07 AM

Hi,

Thanks very much for the quick reply.

In the meanwhile I got it working, but did it another way (which, i believe,  is at best 'the long way' and at worst 'the wrong way'!).

I created a CustomLogEntry Class which extends the LogEntry Class.

Then I created overloads for all the related methods (so as to accept  CustomLogEntry class as a parameter instead of the original LogEntry Class) in Logger, Logger, LogSource, LogWriter and FormattedDatabaseTraceListener classes 

Then I created an overload for ExecuteWriteLogStoredProcedure which accepts the CustomLogEntry class as a parameter and added the requisite DB parameters which map to my extraneous information.

Worked like a charm.

Thanks again for the quick response, the pnp team has done a great job.

 

Best Regards,

Parag.

 

 

Nov 9, 2009 at 4:18 AM
[ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class ExtendedDatabaseTraceListener : CustomTraceListener
    {
        private const string dbKey = "dbaseName";
        private string actionSproc = "extendedSproc";
        Database database;

        public ExtendedDatabaseTraceListener()
        { 
            
        }

        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(ExtendedLogEntry)))
            {
                //implement a logic that will insert the ExtendedLogEntry to the database
            }
        }



        public override void Write(string message)
        {
            
        }

        public override void WriteLine(string message)
        {
            
        }
    }

I tried to do so ........but problem is  it never entered into if loop...........i checked data.GetType never is type of CustomLogEntry .....
 base.TraceData(eventCache, source, eventType, id, data);
            if ((data != null) && (data.GetType() == typeof(ExtendedLogEntry)))
            {
                //implement a logic that will insert the ExtendedLogEntry to the database
            }
help me in this one........
Nov 9, 2009 at 4:54 AM

Well, obvious question, did you passed an object of type ExtendedLogEntry in your Logger.Write call? 

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 9, 2009 at 5:33 AM

Well, I would ask the same question.

You specified CustomLogentry which is the one I had used instead of ExtendedLogEntry which the support team suggested.

I had run into the same problem using MY method, and i resolved it by putting in a "If (data Is CustomLogEntry)" before "If (data Is LogEntry)".

 I resorted to the method suggested by the support team and I would advise you to do the same.

The only trouble is the method worked just fine for me, so I do not have an answer for your question! :)

Regards,

 

Nov 9, 2009 at 1:17 PM

Hi thank you for the reply.......where do we need to call Logger.Write as I am using policy injection in my application.

Nov 9, 2009 at 1:27 PM

You are welcome.

Unfortunately your question does not tell me much.

Kindly ellaborate.

I might as well tell you that i have not used the policy injection blocks.

Regards,

 

Nov 10, 2009 at 2:29 AM

Ah, so you're using the LoggingHandler of the policy injection, right?  It really won't enter the if loop inside the TraceData method as the LoggingHandler doesn't know and definitely wouldn't create your custom LogEntry class.  (It actually creates a TraceLogEntry)  The way around this is to create your own custom call handler that will do this. - http://msdn.microsoft.com/en-us/library/dd203272.aspx.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com