Ent library 4.1 - DB Trace listener/Log blk - not working for me

Topics: Logging Application Block
Apr 7, 2010 at 1:44 AM

Hi,

I am trying to configure the Logging Application block with DB Trace listener. It is not throwing error at the same time doesn't write in DB.

 <add databaseInstanceName="LocalSqlServer" writeLogStoredProcName="dbo.WriteLog"
        addCategoryStoredProcName="dbo.AddCategory" formatter="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Database Trace Listener" />

I have given my db connection string and also created a new storedproc WriteLog.

Kindly advice if i missed anything.

 

 

Apr 7, 2010 at 2:10 AM

Hi vijaykisha,

Are you using the default Logging Database for EntLib here?Also, could you post your whole configuration here? Thanks.

~ginkapitan

Apr 7, 2010 at 4:05 AM

Hi,

 

Thanks for your time. I have not used default logging database. I want it to be in my database. How to configure ? Also let me where to look for default logging db. Should I install something ?

 

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add databaseInstanceName="LocalSqlServer" writeLogStoredProcName="dbo.WriteLog"
        addCategoryStoredProcName="dbo.AddCategory" formatter="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Database Trace Listener" />
      <add source="Enterprise Library Logging" formatter="Text Formatter"
        log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Formatted EventLog TraceListener" />
      <add fileName="C:\Enterprise.log" footer="----------------------------------------"
        formatter="Text Formatter" header="----------------------------------------"
        rollFileExistsBehavior="Overwrite" rollInterval="None" rollSizeKB="0"
        timeStampPattern="yyyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Rolling Flat File Trace Listener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="DBCategory">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="EventCategory">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </add>
      <add switchValue="All" name="FlatFileRollCategory">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings" />
    </specialSources>
  </loggingConfiguration>

Apr 7, 2010 at 6:44 AM
Edited Apr 7, 2010 at 6:45 AM

The SQL script should be in the location where you have installed the EntLib Source Code usually in C:\EntLib41Src\Blocks\Logging\Src\DatabaseTraceListener\Scripts\LoggingDatabase.sql.

If in case you also cannot find the EntLib Source Code installer it can be found in the EntLib installation folder which is usually in C:\Program Files\Microsoft Enterprise Library 4.1 - October 2008\src.

And since you want to do logging in your custom database try viewing this thread http://entlib.codeplex.com/Thread/View.aspx?ThreadId=83105 for additional information how you can achieve this.

HTH,

~ginkapitan

Apr 7, 2010 at 10:46 AM

Hi,

 

I am trying to create a custom DB Listener

following is te code used

{
    [Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationElementType(typeof(Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData))]
    public class MyDbTraceListener : Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.CustomTraceListener
    {
        public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data)
        {
            if (data is LogEntry && this.Formatter != null)
            {
                this.WriteLine(this.Formatter.Format(data as LogEntry));
            }
            else
            {
                this.WriteLine(data.ToString());
            }


        }
        public override void Write(string message)
        {
            System.Diagnostics.EventLog.WriteEntry("Application", "sdafsadfSAD");
        }

        public override void WriteLine(string message)
        {
            System.Diagnostics.EventLog.WriteEntry("Application", "sdafsadfSAD");
        }
    }
}

I am getting the following error while loading the assembly for custom trace listener.

---------------------------
No Types Found In Assembly.
---------------------------
There were no types found in the assembly 'CustomTraceListener' that implement or inherit from the base type 'Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.CustomTraceListener'.
---------------------------
OK  
---------------------------

Apr 7, 2010 at 11:36 AM

Hi,

I tried to just copy and paste your custom trace listener and it works well in my end.

Try closing your solution then restart the Visual Studio then open up your solution again and try to get your trace listener again from your assembly.

Let me know if this doesn't work.

~ginkapitan

Apr 12, 2010 at 12:16 PM

Hi,

 

Thanks for all your support. It worked.

1. I want to filter based on filter value set on the customlistenerdata. Please let me know how to do it ?

2. How to create a new source in Evenlog on my application name and log the message there.

3. Also In dbtracelistener instead of specifying the parameters in the code i need it from config file like the one in Log4Net ?

for e.g

<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <!--Connection String-->
      <connectionString value="data source=.;initial catalog=LogRecorder;integrated security=true;persist security info=True;" />
      <commandText value="WriteLogDetails" />
      <commandType value="StoredProcedure" />
      <!-- <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" /> -->
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>

Kindly suggest.

 

Apr 13, 2010 at 2:30 AM

Hi,

See responses below for your questions.

1. I want to filter based on filter value set on the customlistenerdata. Please let me know how to do it ?

- I think what you need is to use a CustomFilter. See if using it would fit to you, check this blog http://geekswithblogs.net/.NETonMyMind/archive/2006/05/15/78201.aspx

2. How to create a new source in Evenlog on my application name and log the message there.

- You can create new source by System.Diagnostics.EventLog.CreateEventSource(myEventSource, myEventLogName), though you may also need to have more information about this. See this FAQ item for some details http://entlib.codeplex.com/wikipage?title=Why%20are%20messages%20not%20logged%3f&referringTitle=EntLib%20FAQ

3. Also In dbtracelistener instead of specifying the parameters in the code i need it from config file like the one in Log4Net ?

- For what I know there is no explicit way same as the Log4net config you pasted above to add parameters in the config. But you can try to utilize the Attributes Collection property if this will work for you. Only the one limitation about this is that the name of the key cannot begin with the character '@'.

<listeners>

<add Database="CustomLogging" WriteStoredProc="CustomWriteToCustomDB" Param1="Value1" listenerDataType="......

</listeners>

 

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 13, 2010 at 3:59 AM

Hi Gino,

1.  I am able to use the filter attribute of eventlog and rolling file.It's not working for customtracelistenerdata. How to utilize the filter attrib of the below in the custom db trace listener code.

<add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="Error" type="CustomDbListener.MyDbTraceListener, CustomDbListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=63fe6f2ee84f0620" name="CustomDBTrace Listener" initializeData="" />

2nd  and 3rd : I will try and update the same.

Apr 13, 2010 at 6:03 AM

You'll need to add some codes in your CustomTraceListener to utilize the filter property.

In your TraceData Method add the following if stament

if (this.Filter == null || this.Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
{
      //do TraceData method code 
}

by having this you'll get the same behavior with the Event Log and Flat File Trace Listeners.

HTH,

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Apr 13, 2010 at 7:51 AM

Thanks .

It worked

From: AvanadeSupport [mailto:notifications@codeplex.com]
Sent: 13 April 2010 11:34
To: Vijaykishan Shyam sundar (WI01 - Solutions)
Subject: Re: Ent library 4.1 - DB Trace listener/Log blk - not working for me [entlib:208513]

From: AvanadeSupport

You'll need to add some codes in your CustomTraceListener to utilize the filter property.

In your TraceData Method add the following if stament

if (this.Filter == null || this.Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
{
      //do TraceData method code 
}

by having this you'll get the same behavior with the Event Log and Flat File Trace Listeners.

HTH,

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com

Please do not print this email unless it is absolutely necessary.

The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments.

WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.

www.wipro.com