This project is read-only.
In this scenario we will extend the ActionFilterAttribute to provide a logging mechanism that store the user who has created the contact..

Creating a dedicated logger for created contact

We are going to create a new appender and a new logger dedicated to log an information message in a specific log file. Edit the app.config file and add the following code:

<!--Appender for logging created contact-->
<appender name="CreateContactFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="./log/CreatedContact.log" />
  <appendToFile value="true" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="2000000" />
  <rollingStyle value="Size" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <header value="[Header]&#xD;&#xA;" />
    <footer value="[Footer]&#xD;&#xA;" />
    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
<!--Dedicated logger-->
<logger name="LoggingContact">
  <level value="INFO" />
  <appender-ref ref="CreateContactFileAppender" />

Creating custom action filter attribute

Right click on the ./filters folder and add a new class named LogActionFilterAttribute. Make sure that the new created class is public and extends the base ActionFilterAttribute class provided by the Koossery.MVCwin open source.
Override the two methods OnActionExecuting and OnActionExcuted. Add a logger base on the previously created LoggingContact. The listing below shows the content of the class.

public class LogActionFilterAttribute : ActionFilterAttribute
    //The logger
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger("LoggingContact");
    string login = null;

    public override void OnActionExecuting(Koossery.MVCwin.Filters.context.ActionExecutingContext preContext)
        //Retrieve the current user
        if (Constants.CurrentUser != null) login = Constants.CurrentUser.Identity.Name;

        //log the message
        log.Info("The user " + login + " is creating a contact");

    public override void OnActionExecuted(Koossery.MVCwin.Filters.context.ActionExecutedContext postContext)
        if (Constants.CurrentUser != null) login = Constants.CurrentUser.Identity.Name;

        //log the message
        log.Info("The user " + login + " has created a contact");

Assigning the attribute to the Save action of the SaveController

Now that the custom attribute is created, we can assigned it to the Save action of the SaveController. That is made as shown below:

/// <summary>
/// Saves a contact
/// </summary>
/// <returns></returns>
public IActionResult Save()
    SaveViewData saveView;
		. . .

Run the application, log in (using {yacoubou,minastirite} or {bake,tochange}) and create a contact. Once the contact is create, locate the bin/debug folder, you should see the log folder wich contains the file CreateContact.log as shown in the figure below.


Open the file you should see the following, depending on the username :



In this scenario we learnt how to use the ActionFilterAttribute to create a logging mechanism. Create a custom ActionFilterAttribute is really simple. See you on the next scenario.

Last edited Sep 1, 2009 at 10:29 AM by yacoubou, version 2


No comments yet.