In this article I am going to explain how create event log in windows azure application. In the production deployment, sometimes we need to trace the inner working of our application. In ASP.Net and windows form application we can trace the information into a file. But in an Azure we can use the following methods to trace information:

  • Writing Trace to Storage Account Table
  • Writing Trace to Storage Account Queue
  • Writing Trace to SQL Azure
  • Emailing Trace to Administrator

Now create an entity (LogEntry) which is derived from the TableServiceEntity, as we store this in the storage table.

 public class LogEntry : TableServiceEntity
        public string Message
        public DateTime EventTime


Now create a custom listener class (MyListener) by inheriting from TraceListener. For deriving from it we need to override Write () and WriteLine () methods which are abstract:

    public class MyListener : TraceListener
        public override void Write(string msg)
        public override void WriteLine(string msg)
            CloudStorageAccount account = CloudStorageAccount.DevelopmentStorageAccount;
            CloudTableClient client = account.CreateCloudTableClient();
            // Create table context
            TableServiceContext tableContext = new TableServiceContext                                                                   (account.TableEndpoint.ToString(),account.Credentials); 
                // Create entity
            LogEntry log = new LogEntry();
            log.PartitionKey= Guid.NewGuid().ToString();
            log.RowKey= Guid.NewGuid().ToString();
            log.Message= msg;
            log.EventTime= DateTime.Now;
            // Add entity to table and save the changes
            tableContext.AddObject("Log", log);

 The WriteLine() methods stores the messages into development storage table.

Now we can integrate the listener to our application, Create a new web role project and add the following code into the WebRole OnStart() method.

 public override bool OnStart()
            Trace.Listeners.Add(new MyListener());
            Trace.WriteLine("Traced Message");
            Trace.WriteLine("Another traced message");
            return base.OnStart();

  Now press F5 to execute the application.

After executing the application you can view the results in a table. Open the server explorer (Ctrl + W + L) and expand Windows Azure Storage section and Tables section. Double click on the log table to view the result.

Create Log Event in Windows Azure

It will display the output in right section as shown in the above figure.

I think after reading this article you can easily trace an event in a windows azure application and store them in a table.

  Modified On Mar-23-2018 05:58:58 AM

Leave Comment