Wednesday, March 24, 2010

Logging to Logverse

If you haven't heard of Logverse, it's a service that allows applications to log to it. And just about anything can be logged to it--errors, user actions, debug information... whatever you want.

It's perfect for applications out in the field, like desktop and mobile applications. But even web or other server applications can take advantage of it.

You don't need to use a logging framework to use Logverse. But if you use The Object Guy's Logging Framework, it is extremely easy to create a logger that logs to Logverse.

I have two very small classes here. The first is just a utility class that will write to Logverse.

public class LogverseWriter
{
   private readonly Guid LogId;
   private readonly string SubscriptionKey;

   public LogverseWriter(string aLogId, string aSubscriptionKey)
   {
      LogId = new Guid(aLogId);
      SubscriptionKey = aSubscriptionKey;
   }

   public bool Write(Dictionary<string, string> values)
   {
      var logEntryInfo = new Logverse.LogEntryInfo()
         {
           LogId = LogId,
           SubscriptionKey = SubscriptionKey,
           ClientTimeStampUtc = DateTime.UtcNow
         };

      logEntryInfo.Fields = values;

      var service = new Logverse.LogServiceClient();
      var result = service.Log(logEntryInfo);
      service.Close();

      return result.Result == Logverse.LogResult.EResult.Logged;
   }
}

The second class I have is the LogverseLogger.

public class LogverseLogger : Logger
{
   private readonly LogverseWriter LogverseWriter;

   public LogverseLogger(string aLogId, string aSubscriptionKey)
   {
      LogverseWriter = new LogverseWriter(aLogId, aSubscriptionKey);
   }

   protected override bool DoLog(LogEntry aLogEntry)
   {
      return LogverseWriter.Write(
              new Dictionary<string, string>()
              {
                { "Category", aLogEntry.Category != null ? aLogEntry.Category.ToString() : "" },
                { "Severity", aLogEntry.SeverityString },
                { "Message",  aLogEntry.Message }
              });
   }

   public static void Create(string aLogId, string aSubscriptionKey)
   {
      ConfigLogger.Instance.AddLogger("logverse_" + aLogId, new LogverseLogger(aLogId, aSubscriptionKey));
   }
}

The Logverse logger can't currently be configured using the web.config (or app.config), so at the beginning of the application I call the Create method. Here's an example for a web application. This code would go into global.asax.cs.

protected void Application_Start(object sender, EventArgs e)
{
   LogverseLogger.Create("<mylogid>", "<mysubscriptionkey>");
}

WOW!

Now that was easy. And now I can log to Logverse. Pretty cool, huh?

One might ask, "What about exception handling?" Good question. Actually, it isn't required in the Logger, because that's handled automatically by the framework. (Cool!) However, if you were to use the LogverseWriter class independently, then you'd probably would want to put some exception handling in it. The only reason I didn't was so that I'd have an excuse to talk about it in this paragraph. :)

Friday, March 19, 2010

Logverse Videos



Sunday, March 14, 2010

Logverse

Logverse is coming...