Archive for Gibraltar
Getting to Know Gibraltar – Swapping Trace For Log
Posted by: | CommentsYou’ll recall from the last post that we have the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
namespace GibraltarGettingStartedApp
{
class Program
{
static int Main(string[] args)
{
Trace.WriteLine("About to Blow up...");
int zero = 0;
try { return 1 / zero; }
catch (DivideByZeroException e)
{
Trace.WriteLine("Error: " + e.Message);
}
Trace.Close();
return 0;
}
}
}
Showing us that we can get Gibraltar working with the Trace code that we already had in our code base. That’s a huge advantage if you are already using the Trace functionality in your code.
However, as you start working with Gibraltar, you’ll find that it supports it’s own logging API that offers several distinct advantages over Trace.WriteLine. There may come a time when you wish to take advantage of our better API. If so, you’ll want to swap out all of your Trace commands or, if you have a very large application, you may decide just to swap out the calls in a particular area. In this post, we are going to examine how easy it is to do that.
In turns out all that’s required is to replace the calls to Trace with calls to Gibraltar’s Log class. There is no need to touch the Gibraltar “plumbing code” at all.
Doing that, it looks like we’ve hit our first little wrinkle. While Log does have it’s own versions of Trace’s TraceInformation, TraceWarning and TraceError, there’s no direct analogue for Trace.Writeline. We used WriteLine because we wanted to do the simplest thing that worked. This means instead of just replacing Trace with Log, we can take this opportunity to move to a more rich method. Also, as there is no analogue for Trace.Close, so we’ll change our code to use Log.EndSession. Having made these changes, our code now looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using Gibraltar.Agent;
namespace GibraltarGettingStartedApp
{
class Program
{
static int Main(string[] args)
{
Log.Verbose("About to blow up...");
int zero = 0;
try { return 1 / zero; }
catch (DivideByZeroException e)
{
Log.Verbose("Error: " + e.Message);
}
Log.EndSession();
return 0;
}
}
}
And as we can see, from Gibraltar Analyst, everything still works as expected:
So, we have successfully swapped out Trace for Log and not broken anything, great!
However, now that we’ve made the swap over to Log, we can take advantage of it’s overloaded methods to provide more information. Instead of just passing a string to Trace.TraceVerbose, when we are using Log, we can pass the exception too, which allows Analyst to capture it:
Of course, once we have successfully moved from Trace to Log we can take advantage of the richer API available with Log. Sticking with the example of logging information we can change our code to capture Category and caption along with our message:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using Gibraltar.Agent;
namespace GibraltarGettingStartedApp
{
class Program
{
static int Main(string[] args)
{
Log.Verbose("Examples",
"About to Blow up...",
"Additional details can be provided for each log message");
int zero = 0;
try { return 1 / zero; }
catch (DivideByZeroException e)
{
Log.Verbose(e,
"Examples",
"Error: " + e.Message,
"Exception type: {0}",
e.GetType().Name);
}
Log.EndSession();
return 0;
}
}
}
And, as you can see, Analyst has recorded the category:
And the caption:
And the formatted description:
I mentioned that the Gibraltar API had a couple of advantages over the Trace API, we won’t go into them in detail here but one of them, which I think is very cool, is that the API will fail safe with regard to the format string. If you pass too many objects with regard to placeholders, like so…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using Gibraltar.Agent;
namespace GibraltarGettingStartedApp
{
class Program
{
static int Main(string[] args)
{
Log.Verbose("Examples",
"About to Blow up...",
"Additional details can be provided for each log message");
int zero = 0;
try { return 1 / zero; }
catch (DivideByZeroException e)
{
Log.Verbose(e,
"Examples",
"Error: " + e.Message,
"Exception type: {0}",
e.GetType().Name,
"This shouldn't be here!",
"Nor should this!");
}
Log.EndSession();
return 0;
}
}
}
Then Gibraltar won’t throw an exception, but will in fact continue to work as expected, matching the first object to the placeholder and ignoring the rest. Neat, eh?
If you make the inverse mistake, having too many placeholders, then Gibraltar will still work, but will record that error too, like so:
You should also check out these methods: Log.Critical, Log.Error, Log.Warning and Log.Information or see the complete API for the Log class.
Well let’s end this post here. Today we’ve looked at preparing our code to swap from Trace to Log, we’ve swapped to Log, using the analogue of the Trace methods, and then we’ve improved our logging by using the richer API available to us when we use Log.
Next time we’ll look at swapping out Log for a third party logging solution. Until then… happy coding! ![]()
You won’t want to miss us at CodeMash!
Posted by: | Comments
Next week is CodeMash 2012 – January 11-13 at the cool Kalahari resort in Sandusky, OH. Don’t worry, it’s cool you have no idea where that is because in Ohio you’ll never go outside in January anyway.
If you’re already planning on attending, that’s fabulous – we’ll be all over, just look for the guys in the dark blue polos. If you weren’t planning on it, you should – you might find some late minute tickets from people that couldn’t make it.
Whats on tap from us at CodeMash? A bunch of things:
- Gary and I are presenting some of our more popular sessions
- We’ll be demoing Gibraltar 3.0 – with a number of updates not in the previous betas and the first public preview of a completely new part of Gibraltar (oooh!). Stop by our booth and see everything that’s new before its public release.
- We’ll be showing off the first experiment to escape from Gibraltar Labs (find out what it is at CodeMash – it’s nothing like anything we’ve done before!)
- We’ll be running a contest to help us name our new experiment before it can be released into the wild. Come up with the best name and you’ll get a new Kindle Fire.
- Along with a few other exhibitors we’ll be part of ButtonMash – stop by our booth to pick up one of our buttons to be entered into the ButtonMash raffle for some really neat prizes.
And that’s just *our* part of the event. Wow, I’m gettin’ tired just thinking about it.
Gibraltar 3.0 New Feature Dive – Live Sessions
Posted by: | CommentsThe Gibraltar Agent has always supported viewing the real time log messages from an application, provided your application was a Windows application (WinForms and WPF) and you were the one running the application. But, what if you want to see what’s happening inside a Windows Service or a process running somewhere else? In Gibraltar 2.0 you had to have your process explicitly publish a snapshot of your log data up to a Hub or via email. This required you to code some way to signal your application when you wanted to publish information and then have all of the data collected in that process since it started get shipped to you.
For Gibraltar 3.0 we’ve introduced a new way of seeing your log data in real time: Live Sessions. When you enable live sessions you can view log data remotely, efficiently, in real time. In Analyst you’ll see a new node in the tree view called Live sessions. When you select that, you’ll see a list of all of the available sessions which you can pick from.
When you first click the Live View tab a data connection is established and you’ll get the last thousand messages that were logged. Once that data’s come across, new messages are shown as they are recorded. You can filter the display to show just messages of a particular severity or to search their text for a particular value.
If you switch away from one session to another the live view stream will continue to run in the background – keeping it current with the most recent thousand messages. Gibraltar takes steps to keep this memory efficient so you can have multiple live streams running at the same time without performance issues.
If you want to keep watching an application while you work you can double click it and get an independent window which will update in real time as long as the remote application is running.
Efficient over the Network
The data stream that’s used by Live Sessions is designed to be very efficient over networks. It is highly compressed (using the same techniques employed by the Agent to write log files), connection based, and only running when there is an active viewer monitoring the session. In real world scenarios the live stream for a busy server can be easily viewed over a low bandwidth, intermittent connection like a tethered phone or Mifi.
- If the connection is lost, the data stream restarts with the last message you received so it can show you all of the messages
- If no one is watching a live session then the data is buffered in the Agent but not sent across the network.
Safety First
There are a number of features designed to make sure live sessions don’t cause a problem to your application:
- Asynchronous, buffered transmission: The Agent publishes data to any set of listeners asynchronously from recording the log file to ensure slow network connections or network connectivity problems won’t slow down recording the main log file.
- Drop on overflow: If there are more log messages waiting to be sent than can be sent over the network new messages will be dropped instead of holding up the application. This is in contrast to the log file which will switch to synchronous while it catches up.
- Encryption optional: You can choose to enable SSL for live session communication so it will be encrypted end-to-end.
Live Sessions and Firewalls
The Live Session protocol is designed to work with most firewall configurations:
- All connections are outbound: The Agent and Analyst establish connections to the central server to communicate. No connections are directly created between the Agent and Analyst. This works well with NAT and often requires no explicit firewall rules or exceptions.
- Separate ports are used for Agent and Analyst: If you need to create a firewall rule you can create separate rules for Agent and Analyst traffic (ports 29970 and 29971 respectively)
- Connections are asynchronous: Many firewalls will simply fail to respond to attempts to connect to ports that aren’t open, causing very long timeouts. By performing all connections asynchronously this prevents the application from being unresponsive.
Try it out Today
If you’d like to check out Live Sessions for yourself, you’ll need to download the latest build of Gibraltar 3.0 and upgrade to the included Agent, Analyst and Hub. The Hub Server does the central relaying between agents and analysts so it has to be set up in the center. Live Sessions are not available with the subscription Hub Service operated by us at this time, you have to have your own private Gibraltar Hub.
Getting to Know Gibraltar – Adding Exceptions
Posted by: | CommentsIn my last post we set up a minimalist application, attached Gibraltar to it, and reported some trace information. In this post we’re going to build on that and add exceptions. First we are going to add an unhandled exception, so we change our code so that it looks like this:
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
namespace GibraltarGettingStartedApp
{
class Program
{
static int Main(string[] args)
{
Trace.WriteLine("About to Blow up...");
int zero = 0;
Trace.Close();
return 1 / zero;
}
}
}
}
That should get the runtime’s panties in a bunch, let’s try it. ![]()
Yep, that did it! Now, let’s see what Gibraltar logged…
Gibraltar knows we crashed and if we open up the record…
We know what the problem was, we can click the link and go right to the line in the source code and Gibraltar tells us what the user experience is going to be, “This fatal error will not be reported to the user, then the application will exit”. All and all, pretty cool.
As an aside, let me close down VS, I want to test if I click the link, will it open the file and go to the line? It does! That’s cool, though I notice that it only opens the actual file and not the solution or project that the file is in. Hmm, I’ll need to investigate that more as it’d be better if the entire solution was opened, don’t you think?
Let’s try running the same unhandled exception in a winforms application and see what happens. We’ll need to run the exe from the commandline, as the VS debugger will “get in the way” and try to do “clever stuff”, but when we do, we see the following dialog:

