Log4net.Azure Configuration

Recently, we have moved our solution (ASP.NET MVC4) to Windows Azure and are still working well. Our only problem is that we cannot find the log files no matter what method we implement:

In fact, our existing application uses the log4net structure for logging purposes. Once we have moved our solution to Windows Azure, we still want to use log4net in azure with minimal changes to our existing code. We have completed many blogs and tutorials to implement the following methods:

  • Synchronize the log file with the memory block using the Windows Azure Diagnostic Module.
  • Using a custom log4net application to write directly to table storage.
  • Logging to the trace log and synchronizing with table storage.

Unfortunately, none of the above brought the desired result. We are still not able to access our magazines. Is there an official source on how to use Log4net with Windows Azure?

Step 1: I imported Log4net.Azure as a link to my MVC4 WebRole application

Step 2: I added the configuration lines to the On_Start method of the WebRole class

public class WebRole : RoleEntryPoint { private static readonly ILog _logger = LogManager.GetLogger(typeof(WebRole)); public override void Run() { _logger.InfoFormat("{0} 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(); } 

Step 3: I create an instance of ILog when I need to write a 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} entry point called", typeof(WebRole).Name); _logger.Debug("<<<<<<<<<< WS just invoked >>>>>>>>>>>>..."); return "hello world logs on Azure :)"; } } 
+4
source share
3 answers

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

Note: create a 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
source

Re: log4net.Azure

Logs will not be visible because the implementation uses the BufferingAppenderSkeleton base class with a default buffer size of 512. You will need to force the application to create 513 log entries in the ram before they are cleared. I did it this way to make it more efficient.

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

  • Change the buffer size in the configuration file
  • Flash call (but only in debug mode, this is a performance killer)
  • Flash call when the application closes, so that it records immediately
+1
source

If you use full IIS in your webrole (which is the default configuration), the website and the web role work in separate processes .

Because of this, you have to tweak the log twice. Once in OnStart () of your WebRole and once in Application_Start () of your Global.asax.

0
source

Source: https://habr.com/ru/post/1490476/


All Articles