CustomTraceListener gives exception

Topics: Logging Application Block
Feb 24, 2010 at 2:39 PM
Edited Feb 24, 2010 at 2:46 PM

I am using EntLib 4.1 - Logging Application Block and creating a CustomTraceListener in Logging project. The dll of this project is added as a AddIn in some other application say "Application B".  Application B has its own ApplicationB.config file. Application B has a folder named as setting which contains this ApplicationB.config file, ApplicationB.exe and a folder named AddIn where additional addins will reside. So I have added my config setting for logging in ApplicationB.config file and logging dll in Addin >> logging folder. When I remove the Custom Trace Listener related things from this ApplicationB.config file, it works fine but with this Custom trace Listener I am getting following exception,

{"The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: Custom TraceListener type Logging.EventLogTraceListener for name Custom Trace Listener does not have the required constructor .ctor(string). (Strategy type ConfiguredObjectStrategy, index 2)"}

My config file is as follows,

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Logging.EventLogTraceListener, Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
        name="Custom Trace Listener" initializeData="" formatter="Text Formatter" />
      <add source="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" />
    </listeners>
    <formatters>
      <add template="{keyvalue(FileDetails)}{eventid}:{timestamp(yyyy-MM-dd hh.mm.ss.fff)}:{keyvalue(ElapsedTime)}:{severity}:{category}:{processId}:{win32ThreadId}:{machine}:{message}:{keyvalue(Exception)}"
        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="General">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
          <add name="Custom 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">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

Custom trace listener is as following,

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.Diagnostics;


namespace Logging
{
    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class EventLogTraceListener : CustomTraceListener
    {
        EventLogTraceListener()
            : base()
        {
        }

        TcEventLogTraceListener(string str)
            : base()
        {
        }

        public override void TraceData(TraceEventCache eventCache, string source, 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)
        {
            Console.Write(message);
        }

        public override void WriteLine(string message)
        {
            // Delimit each message
            Console.WriteLine((string)this.Attributes["delimiter"]);

            // Write formatted message
            Console.WriteLine(message);
        }

    }
}

If I try same stand alone project without making it dll as a AddIn of Application B, then it works fine

 

 

Feb 25, 2010 at 12:33 AM

Regarding your last statement, it also includes the fact that you don't have the Custom trace listener in your config file, right?  So simplifying it, it works if you don't use the custom trace listener, is this correct?   

It is using your constructor with the most number of parameters which is the one which takes a string parameter.  Which one do you really need to use?  If you want to use the one with the parameter, you can put a value for it in the "initializeData" attribute of your custom trace listener in the config file. 

 

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

Feb 25, 2010 at 7:57 AM

Thanks but now I am getting following exception,

 {"The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: The type 'Logging.EventLogTraceListener,Logging,Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' cannot be resolved. Please verify the spelling is correct or that the full type name is provided. (Strategy type ConfiguredObjectStrategy, index 2)"}    System.Exception {Microsoft.Practices.ObjectBuilder2.BuildFailedException}

Please reply if you know why I am getting this exception.

 

 

 

Feb 25, 2010 at 8:06 AM

What exact code that is causing this exception?

 

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

Feb 25, 2010 at 8:15 AM
I am using following Custom Trace Listener and config file for which It gives exception as,
 
{"The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: The type 'Logging.EventLogTraceListener,Logging,Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' cannot be resolved. Please verify the spelling is correct or that the full type name is provided. (Strategy type ConfiguredObjectStrategy, index 2)"} System.Exception {Microsoft.Practices.ObjectBuilder2.BuildFailedException}
 
 
Custom Trace Listener is as follows

 

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.Diagnostics;

 

namespace

Logging

{

[

ConfigurationElementType(typeof(CustomTraceListenerData))]

public class EventLogTraceListener : CustomTraceListener

{

EventLogTraceListener()

:

base()

{

}

EventLogTraceListener(

string str)

:

base()

{

}

public override void TraceData(TraceEventCache eventCache, string source, 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)

{

Console.Write(message);

}

public override void WriteLine(string message)

{

// Delimit each message

Console.WriteLine((string)this.Attributes["delimiter"]);

// Write formatted message

Console.WriteLine(message);

}

}

}

 

My config file is as following,

<

loggingConfiguration name="Logging Application Block" tracingEnabled="true"

defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">

<

listeners>

<

add delimiter="-----------------------------" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

traceOutputOptions="None" filter="All" type="Logging.EventLogTraceListener,Logging,Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

name="Custom Trace Listener" initializeData="" formatter="Text Formatter" />

<

add source="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" />

</

listeners>

<

formatters>

<

add template="{keyvalue(FileDetails)}{eventid}:{timestamp(yyyy-MM-dd hh.mm.ss.fff)}:{keyvalue(ElapsedTime)}:{severity}:{category}:{processId}:{win32ThreadId}:{machine}:{message}:{keyvalue(Exception)}"

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="General">

