Skip to content

Instantly share code, notes, and snippets.

@cdrnet
Created August 20, 2011 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdrnet/1158820 to your computer and use it in GitHub Desktop.
Save cdrnet/1158820 to your computer and use it in GitHub Desktop.
Lokad.Cloud Standalone Storage Instrumentation Sample
// NuGet Dependencies: Lokad.Cloud.Storage, Rx-Main
// Framework Dependencies: System.Runtime.Serialization
using System;
using System.Reactive.Linq;
using System.Runtime.Serialization;
using Lokad.Cloud.Storage;
using Lokad.Cloud.Storage.Instrumentation;
using Lokad.Cloud.Storage.Instrumentation.Events;
namespace LokadCloudStorageInstrumentationSample
{
[DataContract]
class Book
{
[DataMember]
public string Title { get; set; }
[DataMember]
public string Author { get; set; }
}
class Program
{
static void Main(string[] args)
{
var observer = new CloudStorageInstrumentationSubject();
// subscribe to all storage system events and write their type
// name directly to the console. In this example you would
// likely see 3 StorageOperationSucceeded events
observer.Subscribe(Console.WriteLine);
// for retrials we may want some additional special logging:
observer.OfType<StorageOperationRetriedEvent>()
.Subscribe(@event => Console.WriteLine("Retried because of exception {0}.", @event.Exception));
// Note that ForInMemoryStorage is currently not instrumented
// and would not raise any system events to be logged.
var providers = CloudStorage
.ForDevelopmentStorage()
//.ForAzureAccountAndKey("accountName", "secretKey")
.WithObserver(observer)
.BuildStorageProviders();
// 'books' is the name of the table
var books = new CloudTable<Book>(providers.TableStorage, "books");
var potterBook = new Book { Author = "J. K. Rowling", Title = "Harry Potter" };
var poemsBook = new Book { Author = "John Keats", Title = "Complete Poems" };
// inserting (or updating record in Table Storage)
books.Upsert(new[]
{
new CloudEntity<Book> {
PartitionKey = "UK", RowKey = "potter", Value = potterBook},
new CloudEntity<Book> {
PartitionKey = "UK", RowKey = "poems", Value = poemsBook}
});
// reading from table
foreach (var entity in books.Get())
{
Console.WriteLine("{0} by {1} in partition '{2}' and rowkey '{3}'",
entity.Value.Title, entity.Value.Author,
entity.PartitionKey, entity.RowKey);
}
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment