spy activity logging
| | | |

User’s Activity Logging in Asp.net Core MVC Application | C#

In this Article, I’m going to Implement User’s Activity Logging in Asp.net Core MVC Application. If an application is managed by more than one users then you must be aware that who is performing which activity.

users activity logging

Why Activity Logging

Before start Implementation, you must have a clear idea for what kind of applications Activity Logging is necessary.

Let say you have an Admin Panel for managing your App or Website backend & you have more than one Administrator to manage your Admin Panel. If an Admin Create, Delete or Update records then you must be aware of his the activities in your Application.

Here’s the Admin Panel Tutorial you might be interested => Creating Admin Panel in Asp.net Core MVC – Step by Step Tutorial

Example Activities to Log

  1. Login, Logout
  2. Create, Delete or Update a Record
  3. Create or remove a User
  4. Update Profile Info
  5. Change Password
  6. etc

Now, I’ll explain the several ways to Log the Users Activities.

Nuget Libraries with Action filters

There are many Libraries available to Log Users Activities as well as Error. Some Libraries also facilitates us to generate Log files automatically. Here are some of the most commonly used libraries Log4NetElmah. Usually, these libraries used with Action Filters to Log each action of User. Sometimes the purpose of this type of Logging is to judge users experience. For Logging Users Activity I always prefer custom Logging where Its necessary to Log.

Custom Implementation

When the first time I looked at some of the most common logging libraries, and they seemed like more work to set up than to write on my own. So I decided to write my own code to log the Users Activities in my Application.

What to Save as Log 

  1. User Email/Username/Id
  2. Full Name
  3. Ip Address of Client System
  4. Current Time
  5. JavaScript Navigator userAgent Property
  6. Log Type(e.g Login, Logout, Edit Profile Info, Change Password)

* Never save User Password in Logs

Implementation

At the time of successful login store User Id, Email, Full name, IP Address & the JavaScript Navigator userAgent Property using Session or any other temporary Data store Technique.

How To Get JavaScript Navigator userAgent Property

This is a very simple way to get the Client Information using JavaScript in a Web Applications.

With a single Statement, like this navigator.userAgent

We’ll get something like this

User-agent header sent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36

How to Get Client IP Address using C#

Here’s the Code to Get Client’s IP Address

public static string GetLocalIPAddress()
{
    var host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (var ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            return ip.ToString();
        }
    }
    throw new Exception("No network adapters with an IPv4 address in the system!");
}

You may also Get & store the Mac Address of the user.

& Don’t forget to store Current Time with each Log Record.

Conclusion

Applications like Admin Panel must log each Activity of the user because we usually have more than one admin for our application. I’ll recommend saving these activities in Database but for Error Logging I’ll recommend Error Files because sometimes If your application is not properly handling exceptions It can generate logs in a huge number that may affect your Database performance. For the Critical Actions, e.g. an admin is deleting another user, you must send an Email to the super admin Immediately. 

Thank you for reading, share with the community If you find this helpful or comment below If you have any Question.

Creating Interactive Dashboards

Role Based Authorization

Similar Posts

One Comment

  1. Impressed the way you people are working and helping other people to get their life objectives .
    Allah has given you BIG heart to serve the human in needs
    in this time of selfishness no one care about other .
    you people are extremely doing well and i request you people to continue to this path .Allah will reward you more than your expectation .

Comments are closed.