The .NET Client Library provides detailed error information by way of its IOperationResult interface.  To find out why a store operation failed, use ExecuteStore instead of Store.

var result = client.ExecuteStore(StoreMode.Add, “key”, “value”);
if (! result.Success)
{
Console.WriteLine(“Operation failed with message {0} and status code {1}”, result.Message, result.StatusCode);
}

While the IOperationResult value that’s returned provides status codes, caught exceptions and error messages, there are situations where detailed logging is necessary to diagnose a persistent problem.

With the recent 1.2.2 release of the .NET Client Library for Couchbase, logging assemblies are included by default in both the Nuget package and the zip with the latest binaries.  Out of the box, Couchbase (and its dependency Enyim.Caching) supports log4net and NLog logging.

It’s pretty straightforward to enable logging with Couchbase, but you do need to know the basics of configuring log4net or NLog to get going.  If you’re just starting out with Couchbase and ASP.NET, it would be useful to see logging details without having to learn another framework, even if minimal.  Fortunately, if you’re using the diagnostics tool Glimpse in your ASP.NET application, you can turn on logging quite easily.

If you’re not familiar with Glimpse, the common analogy is “Firebug for ASP.NET.”  Once you install the NuGet package for Glimpse and enable the framework, you get a diagnostics window in your application that provides you with a view of the request as it was executed on the server.  You can see your web.config settings, environment details, routes, server variables and several other aspects of your executing web application.

Nik and Anthony, who created the open source Glimpse project (supported now by Redgate) have created a powerful extensibility model.  Plugging into the Glimpse UI to provide instrumentation details for a library is as simple as implementing the ITab interface (or extending TabBase).

The Enyim.Caching dependency in the Couchbase client defines ILog and ILogFactory interfaces, which when implemented may be used to provide a custom logging provider for capturing Couchbase instrumentation details.  For my new CouchbaseLabs project Couchbase.Glimpse, I have created a new logger that will capture per-request logs and send the output to Glimpse.

To get started, install and enable Glimpse.  Once you’ve done that, install the CouchbaseGlimpse extension.  The NuGet package will add the necessary configuration for you.  You can also grab the source from GitHub.  If you go the source code route, you’ll need to include the config section below.

<configuration>
<configSections>
<sectionGroup name=“enyim.com”>
<section name=“log” type=“Enyim.Caching.Configuration.LoggerSection, Enyim.Caching” />
>

>
<enyim.com>
<log factory=“Couchbase.Glimpse.Logging.GlimpseLogFactory, Couchbase.Glimpse” />
>
>

You’ll need to enable logging to start capturing details from the client.  Somewhere in your application, probably Application_Start, you’ll need to call the Configure method on GlimpseLogger.

var config = new GlimpseLogConfiguration { IsDebugEnabled = true };
GlimpseLogger.Configure(config);

Logging levels are cumulative, so enabling debug enables all other levels.  You can also capture specific loggers (logs per class) by adding log names to the white list.  The configuration below will output logs originating only in the PooledSocket and ConfigHelper classes.

var config = new GlimpseLogConfiguration { IsDebugEnabled = true };
config.SourceWhiteList.AddRange(new[] { “PooledSocket”, “ConfigHelper” });
GlimpseLogger.Configure(config);

Once you’ve built your app to include the new Couchbase tab in Glimpse, navigate to a page where the Couchbase client is being used.  You should see something similar to the screen capture below.

You’ll find that when the client is first bootstrapping, it’s a bit chatty with debug logs.  However, if you’re having a problem connecting to your cluster, there’s probably valuable information available to you, including the cluster config (which you can see as JSON in the screen capture above).

The GlimpseLogger isn’t persisting its logs, so if you need to capture diagnostic information beyond a request, you’ll need to use log4net or NLog.

The GitHub repo contains a copy of the .NET beer sample app with Glimpse enabled.  Clone that repo and run the app to get a quick sense of how the plugin works.

Author

Posted by John Zablocki, NET. SDK Developer, Couchbase

John Zablocki is a NET. SDK Developer at Couchbase. John is also the organizer of Beantown ALT.NET and a former adjunct at Fairfield University. You can also check out the book on Amazon named "Couchbase Essentials" which explains how to install and configure Couchbase Server.

Leave a reply