As our applications move to the cloud, logging and telemetry are critical to success. If you’re using the Microsoft .NET toolset, it’s likely you’re building and hosting systems in Azure that rely on Azure worker roles. Debugging these worker roles is much easier when custom logs are available.
In our particular application, we used a worker role to maintain blob storage by periodically deleting outdated data. The role also needed to use a standardized logging assembly that was based on log4net (logging.apache.org). As a result of the difficulty in finding guidance for use of log4net in this somewhat more complicated build, I’m describing our solution in hopes of helping others with similar requirements.
To begin, use NuGet package manager to add Apache log4net to the project(s) in your solution that will use it (Install-Package log4net). Add appropriate references to log4net in your project(s).
Add a “log.config” file to the project from which log4net will be accessed (which may not be the main worker role project in your solution). See example below. Then place a reference in this project’s “assembly.info” file to the log4net config file. See example below.
One of the key concepts here is that we’re going to route log4net messages through Windows Azure worker role diagnostics using the TraceAppender that is available with log4net. Azure diagnostics sends its output to an Azure table named “WADLogsTable” in the storage account of your choosing. Note that it’s possible to write logs into the file system of the VM that is hosting your worker role, but you’ll need to jump through additional hoops to do so. We prefer the table method, since our log data is then readily accessible to other cloud services, and persists even if the host VM goes away.
Enable Azure diagnostics via your worker role’s properties in the cloud service project that publishes your worker role to Azure (not in the worker role project itself). Turn on “Enable Diagnostics” and enter the connection string of your storage account. Click “Configure…” to select the level of logging under the “General” tab. Note that other types of diagnostic logging can also be configured here.
Finally, enable Azure diagnostics for your worker role by placing a reference to Azure diagnostics in your worker role project’s “app.config” file. See example below.
To access your logs, download “Microsoft Azure Storage Explorer”. It’s a wonderful tool for exploring all types of Azure storage.
Some helpful references (our thanks to these authors!):
log.config file example:
<?xml version=”1.0″ encoding=”utf-8″?>
<level value=”ALL” />
<appender-ref ref=”TraceAppender” />
<appender name=”TraceAppender” type=”log4net.Appender.TraceAppender, log4net”>
<param name=”ImmediateFlush” value=”true” />
<conversionPattern value=”[%date] [%level] %logger %message” />
[assembly: log4net.Config.XmlConfigurator(ConfigFile = “log.config”, Watch = false)]
<add type=”Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=18.104.22.168, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ name=”AzureDiagnostics”>
<filter type=”” />
(Original post on January 12, 2017)