User Activity logging in ASP.NET MVC app using Action Filter and log4net
In this post, I will demonstrate how to use an action filter to log user tracking information in an ASP.NET MVC app. The below action filter will take logged user name, controller name, action name, timestamp information and the value of route data id. These user tracking information will be logged using log4net logging framework.
- public class UserTrackerLogAttribute : ActionFilterAttribute, IActionFilter
- {
- public override void OnActionExecuted(ActionExecutedContext filterContext)
- {
- var actionDescriptor= filterContext.ActionDescriptor;
- string controllerName = actionDescriptor.ControllerDescriptor.ControllerName;
- string actionName = actionDescriptor.ActionName;
- string userName = filterContext.HttpContext.User.Identity.Name.ToString();
- DateTime timeStamp = filterContext.HttpContext.Timestamp;
- string routeId=string.Empty;
- if (filterContext.RouteData.Values["id"] != null)
- {
- routeId = filterContext.RouteData.Values["id"].ToString();
- }
- StringBuilder message = new StringBuilder();
- message.Append("UserName=");
- message.Append(userName + "|");
- message.Append("Controller=");
- message.Append(controllerName+"|");
- message.Append("Action=");
- message.Append(actionName + "|");
- message.Append("TimeStamp=");
- message.Append(timeStamp.ToString() + "|");
- if (!string.IsNullOrEmpty(routeId))
- {
- message.Append("RouteId=");
- message.Append(routeId);
- }
- var log=ServiceLocator.Current.GetInstance<ILoggingService>();
- log.Log(message.ToString());
- base.OnActionExecuted(filterContext);
- }
- }
The LoggingService class is given below
- public class LoggingService : ILoggingService
- {
- private static readonly ILog log = LogManager.GetLogger
- (MethodBase.GetCurrentMethod().DeclaringType);
- public void Log(string message)
- {
- log.Info(message);
- }
- }
- public interface ILoggingService
- {
- void Log(string message);
- }
The LoggingService class is using log4net framework for logging. You can add reference to log4net using NuGet.
The following command on NuGet console will install log4net into your ASP.NET MVC app.
PM> install-package Log4Net
The below configuration information in web.config will configure log4net for using with Sql Server
Let me add a log4net section to the <configSections> of web.config
- <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
The below is the log4net section in the web.config file
- <log4net>
- <root>
- <level value="ALL"/>
- <appender-ref ref="ADONetAppender"/>
- </root>
- <appender name="ADONetAppender" type="log4net.Appender.AdoNetAppender">
- <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
- <connectionString value="data source=.\SQLEXPRESS;Database=MyFinance;Trusted_Connection=true;" />
- <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
- <parameter>
- <parameterName value="@log_date" />
- <dbType value="DateTime" />
- <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
- </parameter>
- <parameter>
- <parameterName value="@thread" />
- <dbType value="String" />
- <size value="255" />
- <layout type="log4net.Layout.PatternLayout" value="%thread" />
- </parameter>
- <parameter>
- <parameterName value="@log_level" />
- <dbType value="String" />
- <size value="50" />
- <layout type="log4net.Layout.PatternLayout" value="%level" />
- </parameter>
- <parameter>
- <parameterName value="@logger" />
- <dbType value="String" />
- <size value="255" />
- <layout type="log4net.Layout.PatternLayout" value="%logger" />
- </parameter>
- <parameter>
- <parameterName value="@message" />
- <dbType value="String" />
- <size value="4000" />
- <layout type="log4net.Layout.PatternLayout" value="%message" />
- </parameter>
- </appender>
- </log4net>
Configure log4net
The below code in the Application_Start() of Global.asax.cs will configure the log4net
- log4net.Config.XmlConfigurator.Configure();
The below Sql script is used for creating table in Sql Server for logging information
CREATE TABLE [dbo].[Log] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[Date] [datetime] NOT NULL ,
[Thread] [varchar] (255) NOT NULL ,
[Level] [varchar] (20) NOT NULL ,
[Logger] [varchar] (255) NOT NULL ,
[Message] [varchar] (4000) NOT NULL
) ON [PRIMARY]