[LogCallHandler] and Windows.Forms.Form

Topics: Caching Application Block , Policy Injection Application Block
Nov 8, 2010 at 9:00 AM

Hi,

I'm using the Enterprise Library 3.1.

To add log to my application, I use injection with [LogCallHandler] :

[LogCallHandler]
public class Class1 : MarshalByRefObject    
{

...
}

In my class, it's working great. But is it possible to use the same mechanism with Form ? I try to add  :

[LogCallHandler(LogBeforeCall = false)] 

public partial class Form1 : Form
{

And I use this instruction to create the instance :

Form1

frm = PolicyInjection.Create<Form1>();

But I get no log... So is it possible to use this mechanism with Form ?

Thank you very much for your help.

Seb.

Nov 8, 2010 at 9:03 AM

It is but you need to inherit Form1 from an interface of from the MarshalByRefObj class.

 

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

Nov 8, 2010 at 9:07 AM

Thank you for the quick response !

But my Form "Form1" inherits from "Windows.Forms.Form"

How can I inherits from MarshalByRefObj in the same time ? How writing instruction ?

Thank you

Seb.

Nov 8, 2010 at 9:16 AM

Oh, sorry about forgetting you can't inherit from 2 classes at the same time.  I was stating the general rule on how to make your class interceptable.  Your only choice here would be to have Form1 inherit from an interface.

 

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

Nov 8, 2010 at 9:28 AM

No problem! Thank you again for your help, I'll try with an interface !

Seb.

Nov 8, 2010 at 9:35 AM

This does not work with an interface, maybe I do it wrong if you have an example, let me know!
Thank you

Nov 8, 2010 at 10:13 AM

I'm guessing you used PolicyInjection.Create<MyForm>()?  If yes, you should replace it to specify the interface as well

IForm form = PolicyInjection.Create<Form1, IForm>();

 

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

Nov 9, 2010 at 10:40 AM

Hi,

Thank you, but it doesn't work. Mayby it's not possible ?

Seb.

Nov 10, 2010 at 12:22 AM
Edited Nov 10, 2010 at 12:24 AM

I have tried it and it works.  Could you post your actual code and the method you're calling? Check as well if you have a DefaultCategory set for the loggingConfiguration section in your configuration file

 

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

Nov 10, 2010 at 8:46 AM

You can download a test project here :

http://cid-7b66552848456ffd.office.live.com/self.aspx/Public/testLogger.zip

 

This is what I do...

Thank you very much for your help !

Seb.

Nov 11, 2010 at 3:48 AM

Ok, it turns out the use of interface is unnecessary.  I just found out that the Form class eventually inherits from MarshalByRefObj, sorry, my mistake.  If you try it,  here are the causes why it won't work based on the test project you posted:

1. If you passed a policy-enabled instance of Form1 in Application.Run(), methods called inside the same instance of Form1 won't get intercepted.  You need to wrap the existing instance and call the method against the new instance.  Take note that methods must be public.  In your case, you can add a public method in Form1, change the logic in your button1_Click to wrap the current instance of Form1 and call the public method on that new instance:

private void button1_Click(object sender, EventArgs e)
{
            Form1 form = PolicyInjection.Wrap<Form1>(this);
            form.Log("Test");
}

public void Log(string message)
{
            classTest.Message(message);
}

This will then intercept the Log method with the LogCallHandler. 

 

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

Nov 15, 2010 at 8:54 AM

Hi,

Ok it is a good method. Thank you very much for your help !!!

Seb.