Am I starting a bad practice C # path by creating separate classes for the activity log?

I have a web application containing an activity log for user actions. There are several types of actions that you can register.

Here is an example:

public static class NewAccountActivity() { public static Write(string username) { //... do stuff to enter a new account activity into the database ... } } public static class NewPostActivity() { public static Write(string username, long postId, string postTitle) { //... do stuff to enter a new post activity entry into the database ... } } 

Then go on to create a new class for each activity to register. Each action has a .Write () method with a unique signature for each of them (as shown in the code example above)

And then in my web application (based on asp.net mvc) I use them as follows:

 public ActionResult NewAccount(Account account) { if (Model.IsValid(account)) { //... do new account stuff ... NewAccountActivity.Write(account.UserName); //... redirect to action, or show view ... } } public ActionResult NewPost(Post post) { if (Model.IsValid(post)) { //... do new post stuff ... NewPostActivity.Write(post.UserName, post.postId, post.Title); //... redirect to action, or show view ... } } 

It is a bad idea? Is there a better way? Should it be a bunch of methods clamped into one class?

I started doing this due to the answer to another question that I had on SO .

+4
source share
2 answers

This is my suggestion.

First of all, no static class should represent activity. The Logger class can be static or accessible through a DI container .

Create an interface for the action, namely IActivity , which has one Write or Log method. For each activity where processing is more than one line of code, create a class that implements the IActivity interface.

Now for all the other simple activity logs, create a default operation that accepts the lambda function .

Example: in this code, I assume that each action builds a string and returns it through the Log function:

 public class NewAccountActivity : IActivity { private string userName; public NewAccountActivity(string userName) { this.userName = userName; } public string Log() { return this.UserName; } } public class ActivityEntry : IActivity { private Func<string> action; public ActivityEntry(Func<string> action) { this.action = action; } public string Log() { return this.action(); } } 

Now in your static Logger class, create two functions:

 public static class Logger { public static void Write(IActivity activity) { // Ask activity for its data and write it down to a log file WriteToFile(activity.Log()); } public static void Write(Func<string> action) { Write(new ActivityEntry(action)); } } 

Then in your code, call the registrar class as follows:

 Logger.Write(new NewAccountActivity(currentUserName)); 

or if you need to register something else, which is simple:

 Logger.Write(() => "Hello world"); 

This last call will create a new ActivityEntry instance that will record "Hello World".

+4
source

What about the logger class, which takes an enumeration as an argument, so that it knows what type of β€œactivity” it is registering and how to deal with it?

+2
source

All Articles