Jishnu A P
Jishnu A P

Reputation: 14390

Create a log everytime When methods in an interface class are called

I want to update a log file(txt) everytime when methods in a an interface class are called? Is there any way to do this other than writing code in every method to create log?

Upvotes: 0

Views: 128

Answers (3)

michaelalm
michaelalm

Reputation: 210

Here's my 30 mins. you'll have to implement the logging code somewhere so you have to create another abstraction for your code. thus an abstract class is needed. i think. this is very quick and dirty.

public interface IService<T>
{
    List<T> GetAll();
    bool Add(T obj);
}

then you'll need the abstract class where you'll need to implement your logging routine

public abstract class Service<T> : IService<T>
{
    private void log()
    {
        /// TODO : do log routine here
    }

    public bool Add(T obj)
    {
        try
        {
            log();
            return AddWithLogging(obj);
        }
        finally
        {
            log();
        }         
    }

    public List<T> GetAll()
    {
        try
        {
            log();
            return GetAllWithLog();
        }
        finally
        {
            log();
        }  
    }

    protected abstract List<T> GetAllWithLog();
    protected abstract bool AddWithLogging(T obj);
}

as for your concrete classes

public class EmployeeService : Service<Employee>
{
    protected override List<Employee> GetAllWithLog()
    {            
        return new List<Employee>() { new Employee() { Id = 0, Name = "test" } };
    }

    protected override bool AddWithLogging(Employee obj)
    {
        /// TODO : do add logic here
        return true;
    }
}

public class CompanyService : Service<Company>
{
    protected override List<Company> GetAllWithLog()
    {
        return new List<Company>() { new Company() { Id = 0, Name = "test" } };
    }

    protected override bool AddWithLogging(Company obj)
    {
        /// TODO : do add logic here
        return true;
    }
}

public class Employee
{
    public int Id {get;set;}
    public string Name { get; set;}
}

public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
}

then on your implementation you can just..

    static void Main(string[] args)
    {
        IService<Employee> employee = new EmployeeService();
        List<Employee> employees = employee.GetAll();

        foreach (var item in employees)
        {
            Console.WriteLine(item.Name);
        }

        IService<Company> company = new CompanyService();
        List<Company> companies = company.GetAll();

        foreach (var item in companies)
        {
            Console.WriteLine(item.Name);
        }

        Console.ReadLine();
    }

hope this helps!

Upvotes: 1

Chandu
Chandu

Reputation: 82943

I think you meant class (instead of interface) Two options I can think of:

Implementing INotifyPropertyChanged which is in lines of writing code in every method

or

to adopt on of the AOP frameworks in the article http://www.codeproject.com/KB/cs/AOP_Frameworks_Rating.aspx if that is not a major leap

Upvotes: 0

MK.
MK.

Reputation: 34597

I think you would have to use Aspect Oriented Programming to achieve that. Read http://www.sharpcrafters.com/aop.net

Upvotes: 0

Related Questions