Log Visitor Count in Asp.net MVC I have a table to Log visitor Count Like :

public class VisitorLog:BaseEntity
{
    public int Id { get; set; }
    public DateTime VisitedOn { get; set; }
    public string LocationIP { get; set; }
    public string BrowserName { get; set; }

    public VisitorLog()
    {
        VisitedOn = DateTime.Now;
    }
}


and in Global.asax I have this :

protected void Application_BeginRequest(object sender,EventArgs e)
{
    if (new HttpRequestWrapper(Request).IsAjaxRequest()) return;

    foreach (var task in ApplicationObjectFactory.Container.GetAllInstances&lt;IRunOnEachRequest&gt;())
    {
        task.Execute();
    }
}


and Implementation in Service Layer is like below :

public bool Add(string locationId, string browserName)
{
    if (string.IsNullOrEmpty(locationId) || string.IsNullOrEmpty(browserName)) throw new ArgumentNullException("Visitor Log");
  return  Add(new AddVisitLogViewModel { BrowserName=browserName,LocationIP=locationId.ToLower()});
}

public bool Add(AddVisitLogViewModel visitorLogViewModel)
{

    if (visitorLogViewModel == null) throw new  ArgumentNullException("visitorLog");
    if (Any(row =&gt; row.LocationIP == visitorLogViewModel.LocationIP &amp;&amp; DbFunctions.TruncateTime(row.VisitedOn) ==DbFunctions.TruncateTime(DateTime.Now))) return false;
    var model = Mapper.Map&lt;VisitorLog&gt;(visitorLogViewModel);
    AddItem(model);
    return true;
}


In Application_BeginRequest I have to check request is Ajax or Action Call and others , is there a better way to check this or Improve these code ?
First of all I'd make code in Application_BeginRequest little bit more self-explicative:

if (!IsAjaxRequest())
    ExecuteAllRunOnEachRequestHandlers();


You have to introduce two small utility functions but it will make the intent clear when reading Application_BeginRequest (which may grow over time) without going in the details.

private bool IsAjaxRequest()
    =&gt; new HttpRequestWrapper(Request).IsAjaxRequest();

private void ExecuteAllRunOnEachRequestHandlers()
{
    foreach (var task in GetAllHandlers&lt;IRunOnEachRequest&gt;())
        task.Execute();

    IEnumerable&lt;T&gt; GetAllHandlers&lt;T&gt;()
        =&gt; ApplicationObjectFactory.Container.GetAllInstances&lt;T&gt;();
}


It may happen that you need to ignore other types of requests, in this case simply change IsAjaxRequest() to IsPageVisit() (or whatever else expresses the concept you want to capture) and remove the negation.

In Add(string, string) you always throw ArgumentNullException but which parameter is wrong is an important information that is lost. Also to be String.Empty and to be null are different things:

if (locationId == null)
    throw new ArgumentNullException(nameof(locationId));

if (String.IsNullOrWhiteSpace(locationId))
    throw new ArgumentException("...", nameof(locationId));

if (browserName == null)
    throw new ArgumentNullException(nameof(browserName));

if (String.IsNullOrWhiteSpace(browserName))
    throw new ArgumentException("...", nameof(browserName));


Vertical space is free, do not pack your code in one line, it hurts readability.

return Add(new AddVisitLogViewModel 
{
     BrowserName = browserName,
     LocationIP = locationId.ToLower()
});


In Add(AddVisitLogViewModel) you may apply same principles:

public bool Add(AddVisitLogViewModel visitorLogViewModel)
{
    if (visitorLogViewModel == null)
        throw new  ArgumentNullException(nameof(visitorLog));

    if (HasVisistedToday(visitorLogViewModel)
        return false;

    AddItem(Mapper.Map&lt;VisitorLog&gt;(visitorLogViewModel));

    return true;
}


Where HasVisitedToday() is your actual code with a given name to quickly understand what it does (and probably to match an entry in your specifications). 

Note the use of nameof() to avoid string constants.

Side note: if Add(AddVisitLogViewModel) isn't invoked outside your assembly (its container class is internal) then you might want to replace parameters validation with assertions:

Debug.Assert(visitorLogViewModel != null);