esmehsnj
esmehsnj

Reputation: 162

Log4net not logging on Azure Webapp

For a web service, I have log4net configured and working fine locally but not when the webapp is deployed on Azure Webapp. The directory is created but there is nothing logged at the file..

Here is my config:

<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  <file value="log/sms.log" />
  <appendToFile value="true" />
  <maximumFileSize value="10MB" />
  <maxSizeRollBackups value="10" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %level - %message%newline" />
  </layout>
</appender>
<logger name="TheLogger">
  <appender-ref ref="RollingFile" />
</logger>

Within my AssemblyInfo.cs I have:

[assembly: XmlConfigurator(Watch = true)]

At the Startup class i have :

public static ILog Logger { get; private set; }

    public void Configuration(IAppBuilder appBuilder)
    {
        Logger = LogManager.GetLogger("TheLogger");
        Logger.Info("Application start...");
    }

Clearly my configuration is being picked up because in my log file is written the hour/date format and also Application start... but not the logging information. What am i missing?

Upvotes: 2

Views: 1323

Answers (2)

Mark
Mark

Reputation: 449

I had the same problem and found a solution. I had to replace these 2 lines of code in the Program class of the .Net Core API:

XmlDocument log4netConfig = new XmlDocument();
log4netConfig.Load(File.OpenRead("log4net.config"));

With this:

var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));

The reason is because in .Net Framework API's I used the assembly attribute to set this, now I need to do it like this.

Upvotes: 0

rodrigogq
rodrigogq

Reputation: 1953

Make sure you don't forget to add the root configuration. Are compiling for Debug and deploying in Azure in Release mode?

<log4net>
  <root>
    <level value="INFO" />
    <appender-ref ref="RollingFile" />
  </root>
  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
   <!-- ... -->
  </appender>
</log4net>

Upvotes: 0

Related Questions