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) {
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".
source share