Semantic logging using azure

http://www.shujaat.net/2013/09/semantic-logging-application-block-in.html

Add nuget package for Semantic logging

static void Main(string[] args)
{var listener = ConsoleLog.CreateListener() as ObservableEventListener ??

new ObservableEventListener();

listener.EnableEvents(
MyEventSource.Log,
EventLevel.LogAlways,
Keywords.All);

//// Subscription with Windows Azure Table Service on Dev
listener.LogToWindowsAzureTable(
“azureSink”,
“UseDevelopmentStorage=true;”,
“SLABNEW”);

// Subscription to Console Sink

listener.LogToConsole();

//Trace Event
MyEventSource.Log.ApplicationStart(” Start console “, “user 1”);
var ordersProcessed = 0;
for (int i = 0; i < 10; i++)
{
ordersProcessed++;
Console.WriteLine(“Order no {0} is processed “, ordersProcessed);
}
MyEventSource.Log.ApplicationEnd(” Finished “, ordersProcessed);

Console.ReadLine();
}

class MyEventSource : EventSource
{
//create a static field instance that provides access to an instance of this class
private static readonly Lazy<MyEventSource> Instance
= new Lazy<MyEventSource>(() => new MyEventSource());

//create a static property called Log that returns the current value
//of the Instance field of the event source. This value is

private MyEventSource() { }

public static MyEventSource Log
{
get { return Instance.Value; }
}

[Event(100, Message = “Application Started..”)]
public void ApplicationStart(string startMessage, string userName)
{
if (this.IsEnabled()) //helps to avoid processing events that no listeners have been configure for
{
WriteEvent(100, startMessage, this.MyUtilityMethod(userName));
}
}

[Event(500, Message = “Application Ended..”)]
public void ApplicationEnd(string endMessage, int ordersProcessed)
{
if (this.IsEnabled()) //helps to avoid processing events that no listeners have been configure for
{
WriteEvent(500, endMessage, ordersProcessed);
}
}

//demonstrate method which are not part of EventSource contract
private string MyUtilityMethod(string userName)
{
return string.Format(“User {0} “, userName);
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s