简体   繁体   中英

Log4net configuration is not working in production doe azure web services

I am using log4net for logging in my webapi. logging is working fine in my localhost or while i am deploying it on a app server. Same thing is not working in azure web service. Folder where log file has to be created is there but no files in there.
i have added below section in my web.config file,

<log4net>
<appender name ="ErrorLog" type ="log4net.Appender.RollingFileAppender">
  <file value ="MyLogs\"/>
  <staticLogFileName value ="false" />
  <appendToFile value ="true"/>
  <rollingStyle value ="Date"/>
  <datePattern value ="yyyy-MM-dd.'Err'" />
  <lockingModel type ="log4net.Appender.FileAppender+MinimalLock"/>
  <layout type ="log4net.Layout.PatternLayout">
    <conversionPattern value ="%d{DATE} [%t] %-5p %c - %m%n" />
  </layout>
</appender>
<logger name ="ErrorLog">
  <maximumFileSize value ="15MB" />
  <appender-ref ref="ErrorLog"/>
</logger>
</log4net>

It seems that there are some mistake with log4net configuration.

Here I have a complete steps you could follow with and it works well on my site:

1.Install the log4net.dll binary using NuGet.

2.Configure the log4net name, type properties in the web.config

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>
</configuration>

3.Configure the log4net properties

<log4net>
   <root>
     <level value="Debug"/>
     <appender-ref ref="LogFileAppender"/>
   </root>
   <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
     <param name="File" value="D:\Home\LogFiles\Log4Net\Log4Net.log"/>
     <param name="AppendToFile" value="true"/>
     <rollingStyle value="Size"/>
     <maxSizeRollBackups value="10"/>
     <maximumFileSize value="3MB"/>
     <staticLogFileName value="true"/>
     <layout type="log4net.Layout.PatternLayout">
       <param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
     </layout>
   </appender>
   <logger name="SleepyCore">
     <level value="INFO"/>
   </logger>
 </log4net>

Note : You need to specify the address of the log on azure like D:\\Home

4.Modify the Global.asax Application_Start() method add the following code in the method.

log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));

5.Write log with an instance of the ILog interface

ILog log = LogManager.GetLogger("SleepyCore");
            log.Info("Begin - Page_Load() at " + DateTime.Now.ToString("hh.mm.ss.ffffff"));

6.The output as below:

在此处输入图片说明 在此处输入图片说明

For more details about how to configure log4net on azure, you could refer to this article .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM