Created
February 25, 2014 12:21
-
-
Save kaza/9207832 to your computer and use it in GitHub Desktop.
class which lets you configure log4net using blob storage, and monitors the changes of config blob
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
//To use this class call Log4NetBlobConfigure.ConfigureAndWatch(); in your Application.OnStart() | |
public class Log4NetBlobConfigure | |
{ | |
private static string _lastEtag; | |
private static DateTimeOffset? _lastModified; | |
private const string ContainerName = "config"; | |
private const string BlobUrl = "log4net.config"; | |
private static readonly object LockInstance = new object(); | |
private static readonly ILog log = LogManager.GetLogger(typeof(Log4NetBlobConfigure)); | |
public static void ConfigureAndWatch(int refreshPeriodInSeconds = 60) | |
{ | |
lock (LockInstance) | |
{ | |
LockedConfigureAndWatch(refreshPeriodInSeconds); | |
} | |
} | |
private static void LockedConfigureAndWatch(int refreshPeriodInSeconds) | |
{ | |
if (!Connections.IsStoreConnectionstringDefined) | |
{ | |
SysLogger.Fatal("could not configure as store connectino string is not defined "); | |
throw new ApplicationException("connection string for storage not defined"); | |
} | |
SysLogger.Info("Lock And configure called"); | |
log.Info("Lock And configure called"); | |
if (RefreshTimer != null) | |
RefreshTimer.Dispose(); | |
_repository = LogManager.GetRepository(Assembly.GetCallingAssembly()); | |
Configure(); | |
RefreshTimer = new Timer { Interval = refreshPeriodInSeconds * 1000 }; | |
RefreshTimer.Elapsed += CheckConfig; | |
RefreshTimer.Start(); | |
} | |
private static ILoggerRepository _repository; | |
protected static CloudStorageAccount StorageAccount { get; set; } | |
private static void CheckConfig(object sender, EventArgs e) | |
{ | |
var context = CloudStorageAccount.Parse(Connections.AbaloStoreConnectionstring); | |
var blobClient = context.CreateCloudBlobClient(); | |
CloudBlobContainer container = blobClient.GetContainerReference(ContainerName); | |
CloudBlockBlob blob = container.GetBlockBlobReference(BlobUrl); | |
blob.FetchAttributes(); | |
if ((_lastModified != null && _lastModified != blob.Properties.LastModified) || (_lastEtag != null && _lastEtag != blob.Properties.ETag)) | |
{ | |
SysLogger.Info("Reconfiguring using blob store"); | |
log.Info("Lock And configure called"); | |
Configure(); | |
} | |
} | |
private static void Configure() | |
{ | |
try | |
{ | |
DoConfigure(); | |
} | |
catch (Exception exception) | |
{ | |
log.Error("error in configure", exception); | |
SysLogger.Fatal("error reconfiguring", exception); | |
throw; | |
} | |
} | |
private static void DoConfigure() | |
{ | |
var context = CloudStorageAccount.Parse(Connections.AbaloStoreConnectionstring); | |
var blobClient = context.CreateCloudBlobClient(); | |
CloudBlobContainer container = blobClient.GetContainerReference(ContainerName); | |
CloudBlockBlob blob = container.GetBlockBlobReference(BlobUrl); | |
blob.FetchAttributes(); | |
_lastEtag = blob.Properties.ETag; | |
_lastModified = blob.Properties.LastModified; | |
var stream = new MemoryStream(); | |
blob.DownloadToStream(stream); | |
stream.Position = 0; | |
var result = log4net.Config.XmlConfigurator.Configure(_repository, stream); | |
foreach (object o in result) | |
{ | |
var loglog = o as LogLog; | |
if (loglog == null) | |
{ | |
SysLogger.Fatal("wierdness in logger" + o); | |
} | |
else | |
{ | |
SysLogger.Fatal("error configuring logger" + loglog.Message, loglog.Exception); | |
} | |
} | |
stream.Dispose(); | |
} | |
protected static Timer RefreshTimer { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment