Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 967693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:21:37+00:00 2026-05-16T02:21:37+00:00

I have a web app that contains an activity log for actions the users

  • 0

I have a web app that contains an activity log for actions the users take. There are multiple types of activities that can be logged.

Here’s 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 ...
  }
}

And then go on to create a new class for every single type of activity to log. Each activity has a .Write() method, with a unique signature for each one (as shown in the code example above)

And then in my web app (asp.net mvc based) I use them like this:

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 ...
  }
}

Is this a bad idea? Is there a better way? Should these be a bunch of methods jammed into one class?

I started doing this because of an answer to a different question I had on SO.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:21:37+00:00Added an answer on May 16, 2026 at 2:21 am

    This is my suggestion.

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

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

    Now for all other simple activity log, create a default activity that accept a function lambda.

    Example, in this code, I assume that each activity builds a string and return 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 your logger class like this:

    Logger.Write(new NewAccountActivity(currentUserName));
    

    or if you need to log something else that is simple:

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

    This last call will create a new instance of ActivityEntry that will log “Hello World”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web app that reads data from a SQL DB that contains
I have a legacy intranet web app that was written for IE7 and contains
In my ASP.NET MVC 2 C# web app I have a repository that contains
I have a web app that contains a slide show with about 10 large
I have an iOS app that contains a lot of local web content. Some
We have a web app with a .jar in our WEB-INF/lib that contains some
I have a web app where users can upload files. The files are physically
We have an iOS app that publishes certain activity to Facebook that contains links
I have a Web app that contains a large grid of results. I'd like
We have a web app (let's call it widget app) that contains data I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.