Skip to content

Instantly share code, notes, and snippets.

@tylerd
Created April 15, 2012 22:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerd/2395186 to your computer and use it in GitHub Desktop.
Save tylerd/2395186 to your computer and use it in GitHub Desktop.
Azure Logging Part 1: Log file sync
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="logs\log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
using System;
using System.IO;
using System.Threading;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using log4net;
using log4net.Config;
namespace WorkerRole
{
public class WorkerRole : RoleEntryPoint
{
private ILog _logger;
public override void Run()
{
XmlConfigurator.Configure();
_logger = LogManager.GetLogger(GetType());
_logger.Info("Starting Worker Role");
while (true)
{
Thread.Sleep(10000);
_logger.Info("Waiting 10 Seconds");
}
}
public override bool OnStart()
{
var logFilePath = Path.Combine(Environment.CurrentDirectory, "logs");
var logDir = new DirectoryConfiguration
{
Container = "wad-log4net",
DirectoryQuotaInMB = 100,
Path = logFilePath
};
var diagnostics = DiagnosticMonitor.GetDefaultInitialConfiguration();
diagnostics.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
diagnostics.Directories.DataSources.Add(logDir);
CloudStorageAccount account = CloudStorageAccount.Parse(
RoleEnvironment.GetConfigurationSettingValue(
"Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"));
DiagnosticMonitor.Start(account, diagnostics);
return base.OnStart();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment