4
votes

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:

  1. Synchronizing a log file to blob storage using Windows Azure Diagnostics module.
  2. Using a custom log4net appender to write directly to table storage.
  3. 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 :)";
    }
}
3
what exactly did you do that didn't work? Provide concrete code and configuration settings that you have used. Also check this Question and Answerastaykov
@astaykov Here is what I configure, is there anything that I missing , because I can't seem to find any log ..mahoosh

3 Answers

1
votes

You can use AdoNetAppender with Azure SQL database and config like this example: http://logging.apache.org/log4net/release/config-examples.html

Notice: create Log table using this statement:

CREATE TABLE [dbo].[Log](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Date] [datetime] NOT NULL,
[Thread] [varchar](255) NOT NULL,
[Level] [varchar](50) NOT NULL,
[Logger] [varchar](255) NOT NULL,
[Message] [varchar](4000) NOT NULL,
[Exception] [varchar](2000) NULL,
CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED (
  [Id] ASC
))
1
votes

Re: log4net.Azure

The logs won't be visible as the implementation uses the BufferingAppenderSkeleton base class which has a buffer size of 512 by default. You will have to make the application create 513 logs entries in ram before they are flushed. I did it this way to make it more performant.

You have 3 options to make it work per your expectations in an MVC/ASP.NET environment:

  1. Change the buffer size in the config file
  2. Call flush (but only in debug mode, this is a performance killer)
  3. Call flush when your application shuts down so that it does an immediate write
0
votes

If you are using a full IIS in your webrole (which is the default configuration), the website and the webrole run in seperate processes.

Because of this you'll have to setup the logging twice. Once in the OnStart() of your WebRole, and once in the Application_Start() of your Global.asax.