---
title: "Create Log Event in Windows Azure"  
description: "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"  
author: "Chris Anderson"  
published: 2012-01-23  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/868/create-log-event-in-windows-azure  
category: "cloud computing"  
tags: ["cloud computing"]  
reading_time: 2 minutes  

---

# Create Log Event in Windows Azure

In this article I am going to explain how create event log in windows azure application. In the [production](https://www.mindstick.com/news/2276/by-the-end-of-2023-tesla-cybertruck-mass-production-will-begin) [deployment](https://www.mindstick.com/articles/332756/exploring-the-benefits-of-continuous-integration-and-deployment), sometimes we need to trace the inner working of our application. In ASP.Net and windows [form application](https://www.mindstick.com/forum/34200/system-data-sqlclient-sqlexception-occurred-while-using-checked-or-radio-button-in-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        {            get;            set;        }        public DateTime EventTime        {            get;            set;        }    } 
```

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](https://www.mindstick.com/interview/1411/what-is-an-abstract-schema):

```
    public class MyListener : TraceListener    {        public override void Write(string msg)        {            WriteLine(msg);        }        public override void WriteLine(string msg)        {            CloudStorageAccount account = CloudStorageAccount.DevelopmentStorageAccount;            CloudTableClient client = account.CreateCloudTableClient();            client.CreateTableIfNotExist("Log");             // 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);            tableContext.SaveChanges();        }    }
```

The **WriteLine()** methods stores the [messages](https://www.mindstick.com/news/4179/google-faces-backlash-for-claiming-all-texts-in-google-messages-are-end-to-end-encrypted) into [development](https://yourviews.mindstick.com/view/83465/china-s-development-near-indian-border-and-how-it-is-a-threat-to-india) storage table.

Now we can integrate the listener to our application, Create a new web role [project](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful) 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](https://yourviews.mindstick.com/view/81617/now-it-is-time-to-say-goodbye-to-internet-explorer) (Ctrl + W + L) and expand Windows Azure Storage section and Tables section. [Double click](https://www.mindstick.com/forum/156225/differentiate-between-google-adword-and-double-click) on the log table to view the result.

![Create Log Event in Windows Azure](https://www.mindstick.com/mindstickarticle/d6a4b9da-1b7b-4483-9a18-bf53a0c21a3b/images/7ddbc812-9ff0-4aca-a535-a4b272fe1ac5.png)

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.

\

---

Original Source: https://www.mindstick.com/articles/868/create-log-event-in-windows-azure

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
