Answered Using log4net in windows azure??

  • Tuesday, February 08, 2011 11:41 AM
     
     
    We have an Existing Application that we are migrating to Windows Azure Platform .We have used log4net for logging in the application level.Can anyone suggest some workarounds or solution on  how to use log4net in azure platform and the config changes in the Existing application that need to be done while using log4net in azure.


All Replies

  • Tuesday, February 08, 2011 11:59 AM
     
     

    Hi Chandan_panda,

    Have a look at

    http://social.msdn.microsoft.com/Forums/en/windowsazure/thread/4937172d-866f-48fc-9bdf-05ace25bd87e

     http://www.jdconley.com/blog/archive/2009/02/02/abstracting-away-azure-how-to-run-outside-of-the-cloud.aspx

    I this explain how you can make you own log4net appender and let you hook into the azure diagnostics infrastructure.

     

     

    Hans

     

  • Tuesday, February 08, 2011 8:15 PM
     
     

    Hi,

    In addition to what Hans said here's a nice article that explains how to create a custom log4net appender to log in Windows Azure storage.

     


    With regards,

    Patriek
    If this reply is of help to you, please don't forget to mark it as an answer.
  • Wednesday, February 09, 2011 9:48 AM
     
     

    hi hans ,

    I already went through the links that u have provided they did not helped me much.I am giving the steps that i followed to make a POC on log4net in azure.Please suggest me if there is any other workaround.

    1.I have created Webrole project and added log4net reference to it.

    2.In the Web.config file I added the following changes

    <log4net>
        <appender name="Appender" type="DebugAppender,MyAssembly">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%logger - %message" />
          </layout>
        </appender>

        <root>
          <level value="ALL" />
          <appender-ref ref="azure" />
        </root>
      </log4net>

    3.And have set the  DiagnosticMonitor and log4net in OnStart method of WebRole.cs.

    4.My Appender class is as follows:

     public class DebugAppender : AppenderSkeleton
        {
            protected override void Append(LoggingEvent loggingEvent)
            {
                System.Diagnostics.Debug.WriteLine(loggingEvent.Level.Name,       loggingEvent.RenderedMessage);
            }
       
        }

    but still it does not work.Can u suggest me any specific steps that would help.

    links  reffered for above POC:http://www.gine.jp/blog/taka/post/Azure-e381a6-log4net-e38292e4bdbfe38186.aspx

    and http://social.msdn.microsoft.com/Forums/en/windowsazure/thread/4937172d-866f-48fc-9bdf-05ace25bd87e



  • Wednesday, February 09, 2011 9:56 AM
     
     

    Hi,

    Does your role deployment fail or does it just not log anything?

    You could take a totally different approach by leaving the logging as it was in your on-premises app. You can instructuct DiagnosticMonitor to periodically transfer Directories to blob storage. Neil MacKenzie wrote a nice article about the subject.

     


    With regards,

    Patriek
    If this reply is of help to you, please don't forget to mark it as an answer.
  • Wednesday, February 09, 2011 9:56 AM
     
     

    HI,

     

    Wat exactly is not working?

    Do you have diagnostics to begin with?

    How are diagnostics configured, with what level?

    Are the statements not showing up in your diagnosticstrace in blob storage?

    Could you provide the config voor diagnostics?

     

  • Wednesday, February 09, 2011 10:57 AM
     
     

    Hi Patriek

    Its Not logging any thing And can u say me what exactly i have to change in the web.config of a webrole Application.And i am using the local development storage for now.this is my diagnostic on start code:

      CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
                {
                    configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));

                    RoleEnvironment.Changed += (sender, arg) =>
                    {
                        if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>()
                            .Any((change) => (change.ConfigurationSettingName == configName)))
                        {
                            if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
                            {
                                RoleEnvironment.RequestRecycle();
                            }
                        }
                    };
                });

                // DiagnosticMonitor
                DiagnosticMonitorConfiguration dmc = DiagnosticMonitor.GetDefaultInitialConfiguration();
                dmc.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
                dmc.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
                DiagnosticMonitor.Start("DiagnosticsConnectionString", dmc);
                RoleEnvironment.Changing += RoleEnvironmentChanging;

                // log4net
                string log4netConfig = RoleEnvironment.GetConfigurationSettingValue("log4netConfig");
                log4net.Config.XmlConfigurator.Configure(new System.Uri(log4netConfig));

                return base.OnStart();



    • Edited by chandan_panda Wednesday, February 09, 2011 11:02 AM
    •  
  • Wednesday, February 09, 2011 11:01 AM
     
     

    Hi,

    In your web.config set a rollingFile appender to log to a directory on your WebRole instance. You need to configure LocalStorage to use that directory. Neil explains in his post how you can use LocalStorage and the DiagnosticMonitorConfiguration to transfer the logfiles, created by log4net in a standard manner, to Blob Storage.

    You have to pay attention though to the fact the Diagnostics has changed since Neil's post, but he has written more posts on how to deal with the changes. The main idea is there.


    With regards,

    Patriek
    If this reply is of help to you, please don't forget to mark it as an answer.
  • Wednesday, February 09, 2011 11:04 AM
     
     

    i have followed these links:

    http://www.gine.jp/blog/taka/post/Azure-e381a6-log4net-e38292e4bdbfe38186.aspx

    and http://social.msdn.microsoft.com/Forums/en/windowsazure/thread/4937172d-866f-48fc-9bdf-05ace25bd87e for the above POC

    what is the link for the rolling Appender?



  • Wednesday, February 09, 2011 11:09 AM
     
     

    i have followed these links:

    http://www.gine.jp/blog/taka/post/Azure-e381a6-log4net-e38292e4bdbfe38186.aspx

    and http://social.msdn.microsoft.com/Forums/en/windowsazure/thread/4937172d-866f-48fc-9bdf-05ace25bd87e for the above POC


    chandan123

    LOL! If I follow the first link I don't think I'd figure anything out until I've studied Japanese ;-).

    Anyway, the link I provided earlier was not about getting the custom log4net appender to work, but more like an alternative to get your problem soved.


    With regards,

    Patriek
    If this reply is of help to you, please don't forget to mark it as an answer.
  • Wednesday, February 09, 2011 11:30 AM
     
     

    Please check to see if you diagnostics are configured correctly, do so by outputting a statement directly to the diagnostics trace

     

    System

     

     

    .Diagnostics.Trace.TraceInformation("TESTTEST"); see if this shows up.

     

    If diagnostics are up it's likely log4net isn't started in the proper appdomain

    From sdk 1.3 there are two appdomains due to the full iis option added see http://azurefeeds.com/post/1622/New_Full_IIS_Capabilities_Differences_from_Hosted_Web_Core_.aspx

    so you            

     // log4net
                string log4netConfig = RoleEnvironment.GetConfigurationSettingValue("log4netConfig");
                log4net.Config.XmlConfigurator.Configure(new System.Uri(log4netConfig));
    Should be in the application start event in the global asax

  • Wednesday, February 09, 2011 12:04 PM
     
     Answered Has Code

    Hi,

    This is my config which works for me:

     in your webrole:

     

     public override bool OnStart()
     {
            var config = DiagnosticMonitor.GetDefaultInitialConfiguration();
          config.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
          config.Logs.ScheduledTransferPeriod = TimeSpan.FromSeconds(20);
          config.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromSeconds(20);
          config.WindowsEventLog.ScheduledTransferLogLevelFilter = LogLevel.Verbose ;
    
          DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config); 
    
          return base.OnStart();
    }
    

    in you global.asax.cs

    void Application_Start(object sender, EventArgs e)
    {
          CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSettingPublisher) =>
            {
              var connectionString = RoleEnvironment.GetConfigurationSettingValue(configName);
              configSettingPublisher(connectionString);
            }
          );
          // Code that runs on application startup
          var appender = new AzureAppender();
          appender.ActivateOptions();
          BasicConfigurator.Configure(appender);
    }
    

     

     

  • Thursday, February 10, 2011 12:03 PM
     
     

    HI Hans

    Now my Diagnostic monitor is working fine but When i am logging using log4net I am getting an error.

    "The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

    I have made the following change in Web.config for log4net

     <config>
          <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
        </config>
        <appSettings></appSettings>
        <log4net>
          <appender name="azure" type="AzureRoleManagerAppender,MyAssembly">
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%logger - %message" />
            </layout>
          </appender>
          <root>
            <level value="ALL" />
            <appender-ref ref="azure" />
          </root>
        </log4net>

    Is there any kind of change that is required in the Web.config of the application for log4net?

     

     


  • Thursday, February 10, 2011 12:09 PM
     
     

    Hi Chandan,

    Thats something I get when I leave my web.config checked in and then its read-only, my service fail to start and its in a faulted state. So make sure your web.config is editable when running on the dev fabric.

    Hans

  • Thursday, February 10, 2011 12:52 PM
     
     
    What changes should be made to the Web.config file for Log4net.Is any change  required?
    i have shared my web.config change above.And how to test the logging data in development storage.

  • Thursday, February 10, 2011 1:09 PM
     
     

    Hi Chandan,

    Make the web.config editable when publishing to development fabric. That should resolve the faulted state error.

    As for you config, it looks ok to me. If configured it by code see my earlier reply (just for testing purposes).

    Hans

     

     

  • Friday, February 11, 2011 6:54 AM
     
     

    Hi Hans,

     

    I have also same problem .Can you please share the code .

    It will be very helpful .

     

    Thanks in advance.

  • Friday, February 11, 2011 12:04 PM
     
     

    Hi Prasanna,

    Please describe you exact problem, because I think Chandan could have serveral.

    If you have a faulted state during development fabric start, check to see if you web.config is editable in other words it has the Read-only flag turn OFF.

    I you still have the same error (faulted state) enable WCF diagnostics and view log files/

    As for code see my comment in this thread: Wednesday, February 09, 2011 12:04 PM

     

  • Monday, February 14, 2011 11:49 AM
     
     

    Hi Chandan

    check the below link(you need to convert the content of the link into english :))

    http://www.gine.jp/blog/taka/post/Azure-e381a6-log4net-e38292e4bdbfe38186.aspx

     

    after configurring your application you need to create a custome appender

    like this

     <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
          <layout type="log4net.Layout.PatternLayout" >
            <conversionPattern value="%-5p%d{yyyy-MM-dd hh:mm:ss} [%thread] – %m%n" />
          </layout>
        </appender>

     

  • Thursday, March 08, 2012 10:09 AM
     
     

    Hi

    Can any one suggest how we can use the email services when we get the error in the application from in the Azure platform using Log4Net.

    Usually we configure the Log4Net with SMTPAppender to listen and set the configuration level to Error in for Log4Net to get the emails , when any unhandled exception occured or for some other reasons. 

    Can any one have any idea or able to configure like this as given below and getting the emails if any error occured, after hosting the application on Windows Azure.

    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
                <to value="to@to.come"/>
                <from value="from@from.com"/>
                <subject value="Log Message"/>
                <smtpHost value="SMPTAddress"/>
                <bufferSize value="1"/>
                <lossy value="false"/>
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%d [%t] %-5p %c [%property{UserId}] %m%n"/>
                </layout>
                <filter type="log4net.Filter.LevelRangeFilter">
                    <levelMin value="ERROR"/>
                    <levelMax value="ERROR"/>
                </filter>
    </appender>
            <root>
                <level value="TRACE"/>
               <appender-ref ref="SmtpAppender"/>
            </root>

    Already i have gone through the below articles , but I am trying to find out is there any alternative available using Log4Net with out implementing the custom solutions to achieve this task.

    http://blogs.msdn.com/b/windowsazure/archive/2010/10/08/adoption-program-insights-sending-emails-from-windows-azure-part-1-of-2.aspx?wa=wsignin1.0

    Thanks in Advance.

  • Monday, January 21, 2013 11:14 AM
     
     

    Hi,

     am also phasing this problem.

    Regards,

    PCS