The Quick Start Guide to using Gibraltar in ASP.NET MVC

The first step in using Gibraltar within an MVC site is to install the agent. The easiest way to do this is via NuGet, either using the interface or the Package Manager Console; in either case the package you need is Gibraltar.Agent. Once installed you can start using the logging features right away using the Gibraltar.Agent.Log class. For example, to log a warning about account creation failures, you could add the following to the RegisterAccount action:

if (createStatus == MembershipCreateStatus.Success)
{
    FormsAuthentication.SetAuthCookie(model.UserName, false);
    return RedirectToAction("Index", "Home");
}
else
{
    ModelState.AddModelError("", ErrorCodeToString(createStatus));
    Gibraltar.Agent.Log.Warning("Security", "Membership Creation Failure",
        "Failed to create account '{0}' Status: {1}",
        model.UserName, createStatus.ToString());
}

The Log class has other methods, such as Information, Error and Warning, all of which allow you to log message details and exceptions. These show up in Gibraltar Analyst so you can easily filter and group by the severity of the message. This, of course, is manual logging of messages, but you may want automatic logging of things such as exceptions. In this case you can use the standard MVC attribute system; you first create an attribute that inherits from HandleErrorAttribute, and within the OnException method you log your exception details to Gibraltar. Then you add this attribute to the filters collection in the RegisterGlobalFilters method in Global.asax. For a video to show you how to do this, see I See MVC.

You can find out more about simple logging in the Gibraltar help pages at https://doc.onloupe.com/webframe.html#Gibraltar.Agent~Gibraltar.Agent.Log.html

Since we’re now getting close to Christmas, what feature is on your Gibraltar Software Christmas List?

Related Posts

We're out of our Last Data Center

Back in January of 2016 we decided to completely transition out of our data centers and into the cloud (primarily Azure). We knew we had to do something - either make some big investments in new hardware or commit ourselves to migrating everything off our own gear. After looking at... Read more

Rock solid centralized .NET logging

Unlimited applications, unlimited errors, starting at $25/month