Custom Database listener

Topics: Logging Application Block
Mar 23, 2011 at 1:44 PM

Hi ,

Need to have one more column to the log table of the enterprise library.

Need to write a custom Database listener .How can i achive this.

Please do send me a sample code for the same.

 

Thanks.

Mar 24, 2011 at 12:29 AM

Hi,

Also require the following information

I need to use this custom database listener to log my exception caught How will i do this.

 

Mar 24, 2011 at 1:00 AM

I have a sample custom database trace listener.  Send me an email so I could send the sample to you.  As for your requirement on logging the exception, you only need to configure it in the configuration file.  Define a logging category and reference the custom trace listener within that category. Create an exception policy, associate a logging exception handler to it and add the logging category you created to its Logging Category property. 

You can also check out Enterprise Library's Extensibility labs.  It has a hands on lab for creating a custom trace listener.

 

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

Mar 24, 2011 at 1:53 AM

As requested sent you a mail.Can please send the sample code ASAP.

Thanks for ur help.

 

Mar 24, 2011 at 2:40 AM

I haven't received any email yet, make sure you sent it to entlib.support@avanade.com

 

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

Mar 24, 2011 at 4:19 AM

Hi

 

Trying to run the sample code.It is not writing any to the custom table.

I have changed the connection string has required.

Mar 24, 2011 at 6:46 AM

Hi Santosh,

Sorry and thanks for pointing it out.  I have sent the updated script file.  I missed a column in the Log table and in the WriteLog sp.

 

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

Mar 24, 2011 at 7:05 AM

Thanks for help.

Now trying to log the exception using this custom Database listener.

This is my setting

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <exceptionHandling>
        <exceptionPolicies>
            <add name="Policy">
                <exceptionTypes>
                    <add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                        postHandlingAction="NotifyRethrow">
                        <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="Database" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                                priority="0" />
                        </exceptionHandlers>
                    </add>
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
            <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                source="Enterprise Library Logging" formatter="Text Formatter"
                log="" machineName="." traceOutputOptions="None" />
            <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                DatabaseInstance="CustomLoggingDB" WriteLogSp="WriteLog" AddCategorySp="AddCategory"
                type="CustomProvider.BasicDatabaseTraceListener, CustomProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="BasicDatabaseTraceListener" />
            <add name="Full Database Trace Listener" type="CustomProvider.FullDatabaseTraceListener, CustomProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                listenerDataType="CustomProvider.FullDatabaseTraceListenerData, CustomProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                databaseInstanceName="CustomLoggingDB" writeLogStoredProcName="WriteLog"
                addCategoryStoredProcName="AddCategory" formatter="Text Formatter" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                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="Event Log Listener" />
                </listeners>
            </add>
            <add switchValue="All" name="Database">
                <listeners>
                    <add name="BasicDatabaseTraceListener" />
                </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>
    <dataConfiguration defaultDatabase="CustomLoggingDB" />
    <connectionStrings>
        <add name="CustomLoggingDB" connectionString="User Id=bbusers;Password=bluebutton;Initial Catalog=temp123;Data Source=192.168.2.7;"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

 

But it is not logging.the exception can you please let me what i have done is wrong.

Mar 24, 2011 at 7:37 AM

It's not logging because my code assumes there's always a contextId information in the LogEntry's extended properties.  The highlighted code is the specific line of code which causes the error:

if (contextLog != null)
{
          db.AddInParameter(dbCommand, "ContextID", System.Data.DbType.Int32, contextLog.ContextId);
}
else
{
          db.AddInParameter(dbCommand, "ContextID", System.Data.DbType.Int32, int.Parse(logEntry.ExtendedProperties["contextId"].ToString()));
}

It always assumes there's a contextId in the ExtendedProperties property, up to you how you want to handle this scenario.  You may opt to check if it exists first before trying to use it. The solution I sent to you is only a sample so you can expect to find errors but it's pretty much easy to debug.  Just put a breakpoint inside the custom trace listener's code.  It's not meant as a full blown example.

 

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

Jul 27, 2011 at 1:58 PM

Hi Sarah,

Can you please also send me the example of the Custom Database Trace Listener?

I have just sent one email to the provided support address.

Regards,

Lauro

Jul 28, 2011 at 3:06 AM

Hi Lauro,

I've already send the sample solution. Let me know if you received it.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Dec 4, 2011 at 11:12 AM

Dears,

 

I urgently need this code sample so i want to add my custom column to log table, could any one send me this sample????

Mar 19, 2012 at 2:21 PM

Hi Avanade support,

Could you please share the source code of this custom trace listner with me. I have already sent an email. The Auto reply message stated that I need to create a discussion for achieving the same. I have still not received the code.

 

Mar 19, 2012 at 6:47 PM