<

listeners>

<

add name="Custom Trace Listener" />

<

add name="Formatted EventLog TraceListener" />

</

listeners>

</

add>

</

categorySources>

<

specialSources>

<

allEvents switchValue="All" name="All Events" />

<

notProcessed switchValue="All" name="Unprocessed Category" />

<

errors switchValue="All" name="Logging Errors & Warnings">

<

listeners>

<

add name="Custom Trace Listener" />

<

add name="Formatted EventLog TraceListener" />

</

listeners>

</

errors>

</

specialSources>

</

loggingConfiguration>
I am calling Loggerr from following code,
 

LogEntry

entry = new LogEntry();

entry.Message = t_message.ToString();

entry.TimeStamp =

DateTime.Now;

------

Logger

.Write(entry);    // Throws exception at this point

 



On Thu, Feb 25, 2010 at 2:36 PM, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

What exact code that is causing this exception?

 

Sarah Urmeneta
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




--
Thanks & Regards


Sanjay Dinde

Sr. Software Engineer,
Geometric Software Solutions Co. Ltd,
Pune.
Feb 25, 2010 at 8:32 AM

First, your initializeData attribute still has no value in the config you posted.  You need to specify a value for this if you want to use the constructor with string parameter.  If not, just remove the constructor. Second, add the public modifier in your constructors.  By the way, did you manually edited your config file?  If yes, please use the config tool, so as to avoid typographical errors. 

 

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

Feb 25, 2010 at 9:16 AM
I removed parameterised constructor, made default constructor as public and created config file using config tool. Still I get same problem. My folder structure is as shown in attached file. Does it has any impact?
 
- Folder ApplicationA contains another folder named Addins which again contains another folder Logging where my dll is stored. Source code is at some other location.
- Folder ApplicationA also contains ApplicationA.exe and ApplicationA.exe.config file.
- My logging dll is used as addin in ApplicationA
- Logging related config settings are added in ApplicationA.exe.config file

 
On Thu, Feb 25, 2010 at 3:02 PM, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

First, your initializeData attribute still has no value in the config you posted.  You need to specify a value for this if you want to use the constructor with string parameter.  If not, just remove the constructor. Second, add the public modifier in your constructors.  By the way, did you manually edited your config file?  If yes, please use the config tool, so as to avoid typographical errors. 

 

Sarah Urmeneta
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




--
Thanks & Regards


Sanjay Dinde

Sr. Software Engineer,
Geometric Software Solutions Co. Ltd,
Pune.
Feb 25, 2010 at 9:25 AM

Oh, seems like you did not referenced the assembly of your custom trace listener.  If I'm getting it right, you only add it in a folder named "AddIn", is this correct?  If it is, then you must reference it, adding it to your project's folder doesn't really reference it.

 

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

Feb 25, 2010 at 9:30 AM
You are right but I don't have source code of ApplicationA. Do you know how I can add reference of this dll to the base application. In my case reference of logging.dll which contains custom trace listener to ApplicationA.

On Thu, Feb 25, 2010 at 3:55 PM, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

Oh, seems like you did not referenced the assembly of your custom trace listener.  If I'm getting it right, you only add it in a folder named "AddIn", is this correct?  If it is, then you must reference it, adding it to your project's folder doesn't really reference it.

 

Sarah Urmeneta
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




--
Thanks & Regards


Sanjay Dinde

Sr. Software Engineer,
Geometric Software Solutions Co. Ltd,
Pune.
Feb 25, 2010 at 9:33 AM

You said you have the Logging.dll, you just need to right click on the Reference folder of your project, Select Add Reference and select that .dll file.

 

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

Feb 25, 2010 at 10:00 AM
I have logging.dll which contains custom trace listener. I am adding AddIn in ApplicationA so I don't have source code of this ApplicationA. I just copied logging.dll from ApplicationA>>Addins>>logging folder and pasted to ApplicationA folder and now I am able to avoid this exception but while debugging It's not breaking at TraceData method of CustomTraceListener. Do you have any idea about this?

On Thu, Feb 25, 2010 at 4:03 PM, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

You said you have the Logging.dll, you just need to right click on the Reference folder of your project, Select Add Reference and select that .dll file.

 

Sarah Urmeneta
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




--
Thanks & Regards


Sanjay Dinde

Sr. Software Engineer,
Geometric Software Solutions Co. Ltd,
Pune.
Feb 25, 2010 at 10:29 AM
My CustomTraceListener is as shown below,

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.Diagnostics;


