0

I'm looking for a better logging approach for a large MVC project. I tried to look around towards other's suggestions (like Log4Net, Nlog, ELMAH, EntityFramework's Self tracking & few others), but could not find reasonable answer. By term logging i don't simply mean logging few requests or exceptions etc. But log for every little change in properties. That's the reason I'm looking for some perfect (generic) approach, which can be used for several applications. I need to roughly log as:

public class someModelPerson
{
    public string Name { get; set; }
    public string Age { get; set; }
}

If through some action, the current object of someModelPerson is updated, I need to log at the end of the method as:

Property Name changed from 'John' to 'Don' by user 'xyz' on 12/12/12

I worked with EntityFramework selftracking and implemented something similar and generic (using templates etc) but it's no more recommended by Microsoft. I'm okay if the solution requires a little customization accordingly.

plus do suggest the better DB schema for storing log of single model modifications. (like update_person method may change name, age, height of a person. How should it be logged at DB level? single entry? separate table for changeset?)

Zeeshan
  • 2,884
  • 3
  • 28
  • 47

2 Answers2

1

you can use Postsharp as stated in website :-

PostSharp offers a solution to all of these problems. The logging pattern library allows you to configure where logging should be performed and the pattern library takes over the task of keeping your log entries in sync as you add, remove and refactor your codebase. Let's take a look at how you can add trace logging for the start and completion of method calls.

For more information take a look here :-

http://www.postsharp.net/diagnostics/net-logging

Neel
  • 11,625
  • 3
  • 43
  • 61
  • I tried postsharp's logging. it's cool and simple. I overrided it's logging methods, but i'm not getting how can i achieve *Property Name changed from 'John' to 'Don' by user 'xyz' on 12/12/12* this part using postsharp logging? since I could not properly get changes made to object, in overriden method. Can you help/suggest? – Zeeshan May 21 '14 at 08:26
  • have a look here http://www.postsharp.net/aspects/examples/inotifypropertychanged.. @Zeeshan – Neel May 21 '14 at 08:29
  • Will i have to override *OnPropertyChange* method? (to make log, etc.) – Zeeshan May 21 '14 at 08:32
  • I havnt tried that part but I guess that should work – Neel May 21 '14 at 08:46
-1
public class LoggingFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.HttpContext.Trace.Write("(Logging Filter)Action Executing: " +
            filterContext.ActionDescriptor.ActionName);

        base.OnActionExecuting(filterContext);
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.Exception != null)
            filterContext.HttpContext.Trace.Write("(Logging Filter)Exception thrown");

        base.OnActionExecuted(filterContext);
    }
}
Larry
  • 2,172
  • 2
  • 14
  • 20
  • How is an `ActionFilter` going to help in the OP's particular use case? Seems you didn't read the entire question. – Chris Pratt May 20 '14 at 13:30