Step by step instructions to enable and view logging information in an Azure Cloud Service (Worker Role) using Log4Net.
Pre-requisites
- Visual Studio 2013 (Update 5)
- Microsoft Azure Tools v2.8
- An active Microsoft Azure subscription
Create Cloud Service Project
From Visual Studio 2013, create a new Project using the “Azure Cloud Service” template.
From the “New Microsoft Azure Cloud Service” dialog choose Visual C# and “Worker Role”.
Your solution should now contain 2 projects:
- a cloud service project
- a worker role project (called WorkerRole1 by default)
Add Log4Net package
Once created, use nuget package manager to add the Log4Net package. From the Package Manager console:
PM> Install-Package log4net -Version 2.0.5
Add some Log4Net logging code
Open “WorkerRole.cs” in the WorkerRole1 project and add a private instance of log4net logger:
private ILog _log = LogManager.GetLogger(typeof(WorkerRole));
Modify the RunAsync method as follows:
private async Task RunAsync(CancellationToken cancellationToken)
{
// TODO: Replace the following with your own logic.
while (!cancellationToken.IsCancellationRequested)
{
Trace.TraceInformation("Working");
_log.Info(" *********** log4net RunAsync *****************");
await Task.Delay(1000);
}
}
Add a log4net configuration file
Add a new configuration file to the WorkerRole1 project called “log.config”:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<param name="ImmediateFlush" value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="TraceAppender" />
</root>
</log4net>
</configuration>
Note that the logging level is set to ALL.
Ensure the configuration file is copied to bin folder
In Solution Explorer click on “log.config” and in the Properties window set “Copy to Output Directory” to “Copy Always”
Configure Log4Net in the WorkerRole project to use the new configuration
In the WorkerRole1 project open “AssemblyInfo.cs” and add the following line:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log.config", Watch = false)]
Test in development mode
Build the project, right click the cloud service project in Server Explorer and select “Debug” -> “Start new instance”
In the Output window you should see your log4net messages.
Preparing to Deploy to Azure
In Azure, create a new storage account for storing the diagnostics.
In your cloud service project open the “Roles” folder and right-click “WorkerRole1” and select “Properties”.
In the WorkerRole1 configuration pane, ensure that “Enable Diagnostics” is ticked.
Then click the “Configure…” button. On the “General” tab select “All Information” and close the dialog.
Then from the WorkerRole1 configuration pane, click the “…” button next to the storage account credentials text box. From the “Create Storage Connection String” dialog select your Azure account subscription and the storage account name you created at beginning of this section.
Publishing to Azure
Right-click your Azure Cloud Service project and select Publish.
Go through the wizard and publish the service.
Viewing logs
From Visual Studio open Server Explorer and connect to your Azure subscription.
Under the “Storage” node you will see the storage accounts one of which will be the one you have configured for diagnostics. Expand this node and then the “Tables” node.
The log4net messages will be written to a table called WADLogsTable. Right-click this table and “View Table”. Look in the “Message” column.