TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
User Activity Logging in Asp.net MVC Applications
Ananth Ananthakrishnan
Oct 29
2014
Code
15.5
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
Write the below code with in Global.asax.cs file:
using
System;
using
System.Collections.Generic;
using
System.IO;
using
System.Linq;
using
System.Net;
using
System.Net.Http;
using
System.Web;
using
System.Web.Http;
using
System.Web.Mvc;
using
System.Web.Routing;
using
WebTask.Models;
using
System.Threading.Tasks;
namespace
WebTask
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public
class
MvcApplication : System.Web.HttpApplication
{
string
action =
""
;
string
controller =
""
;
string
parameter =
""
;
string
hostName =
""
;
string
ipaddress =
""
;
string
requesturl =
""
;
string
dataparameter = String.Empty;
protected
void
Application_BeginRequest(
object
sender, EventArgs args)
{
Logging();
}
private
async Task Logging()
{
List<Apilogging> objlogging =
new
List<Apilogging>();
ApiEvent objevent =
new
ApiEvent();
HttpContextBase currentContext =
new
HttpContextWrapper(HttpContext.Current);
UrlHelper urlHelper =
new
UrlHelper(HttpContext.Current.Request.RequestContext);
RouteData routeData = urlHelper.RouteCollection.GetRouteData(currentContext);
action = routeData.Values[
"action"
].ToString();
controller = routeData.Values[
"controller"
].ToString();
parameter = routeData.Values[
"id"
].ToString();
hostName = Dns.GetHostName();
ipaddress = Dns.GetHostByName(hostName).AddressList[0].ToString();
HttpContext.Current.Request.InputStream.Position = 0;
using
(StreamReader inputStream =
new
StreamReader(HttpContext.Current.Request.InputStream))
{
dataparameter = inputStream.ReadToEnd();
}
requesturl = Request.Url.AbsoluteUri;
objlogging.Add(
new
Apilogging { controllername = controller, actionname = action, myip = ipaddress, parameters = parameter, dataparameter = dataparameter, requesturl = requesturl });
await objevent.Insertapiaccess(objlogging);
}
protected
void
Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
ApiLogging and ApiEvent class Details:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Data.Entity;
using
WebTask.Models;
using
System.Net.Http;
using
System.Threading;
using
System.Threading.Tasks;
using
System.Net;
using
System.IO;
using
System.Web.Mvc;
using
System.Web.Routing;
using
System.Net.Http.Headers;
namespace
WebTask.Models
{
public
class
ApiEvent : System.Web.HttpApplication
{
RCHEntities2 objapi =
new
RCHEntities2();
public
async Task Insertapiaccess(List<Apilogging> objaccess)
{
try
{
Webapiaccess obj =
new
Webapiaccess();
foreach
(var item
in
objaccess)
{
obj.ControllerName = item.controllername;
obj.ActionName = item.actionname;
obj.Ipaddress = item.myip;
obj.CreatedDate = DateTime.Now;
obj.UrlParameter = item.parameters;
obj.Dataparameter = item.dataparameter;
obj.RequestUrl = item.requesturl;
}
objapi.Webapiaccesses.Add(obj);
await objapi.SaveChangesAsync();
}
catch
(Exception ex)
{
string
errormsg = ex.Message.ToString();
}
}
}
public
class
Apilogging
{
public
string
controllername {
get
;
set
; }
public
string
actionname {
get
;
set
; }
public
string
myip {
get
;
set
; }
public
string
parameters {
get
;
set
; }
public
string
dataparameter {
get
;
set
; }
public
string
requesturl {
get
;
set
; }
}
}
API access is the edmx. It will generate the Web API access class.
Asp.net MVC