I have worked on one application related for booking space and required to store activity of user into table and access from admin side.
For that I have created one database entity and EventLogger
class.
public partial class UserEventLog
{
public int ID { get; set; }
public string UserID { get; set; }
public Enum_SourceType SourceType { get; set; }
public Enum_EventType EventType { get; set; }
public string EventId { get; set; }
public string EventName { get; set; }
public Enum_LogType LogType { get; set; }
public string LogMessage { get; set; }
public string LogDescription { get; set; }
public string IPAddress { get; set; }
public string HostName { get; set; }
public DateTimeOffset EventTriggeredOn { get; set; }
public virtual AspNetUser User { get; set; }
}
related enum
public enum Enum_SourceType
{
User,
Order
}
public enum Enum_LogType
{
Information,
Warning,
Error
}
public enum Enum_EventType
{
Create,
Update,
Delete,
Duplicate,
DownLoad,
Preview,
Search,
AppliedCoupon,
Cancel,
LoggedIn,
Register,
SendMail,
ConfirmEmail,
Account,
View,
SendMessage
}
and I have created EventLogger class for log event from whole application.
public class EventLogger
{
public static IUserEventLogService UserEventLogService
{
get
{
return Core.ContainerManager.GetConfiguredContainer().Resolve<IUserEventLogService>();
}
}
public static IUnitOfWorkAsync UnitOfWork
{
get
{
return Core.ContainerManager.GetConfiguredContainer().Resolve<IUnitOfWorkAsync>();
}
}
public static async Task Log(DateTimeOffset eventTriggeredOn, Enum_EventType eventType, string eventName, Enum_LogType logType, string logMessage,string eventId = "", string logDescription = "")
{
var currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId();
string ipAddress = GetLocalIPAddress();
var userEventLog = new UserEventLog()
{
EventId = eventId,
LogType = logType,
EventName = eventName,
EventTriggeredOn = eventTriggeredOn,
EventType = eventType,
LogMessage = logMessage,
LogDescription = logDescription,
IPAddress = ipAddress,
UserID = currentUserId,
ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added,
SourceType = Enum_SourceType.User
};
UserEventLogService.Insert(userEventLog);
await UnitOfWork.SaveChangesAsync();
}
public static string GetLocalIPAddress()
{
string visitorIPAddress = GetVistorIp(HttpContext.Current.Request.ServerVariables);
return visitorIPAddress;
}
static string GetVistorIp(NameValueCollection headers)
{
if (!string.IsNullOrWhiteSpace(headers["HTTP_X_FORWARDED_FOR"]))
return headers["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrWhiteSpace(headers["HTTP_CF_CONNECTING_IP"]))
return headers["HTTP_CF_CONNECTING_IP"];
return headers["REMOTE_ADDR"];
}
}
Please suggest me if this solution is right or require any modification or any good solution for suggestion for my implementation.