· 8 years ago · Jan 24, 2018, 04:14 AM
1// NuGet Dependencies: Lokad.Cloud.Storage, Rx-Main
2// Framework Dependencies: System.Runtime.Serialization
3using System;
4using System.Reactive.Linq;
5using System.Runtime.Serialization;
6using Lokad.Cloud.Storage;
7using Lokad.Cloud.Storage.Instrumentation;
8using Lokad.Cloud.Storage.Instrumentation.Events;
9
10namespace LokadCloudStorageInstrumentationSample
11{
12 [DataContract]
13 class Book
14 {
15 [DataMember]
16 public string Title { get; set; }
17
18 [DataMember]
19 public string Author { get; set; }
20 }
21
22 class Program
23 {
24 static void Main(string[] args)
25 {
26 var observer = new CloudStorageInstrumentationSubject();
27
28 // subscribe to all storage system events and write their type
29 // name directly to the console. In this example you would
30 // likely see 3 StorageOperationSucceeded events
31 observer.Subscribe(Console.WriteLine);
32
33 // for retrials we may want some additional special logging:
34 observer.OfType<StorageOperationRetriedEvent>()
35 .Subscribe(@event => Console.WriteLine("Retried because of exception {0}.", @event.Exception));
36
37 // Note that ForInMemoryStorage is currently not instrumented
38 // and would not raise any system events to be logged.
39 var providers = CloudStorage
40 .ForDevelopmentStorage()
41 //.ForAzureAccountAndKey("accountName", "secretKey")
42 .WithObserver(observer)
43 .BuildStorageProviders();
44
45 // 'books' is the name of the table
46 var books = new CloudTable<Book>(providers.TableStorage, "books");
47
48 var potterBook = new Book { Author = "J. K. Rowling", Title = "Harry Potter" };
49
50 var poemsBook = new Book { Author = "John Keats", Title = "Complete Poems" };
51
52 // inserting (or updating record in Table Storage)
53 books.Upsert(new[]
54 {
55 new CloudEntity<Book> {
56 PartitionKey = "UK", RowKey = "potter", Value = potterBook},
57 new CloudEntity<Book> {
58 PartitionKey = "UK", RowKey = "poems", Value = poemsBook}
59 });
60
61 // reading from table
62 foreach (var entity in books.Get())
63 {
64 Console.WriteLine("{0} by {1} in partition '{2}' and rowkey '{3}'",
65 entity.Value.Title, entity.Value.Author,
66 entity.PartitionKey, entity.RowKey);
67 }
68
69 Console.WriteLine("Press enter to exit.");
70 Console.ReadLine();
71 }
72 }
73}