namespace Logging
{
    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class EventLogTraceListener : CustomTraceListener
    {
        public EventLogTraceListener()
            : base()
        {
        }

      
        public override void Write(string message)
        {
            Console.Write(message);
        }

        public override void WriteLine(string message)
        {
            // Delimit each message
            Console.WriteLine((string)this.Attributes["delimiter"]);

            // Write formatted message
            Console.WriteLine(message);
        }

        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType severity, int id, params object[]  
                                                    data)
        {
            if ((this.Filter == null) || this.Filter.ShouldTrace(eventCache, source, severity, id, null, null, null, data))
            {
                Debug.WriteLine("I am in TraceData");
            }
        }

        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType severity, int id, object data)
        {
            if ((this.Filter == null) || this.Filter.ShouldTrace(eventCache, source, severity, id, null, null, data, null))
            {
                Debug.WriteLine("I am in TraceData");
            }
        }

    }
}


On Thu, Feb 25, 2010 at 4:30 PM, sanjay dinde <dinde.sanjay@gmail.com> wrote:
I have logging.dll which contains custom trace listener. I am adding AddIn in ApplicationA so I don't have source code of this ApplicationA. I just copied logging.dll from ApplicationA>>Addins>>logging folder and pasted to ApplicationA folder and now I am able to avoid this exception but while debugging It's not breaking at TraceData method of CustomTraceListener. Do you have any idea about this?


On Thu, Feb 25, 2010 at 4:03 PM, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

You said you have the Logging.dll, you just need to right click on the Reference folder of your project, Select Add Reference and select that .dll file.

 

Sarah Urmeneta
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




--
Thanks & Regards


Sanjay Dinde

Sr. Software Engineer,
Geometric Software Solutions Co. Ltd,
Pune.



--
Thanks & Regards


Sanjay Dinde

Sr. Software Engineer,
Geometric Software Solutions Co. Ltd,
Pune.
Feb 26, 2010 at 5:44 AM

Hi,

So ApplicationA does not really reference the Logging.dll. In your config, it must be configured to use your custom trace listener that is inside the Logging.dll, which is i guess you've already done this one. Now, while running the ApplicationA, and your breakpoints are all set in the visual studio (source code of the Logging.dll), please do a Debug > Attach to Process, select the process of the ApplicationA then attach. Please see if it will break on the TraceDate method.

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

Feb 26, 2010 at 6:42 AM
Hi,
     I changed my dll from logging.dll to MyLogging.dll assuming that the name of two dll's conflict and now it beaks in TraceData method in custom Trace Listener.
As I said in my previous mails, I need to physically copy my dll from ApplicationA>>AddIns>>Logging folder and paste in ApplicationA folder where ApplicationA.exe and ApplicationA.exe.config file reside. Is it possible to set the path of logging dll in config file? i.e. path AddIns>>Logging>>MyLogging in ApplicationA.exe.config file? so that it will not require to copy and paste the dll.

On Fri, Feb 26, 2010 at 12:14 PM, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

Hi,

So ApplicationA does not really reference the Logging.dll. In your config, it must be configured to use your custom trace listener that is inside the Logging.dll, which is i guess you've already done this one. Now, while running the ApplicationA, and your breakpoints are all set in the visual studio (source code of the Logging.dll), please do a Debug > Attach to Process, select the process of the ApplicationA then attach. Please see if it will break on the TraceDate method.

Valiant Dudan


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




--
Thanks & Regards


Sanjay Dinde

Sr. Software Engineer,
Geometric Software Solutions Co. Ltd,
Pune.
Mar 1, 2010 at 4:08 AM

I still don't get it why you don't just reference the logging/MyLogging.dll in your project.  You don't need to have the source code.  Referencing it will automatically copy that dll in your output folder.  What is the AddIns folder anyway?  Is that a folder you created? Do you set it as your project's output folder? 

 

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

Mar 2, 2010 at 1:07 PM
ApplicationA is a base application in which we are adding our dll's. So I have only ApplicationA no any source code for this application. To add any Addin in this application, I have to create new dll and put in AddIns folder so once I run ApplicationA.exe from C:\Program Files\ApplicationA folder, it loads all the dll's from this Addin folder.
    Now my problem is I want to keep my dll i.e. MyLogging.dll and its config file in Addins>>Logging folder whereas ApplicationA.exe and ApplicationA.exe.config files reside in ApplicationA  folder as shown in attached diagram.
Please let me know If you have any idea.

On Mon, Mar 1, 2010 at 10:38 AM, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

I still don't get it why you don't just reference the logging/MyLogging.dll in your project.  You don't need to have the source code.  Referencing it will automatically copy that dll in your output folder.  What is the AddIns folder anyway?  Is that a folder you created? Do you set it as your project's output folder? 

 

Sarah Urmeneta


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




--
Thanks & Regards


Sanjay Dinde

Sr. Software Engineer,
Geometric Software Solutions Co. Ltd,
Pune.
Mar 3, 2010 at 1:03 AM

You could add a post build event to your project which will automatically copy that assembly to the desired location - http://msdn.microsoft.com/en-us/library/42x5kfw4(VS.80).aspx.

 

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