Created
April 15, 2012 22:45
-
-
Save tylerd/2395186 to your computer and use it in GitHub Desktop.
Azure Logging Part 1: Log file sync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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