For this article I was using Windows Azure and SQL Database Tutorials - Tutorial 3: Using Windows Azure Blob Service.

In your controller you must to have code like:

 

public class BalooController : Controller
{
	private MyTableDBContext db = new MyTableDBContext();
	private const string messageImageBlobName = "golfermessageboardpics";
	private CloudBlobClient blobClient;
	private CloudBlobContainer blobContainer;
	private static CloudStorageAccount storageAccount;
	private CloudTableClient tableClient;

	public BalooController ()
	{
		string connectionString = RoleEnvironment.GetConfigurationSettingValue("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString");

		storageAccount = CloudStorageAccount.Parse(connectionString);
		tableClient = new CloudTableClient(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
		tableClient.CreateTableIfNotExist("myBlob");

		blobClient = storageAccount.CreateCloudBlobClient();
		blobContainer = blobClient.GetContainerReference(messageImageBlobName);
		blobContainer.CreateIfNotExist();

		var permissions = blobContainer.GetPermissions();
		permissions.PublicAccess = BlobContainerPublicAccessType.Container;
		blobContainer.SetPermissions(permissions);
	}

	//
	// GET: /Baloo/

	public ViewResult Index()
	{
		return View(db.BlobModel.ToList());
	}

Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString
is in
MvcApplication17\MvcApplication17.Azure\ServiceConfiguration.Local.cscfg (if name of your app is MvcApplication17 :) )
and golfermessageboardpics is actual name of the blob.

 

One example of foreign key model, using code first approach:

  

public class MyMaster
{
	public int Id { get; set; }
	public string Username { get; set; }

	public virtual ICollection MyDetails { get; set; }
}

public class MyDetail
{
	public int Id { get; set; }

	[ForeignKey("MyMaster")]
	public int masterId { get; set; }

	public virtual MyMaster MyMaster { get; set; }
}

and another version of same thing:

public class MyMaster
{
	public int MyMasterId { get; set; }
	public string Username { get; set; }

}

public class MyDetail
{
	public int MyDetailId { get; set; }

	[ForeignKey("MyMasterId")]
	public int masterId { get; set; }

	public virtual MyMaster MyMasterId { get; set; }
}

To add MVC application to IIS as web site, just right click on the sites, click add web site, but if you want to have host name like on the picture:

If you create a MVC app by default, then you physical path should look like:

C:\Users\Stanko Milošev\Documents\Visual Studio 2010\Projects\MvcApplication1\MvcApplication1

MvcApplication1 is mentioned twice.

Then in your host file (C:\Windows\System32\drivers\etc\hosts) you have to add also host name (in my case test) something like:

127.0.0.1      test

Also, don't forget to change App pool to .NET 4.0 like:

Or other logs.

In my local environment following code worked:

 

using System;
using System.Diagnostics;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.Diagnostics.Management;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace MvcWebRole1
{
    public class WebRole : RoleEntryPoint
    {
        public override void Run()
        {
            SetLogging();
            base.Run();
        }
        public override bool OnStart()
        {
            return base.OnStart();
        }

        protected virtual void SetLogging()
        {
            if (RoleEnvironment.IsAvailable)
            {
                string wadConnectionString = "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString";
                //CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=myAccountName;AccountKey=myAccountKey");
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");

                RoleInstanceDiagnosticManager roleInstanceDiagnosticManager = storageAccount.CreateRoleInstanceDiagnosticManager(RoleEnvironment.DeploymentId, RoleEnvironment.CurrentRoleInstance.Role.Name, RoleEnvironment.CurrentRoleInstance.Id);
                DiagnosticMonitorConfiguration config = DiagnosticMonitor.GetDefaultInitialConfiguration();

                config.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
                config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Undefined;
                roleInstanceDiagnosticManager.SetCurrentConfiguration(config);
            }
        }
    }
}

Part which is commented:

//CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=myAccountName;AccountKey=myAccountKey");

Is in use for the Azure, if you want to upload to Azure, then AccountName and AccountKey has to be changed, if anything from these two is wrong, Azure will not report a problem, but virtual machine will not be started.

Also, instead of

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");

It can be:

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(wadConnectionString));

Also, in local environment, if WadLogsTable is not populated, then reset Azure storage emulator (you will delete all tables and data you have in local storage), and also restart computer, obviously some service has to be restarted, but I don't know which one exactly... and wait at least 3 minutes, while application is started (debugged).

Only TraceError will be logged, and it should be somewhere in controller, because in OnStart will not work.

---

Another solution is this code

 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.Diagnostics.Management;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace MvcWebRole1
{
    public class WebRole : RoleEntryPoint
    {

        public override bool OnStart()
        {
            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

            // instantiate and add a new diagnostic monitor trace listener
            // before doing this, you should make sure no listeners are defined in your web.config or app.config
            var tempListener = new DiagnosticMonitorTraceListener();
            Trace.Listeners.Add(tempListener);

            // creates a diagnostic manager instance based on an azure storage account
            // before doing this, setup a storage account using the azure web portal
            string connectionStringName = "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString";
            var storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(connectionStringName));
            var roleInstanceDiagnosticManager = storageAccount.CreateRoleInstanceDiagnosticManager(RoleEnvironment.DeploymentId,
            RoleEnvironment.CurrentRoleInstance.Role.Name, RoleEnvironment.CurrentRoleInstance.Id);

            // setup the basic logging configuration options
            // ScheduledTransferPeriod is how often the trace log is copied to the storage account tables
            // ScheduledTransferLogLevelFilter is the filter on the messages to copy
            // BufferQuotaInMB is how much space is available to the local buffer (when limit is reached, old data is purged)
            var config = roleInstanceDiagnosticManager.GetCurrentConfiguration();
            config.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
            config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Undefined;
            config.Logs.BufferQuotaInMB = 5;

            // setup the windows event log configuration options
            // adds the System and Application level windows event logs to the sources
            // other options are the same as the basic logging
            config.WindowsEventLog.DataSources.Add("System!*");
            config.WindowsEventLog.DataSources.Add("Application!*");
            config.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
            config.WindowsEventLog.ScheduledTransferLogLevelFilter = LogLevel.Undefined;
            roleInstanceDiagnosticManager.SetCurrentConfiguration(config);
            return base.OnStart();
        }

    }
}

More details can be found here.

Lines of code:

            config.WindowsEventLog.DataSources.Add("System!*");
            config.WindowsEventLog.DataSources.Add("Application!*");

Will create Windows Event Logs, to create WADLogsTable I am not exactly sure which DataSource has to be added. This solution is using Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString, which can be found in ServiceConfiguration.Cloud.cscfg, or ServiceConfiguration.Local.cscfg, depending on which you need, local or cloud deployment.

Also, don't forget to set level of monitoring to verbose:

WADLogsTable

Cloud services -> Configure

---

Just to add my conclusion, actually both ways of creating WadLogsTable are working, and there is no really need for setting level of monitoring, only problem is that trace.Error has to be somewhere in controller (executed on some action), and we have to wait long enough (3 minutes for example)

---

To query WadLog table, using Visual Studio 2010, I can use query like:

PartitionKey ge '0635228657410000000' and Role eq 'MyRole' and Level le 3

Where number 0635228657410000000 is generated with this tool, that tool will generate number like:

635228656810000000, which means that I have to add additional 0 at beginning of that number

And Level is used to show only errors

Subcategories