You can find a sample custom trace listener (not this exact one) on the Enterprise Library Sample Projects page in the Extended Properties Trace Listener with Custom Exception Handler project.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Apr 25, 2012 at 1:57 PM
AvanadeSupport wrote:

I have a sample custom database trace listener.  Send me an email so I could send the sample to you.  As for your requirement on logging the exception, you only need to configure it in the configuration file.  Define a logging category and reference the custom trace listener within that category. Create an exception policy, associate a logging exception handler to it and add the logging category you created to its Logging Category property. 

You can also check out Enterprise Library's Extensibility labs.  It has a hands on lab for creating a custom trace listener.

 

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

Hi guys of Avanade support!

Sarah,
Could you please send me this custom database trace listener code? We need to migrate to entlib 5.0 the listener we did for entlib 2.0. 
Could you send? 

RandyLevy,
We had a look at the example of the HandsOn, but it's not the same. If possible, we would like a custom database trace listener, not a custom trace listener.

Thanks in advance.

Lauro Valente
Volvo IT 

Apr 25, 2012 at 4:09 PM

Did you look at the Extended Properties Trace Listener with Custom Exception Handler Sample Project?  That project has a custom database trace listener and should help.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

May 30, 2012 at 5:50 PM

Hello ,

I downloaded the sample project  and tried works great except when I  try to open the App.config  from the sample Console App using the configuration tool 

there is an error

Error loading Section loggingconfiguartion

Error Message : Invalid  TracelistenerData type in configuration.

listenerDataType="ExtendedPropertyDatabaseListener.ExtendedFormattedDatabaseTraceListenerData, ExtendedPropertyDatabaseListener"

 

am I missing some reference .  I  am just starting to work this and need help

 

Thanks

May 30, 2012 at 7:15 PM
Edited May 30, 2012 at 8:37 PM

Thanks for downloading the sample trace listener.  I assume that you are using the "Edit Configuration File" context menu option (right click).  To get around this use the right click->"Edit Enterprise Library V5 Configuration".  Then a warning will appear inside of the configuration instead of an error but the configuration will load.

The other (better) solution is to update the Enterprise Library binaries path for the solution:

  1. Click on the solution in the solution explorer
  2. Open the properties window (ctrl-w ctrl-p -- NOT right click->Properties)
  3. Append ";ExtendedPropertyDatabaseListener\bin\Debug" to the "Enterprise Library binaries path" setting

Thanks for pointing out that issue.  Later today, I will update the solution with that change so that it "just works".

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

May 30, 2012 at 7:18 PM

Thank you for your prompt response . I will give it a try ..

 

Thanks again

May 30, 2012 at 8:36 PM
Edited May 30, 2012 at 8:37 PM

Oops, I gave you the wrong path to append! It should be: ";ExtendedPropertyDatabaseListener\bin\Debug".  I've updated the original response above with the correct path.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

May 30, 2012 at 8:53 PM
Edited May 30, 2012 at 8:56 PM

I've also updated the sample project.  You should be able to download, build, and then use "Edit configuration file" with no error.  Try it out and let me know if you have any issues.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

May 30, 2012 at 8:55 PM

Thank you again . I will download the project to use the config tool. thank much for helping a novice like me

 

May 31, 2012 at 2:30 PM
Edited May 31, 2012 at 2:43 PM


randylevy wrote:

I've also updated the sample project.  You should be able to download, build, and then use "Edit configuration file" with no error.  Try it out and let me know if you have any issues.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Hi Randy ,

I downloaded the new project and tried out again with no luck I get the same error again  while trying to open the App.config  with Enterprise Library  V5 Configuration. If i continue to open the file  then the entire logging section is lost .

Please let me know if i am missing some reference .

 

May 31, 2012 at 3:16 PM
Edited May 31, 2012 at 3:17 PM

I had assumed you were using the "Edit configuration file" from within Visual Studio 2010.  I would recommend opening the configuration file from within Visual Studio 2010 -- that should just work.

If you are using the standalone configuration tool then you can open the solution, build the solution and then copy the ExtendedPropertyDatabaseListener.dll (from the ExtendedPropertyDatabaseListener\bin\Debug directory) to the directory where the configuration tool is being run from (e.g. C:\Program Files (x86)\Microsoft Enterprise Library 5.0\Bin).  Then the configuration file should open fine once the custom trace listener can be resolved.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

May 31, 2012 at 3:35 PM

Hello Randy,

 

I was  opening the config file using  Edit  Enterprise Library  V5 Configuration.Vs2010 , but was still getting an error

However I copied the  ExtendedPropertyDatabaseListener.dll  to C:\Program Files (x86)\Microsoft Enterprise Library 5.0\Bin  and it now works like a charm.

Thank you again for your help.

 

Varsha