Recently, we moved our solution (ASP.NET MVC4) to Windows Azure and so far, it is working fine. Our only concern is that we are not able to locate our logs files no matter what method we do implement:
Actually, our existing application uses log4net framework for Logging purpose. Once we have moved our solution on Windows Azure, we still want to use log4net in azure with minimal change in our existing code. We have followed many blogs and tutorials in order to implement the following methods:
- Synchronizing a log file to blob storage using Windows Azure Diagnostics module.
- Using a custom log4net appender to write directly to table storage.
- Logging to the Trace log and synchronizing to table storage.
Unfortunatly, none of the above has delivered the desired result. We are still not able to get access to our logs. Is there any official source about how to use Log4net with Windows Azure?
Step1: I imported Log4net.Azure as a reference to my MVC4 WebRole application
Step2: I added configuration lines in the On_Start method of WebRole class
public class WebRole : RoleEntryPoint
{
private static readonly ILog _logger = LogManager.GetLogger(typeof(WebRole));
public override void Run()
{
_logger.InfoFormat("{0}'s entry point called", typeof(WebRole).Name);
while (true)
{
Thread.Sleep(10000);
_logger.Debug("Working...");
}
}
public override bool OnStart()
{
BasicConfigurator.Configure(AzureAppender.New(conf =>
{
conf.Level = "Debug";
conf.ConfigureRepository((repo, mapper) =>
{
repo.Threshold = mapper("Debug"); // root
});
conf.ConfigureAzureDiagnostics(dmc =>
{
dmc.Logs.ScheduledTransferLogLevelFilter = LogLevel.Information;
});
}));
return base.OnStart();
}
Step3: I create an instance of ILog whenevr I need to log, here is an example:
public class TestController : ApiController
{
private static readonly ILog _logger = LogManager.GetLogger(typeof(WebRole));
[HttpGet]
public String Get()
{
_logger.InfoFormat("{0}'s entry point called", typeof(WebRole).Name);
_logger.Debug("<<<<<<<<<< WS just invoked >>>>>>>>>>>>...");
return "hello world logs on Azure :)";
}
}