我打算根据ActionFilterAttribute
创建过滤器以记录用户活动:
public class LogAttribute : ActionFilterAttribute
{
public ActionType Type { get; set; }
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
// logic for log
}
}
我想在控制器中对Actions使用此属性。我想记录一些用户在我的视图中设置的值X,Y,Z
以及一些与操作相关的信息。在这种情况下,我必须将这些X,Y,Z作为参数发送到我的操作,然后在属性代码中从ResultExecutedContext获取这些值。但是这种方法让我感到困惑,因为我的每个动作都会有冗余参数。
有没有很好的方法可以将值从View共享到C#代码?
答案 0 :(得分:2)
我可以通过以下方式实现这一目标
创建一个表格(名称 - Portal_Logger
和D_EMP_MASTER
),如下所示
<强> Portal_Logger 强>
<强> D_EMP_MASTER 强>
接下来,创建一个过滤器,用于跟踪用户的活动,如下所示
<强> LoggerActionFilter.cs 强>
using System;
using System.Web.Mvc;
using System.Net;
using System.Linq;
using System.Net.Sockets;
using MyMvcApplication.Models.Entity;
using MyMvcApplication.Utilities;
namespace MyMvcApplication.Filters
{
/// <summary>
///
/// </summary>
public class LoggerActionFilter : ActionFilterAttribute
{
private static TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Stores the Request in an Accessible object
var request = filterContext.HttpContext.Request;
var visitCount = Convert.ToInt64(filterContext.HttpContext.Session["LoggerActivityTracking"].ToString());
// Generate an audit
Portal_Logger aLogger = new Portal_Logger()
{
// Your Logger Identifier
LoggerId = Guid.NewGuid(),
//Logged On User Id
LogedUserId = Convert.ToInt32(filterContext.HttpContext.Session["LogedUserID"]),
// The IP Address of the Request
IPAddress = Convert.ToString(ipHostInfo.AddressList.FirstOrDefault(address => address.AddressFamily == AddressFamily.InterNetwork)),
//Get the Web Page name(from the URL that was accessed)
AreaAccessed = UserActivityUtility.GetWebPageName(request.RawUrl == "/"? "/Home/UserLandingPage" : request.RawUrl),
// Creates our Timestamp
Timestamp = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE),
VisitorSessionId = visitCount
};
// Stores the Logger in the Database
using (IOTAssetEntities context = new IOTAssetEntities())
{
if (aLogger.LogedUserId != null)
{
aLogger.LogedUserEmpId = context.D_EMP_MASTER
.Where(x => x.User_Id == aLogger.LogedUserId)
.Select(x => x.Emp_Id).FirstOrDefault();
aLogger.LogedUserEmpName = context.D_EMP_MASTER
.Where(x => x.User_Id == aLogger.LogedUserId)
.Select(x => x.Emp_Name).FirstOrDefault();
aLogger.AccessedType = aLogger.AreaAccessed.Contains("Report") ? "Report" : "Page";
}
context.Portal_Logger.Add(aLogger);
context.SaveChanges();
}
// Finishes executing the Logger as normal
base.OnActionExecuting(filterContext);
}
}
}
注意:Session["LoggerActivityTracking"]
和Session["LogedUserID"]
数据我在单独的控制器(AuthenticationController
)动作(ValidateUser
)中初始化,该操作在用户登录应用程序时执行。
下面我使用的是一个Utility类,它将从RawUrl中获取网页的确切名称(例如:/Home/MyController
或/Home/MyController1?id=1&name=chandan
)
以下是我的实用工具类
<强> UserActivityUtility.cs 强>
using System;
namespace MyMvcApplication.Utilities
{
public static class UserActivityUtility
{
/// <summary>
///
/// </summary>
/// <param name="pageName"></param>
/// <returns></returns>
public static string GetWebPageName(string pageName)
{
string formattedPageName = string.Empty;
//For All user web pages
if (pageName.Contains("UserLandingPage"))
{
formattedPageName = "Home - App Management";
}
//For Report Web Pages
else if (pageName.Contains("AppUtilization"))
{
formattedPageName = "Applocation Utilization Report";
}
else if (pageName.Contains("AlertReport"))
{
formattedPageName = "Alert Report";
}
else if (pageName.Contains("DetailedReport"))
{
formattedPageName = "Detailed Report";
}
//For Admin Web Pages
else if (pageName.Contains("CreateUser"))
{
formattedPageName = "Admin - Create User";
}
else if (pageName.Contains("UpdateUserDetails"))
{
formattedPageName = "Admin - Update User Details";
}
else if (pageName.Contains("EditUser"))
{
formattedPageName = "Admin - Edit User";
}
else
{
formattedPageName = pageName;
}
return formattedPageName;
}
}
}
最后将该属性应用于Controller Action / s(您要跟踪的操作),如下所示
public class HomeController : Controller
{
[LoggerActionFilter(Order = 1)]
public ActionResult UserLandingPage()
{
return View();
}
}
public class AdminController : Controller
{
[LoggerActionFilter(Order = 1)]
public ActionResult CreateUser()
{
return View();
}
}
public class ReportController : Controller
{
[LoggerActionFilter(Order = 1)]
public ActionResult AppUtilization()
{
return View();
}
}
以下是&#34; MS SQL StoredProcedure&#34;获取用户登录详细信息和页面访问摘要详细信息
用户登录详情
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[sp_Portal_GetUserLoginDetails_Logger]
(
@FromDate DATE,
@ToDate DATE
)
AS
BEGIN
--EXEC sp_Portal_GetUserLoginDetails_Logger '2017-06-16','2017-06-16'
SET NOCOUNT ON
SELECT u.LogedUserID,u.UserName,u.IPAddress,u.AreaAccessed,u.[Timestamp],u.LastLoggedInDay
FROM
(
SELECT DISTINCT l.LogedUserID,e.Emp_Name as 'UserName',l.IPAddress,l.AreaAccessed,l.[Timestamp],l.LastLoggedInDay
FROM
(
SELECT *
FROM
(
SELECT Row_Number() Over ( PARTITION BY LogedUserID ORDER BY [Timestamp] DESC) As RowNum, LogedUserID,
IPAddress,AreaAccessed,TIMESTAMP, DATEDIFF(D,TIMESTAMP,CURRENT_TIMESTAMP) AS 'LastLoggedInDay'
FROM Portal_Logger
WHERE CAST([Timestamp] AS DATE) BETWEEN @FromDate AND @ToDate
) t2
WHERE RowNum = 1
) l
JOIN D_EMP_MASTER e on l.LogedUserID = e.User_Id
) u
ORDER BY u.[Timestamp] DESC
END
Page Wise访问摘要详细信息
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_Portal_GetUserAccessedSummary_Logger]
(
@FromDate DATE,
@ToDate DATE
)
AS
BEGIN
--EXEC sp_Portal_GetUserAccessedSummary_Logger '2017-06-16','2017-06-16'
SET NOCOUNT ON
SELECT A.AreaAccessed,
COUNT(A.Timestamp) AS NoofTimeAccessed,
CONVERT(TIME(0), DATEADD(SECOND,SUM(SessionSeconds), 0)) AS TotalTimeSpent,
CONVERT(TIME(0), DATEADD(SECOND,AVG(SessionSeconds), 0)) AS AvgTimeSpent,
COUNT(DISTINCT LogedUserID) AS NoofUserAccessed
FROM
(
SELECT
A.AreaAccessed,A.Timestamp,
ISNULL(DATEDIFF(S,A.Timestamp,ISNULL(LEAD(A.Timestamp,1)OVER(PARTITION BY A.VisitorSessionId ORDER BY A.Timestamp),A.Timestamp)),0) AS SessionSeconds,
A.LogedUserID
FROM Portal_Logger A(NOLOCK)
JOIN D_EMP_MASTER B(NOLOCK) ON A.LogedUserID=B.User_Id
WHERE A.VisitorSessionId IS NOT NULL
AND CAST(A.Timestamp AS DATE) BETWEEN @FromDate AND @ToDate
)A
GROUP BY A.AreaAccessed
ORDER BY NoofTimeAccessed DESC
SET NOCOUNT OFF
END
答案 1 :(得分:0)
必须由用户或代码在某个时刻设置值。但是,您可以使用Session
数组来包含这些值。它们将在登录会话期间保留。
存储到会话
Session["key"] = "data";
以及稍后的代码
var val = Session["key"]; // val now has a value of "data";
答案 2 :(得分:0)
我使用的是一个帮助方法,它使用反射并将模型转储到字典中,这样你就没有问题将值复制到其他地方,所有东西都来自一个地方“你的模型”
private static Dictionary<string, string> DumpModel(object obt)
{
Type type = obt.GetType();
if (TypeDefinitionIsList(type))
return null;
PropertyInfo[] properties = obt.GetType().GetProperties();
Func<PropertyInfo, string> func = info =>
{
if (info.PropertyType.GetInterface(typeof (ICatalogue<>).FullName) != null)
{
dynamic propertyValue = info.GetValue(obt, null);
if (propertyValue != null)
return string.Format("{{ Id: {0}, Description: {1} }}",
propertyValue.Id,
propertyValue.Description);
return "null";
}
object normalValue = info.GetValue(obt, null);
if (normalValue == null) return "null";
return TypeDefinitionIsList(normalValue.GetType()) ? HelpersMessages.NotSupportedList : normalValue.ToString();
};
return properties.ToDictionary(x => "model-"+x.Name, func);
}
您可以在过滤器属性中使用它
var request = filterContext.HttpContext.Request;
object model = filterContext.Controller.ViewData.Model;
你可以使用像
这样的东西 DumpModel(model);
使用这些值记录您需要的wharever, 我希望这有帮助