Here Gibraltar is giving us even more help. It’s popped up a dialog which will actually help us recover from the crash and carry on using our application. If we select “Restart Application” Gibraltar will restart the application for us and enable us to carry on using our application. Of course, this facility should only be used in a testing environment. In the real world, I don’t think it would be wise to restart and to continue using an application which had just suffered from a crash, as there is no way to know what state the underlying model is in. However, from a testing perspective, it’s an excellent tool.
Of course, the same information is recorded in Gibraltar:
![]()
Notice that the “Status” has been recorded as “Normal”. If we open the record we can see why:
It’s because we chose to continue the execution and shut down the application normally after.
Okay, let’s change our console application code to handle the exception…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
namespace GibraltarGettingStartedApp
{
class Program
{
static int Main(string[] args)
{
Trace.WriteLine("About to Blow up...");
int zero = 0;
Trace.Close();
try { return 1 / zero; }
catch (DivideByZeroException e) { }
return 0;
}
}
}
And let’s see what difference that made to Gibraltar…

Hmm, as far as Gibraltar is concerned everything is normal, there is no sign of the handled exception. For a clue as to why that is, let’s examine logging best practices, you can read them at…
http://www.gibraltarsoftware.com/Support/Documentation/Logging_BestPractices.html
And they quite clearly state:
“We strongly recommend that in all cases where an exception is consumed within a catch block that you log the exception with an Informational severity rather than silently swallowing the error.”
Okay, so this states that if we handle an exception, and we want that fact recorded by Gibraltar, then we have to log it. That makes sense really. So, let’s change our code to the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
namespace GibraltarGettingStartedApp
{
class Program
{
static int Main(string[] args)
{
Trace.WriteLine("About to Blow up...");
int zero = 0;
try { return 1 / zero; }
catch (DivideByZeroException e)
{
Trace.WriteLine("Error: " + e.Message);
}
Trace.Close();
return 0;
}
}
}
Now let’s see how that’s affected Gibraltar:

This time you can plainly see that the error has been recorded, which is good, but notice too that there’s no exception tab:

Of course, we should be able to do better and we can. If we use the TraceInformation method, and pass a formatted string, along with the exception, then Gibraltar will give us a much richer experience.
So, let’s change our code to use that method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
namespace GibraltarGettingStartedApp
{
class Program
{
static int Main(string[] args)
{
Trace.WriteLine("About to Blow up...");
int zero = 0;
try { return 1 / zero; }
catch (DivideByZeroException e)
{
Trace.TraceInformation("Error: {0}",e.Message,e);
}
Trace.Close();
return 0;
}
}
}
And let’s look at the information provided in Gibraltar:

This time, as you can see, we get the “Exceptions” tab. This is because Gibraltar scans the insertion variables and, if it finds one that is an exception, then it uses that as the exception for the log message (regardless of its severity). Pretty cool eh?
In this post we added an unhandled exception to your console application to see what effect that had on Gibraltar. We then added an unhandled exception to a winforms application so we could see the enhanced features available to us there and then we finished up by handling that exception and seeing why best practice is to log handled exceptions.
I hope you’ll join me next time as we continue our exploration of Gibraltar, until then, happy coding! ![]()


