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

PostSharp Diagnostics Now Supports Loupe in the Box

The latest update to PostSharp Diagnostics adds Loupe support, enabling extensive high-performance logging to be added to any .NET application with virtually no code changes. PostSharp even has a great free option for developers that complements Loupe Desktop! Read more

Loupe Agent for .NET Core Now Available

The first release of the Loupe Agent for .NET Core is also our first open source version of the Loupe Agent. This is the first step in our plan to open source the entire Loupe Agent to make it easier for anyone to extend and take advantage of what Loupe... Read more

We've Moved Loupe Service to App.OnLoupe.Com

Loupe Service now has a shorter, direct site name that's faster, anywhere in the world. Just to go App.OnLoupe.Com, the new CDN-accelerated endpoint for the Loupe Service. Your existing Agents and Loupe Desktops are unaffected by this change, but access to the web UI will be redirected to the new... Read more

Rock solid centralized .NET logging

Unlimited applications, unlimited errors, scalable from solo startup to enterprise.