ExceptionPolicy.HandleException Customization

Apr 8, 2009 at 6:17 AM

Hi all iam using enterprise library 3.1
i defined policies
and iam logging exception using the following line

public void save(Student s)
{

catch

 

(Exception ex){

 

 

ExceptionPolicy.HandleException(ex,AnresExceptionPolicy.PolicyBusiness);
}
}

its logging only the exception
all i need is i want to log studentid from the parameter along with the log
so that i can trace the log ,for which student id i got the exception

in case of enterprise library 2.0
there is a function  which takes 2 parameter
ExceptionPolicy.HandleException(ex, Core.Common.Constants.PolicyGeneral, entry);
but in case of enterprise library 3.1 there is no such function takes 3 parameter please help me with this

Apr 8, 2009 at 6:38 AM
If you just need this for tracing purposes, you can just configure a policy which will log the parameter of the Save method. (The logging will occur regardless whether an exception occured or not)  In your case the parameter s of type Student.  Override the ToString method in your student class to return the studentid property.  This will save you time from writing your own custom call handler.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Apr 8, 2009 at 6:46 AM
can you explain me that with some coding and how to do that..
Please
Apr 8, 2009 at 6:52 AM
Edited Apr 8, 2009 at 6:57 AM
Sure.  What's more involve here is your configuration, not much on the coding side.  Are you familiar with the Policy Injection Application Block/configuring policies?

I should also mention that your class which contains the Save method should inherit from an interface or derives from MarshalByRefObj class to make it interceptable. 


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Apr 8, 2009 at 7:22 AM
Edited Apr 8, 2009 at 7:29 AM
Nope
i have no idea in Policy and Injection Application Blocks

i know data access block and exception policies









Apr 8, 2009 at 7:38 AM
Ok, I'll enumerate first the steps on the code part:
1. Have your class containing the Save method inherit from an interface or derive it from MarshalByRefObj class. 
2. Add reference to PolicyInjection and PolicyInjection.CallHandlers assembly.
3. Locate your instantiation of that class before calling the Save method.  Make it interceptable.  I'll demonstrate an example here.
        public interface IService
        {
             void Save(Student s);
        }
        Service - class implementing IService.
    
        instead of :
        IService service =new Service();
        service.Save(student);
        
        use this:
        IService service = new Service();
        service = PolicyInjection.Wrap<IService>(service); //wrap method
        service.Save(student);


Configuration:
1. Open your configuration file using the EntLib Configuration tool. 
2. Add the Policy Injection Application Block.
3. Add a policy, specify any name.
4. Right click on the Handlers node, select Logging Handler.  (Make sure you've already configured your logging application block) 
5. Add an item in the Categories property of the LoggingHandler which matches a category from your logging application block. 
    Also make sure that the IncludeParameterValues property is set to true.
6. On the Matching Rules node, right click and add Member Name Matching Rule.
7. Add "Save" in the Matches collection property. 

These steps will result to logging of the parameter values of any method with the name "Save".  Make sure to override the ToString() method of your Student class as that will determine what gets logged in your log destination.

You can use other matching rules, please refer to the documentation on how to use each one of them. 


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Apr 9, 2009 at 4:17 AM
please have a look at this code

using

System;

using

System.Collections.Generic;

using

System.Text;

using

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

using

Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers;

using

Microsoft.Practices.EnterpriseLibrary.PolicyInjection;

 

 

namespace

ConsoleApplication1

{

public class Student

 

{

private int id;

private string name;

public int Id

{

get{return id;}

set{id=value;}

}

public string Name

{

get{return name;}

set{name=value;}

}

}

public interface IStudent

 

{

void Save(Student StudentObj);

 

}

public class StudentService:IStudent

 

{

public void Save(Student StudentObj)

{

try

 

{

throw new Exception();

}

catch(Exception ex)

{

ExceptionPolicy.HandleException(ex,"Exception Policy");

}

}

}

class Program

 

.HandleException(ex,

{

static void Main(string[] args)

{

IStudent service=new StudentService();

service=PolicyInjection.Wrap<IStudent>(service);

.Wrap<>(service);

Student Obj=new Student();

Obj.Id =99;

Obj.Name ="TestName";

service.Save(new Student());

 

}

}

}



all i need is to save the Id value of the student object along with the exception log in database.i want to do the log only when i got the exception .please help me with that.....
Apr 13, 2009 at 9:07 AM
Hi,

Code seems fine... I only have some corrections, first is the service.Save(new Student()); here you should be passing the Obj  instance of Student. also you never override the ToString method for the Student Class as Sarah suggested. For logging to database use the Database Trace Listener.

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