0

I've got the following entries in my Web.config file in an asp.net mvc application:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    ...
  </configSections>

  <log4net>
    <appender name="PublicAccessAppender" type="log4net.Appender.RollingFileAppender">
      <datePattern value="'C:\Users\my_user_name\Documents\Temp\logs\public-access.'yyyy-MM-dd'.log'" />
      <staticLogFileName value="false" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <maxSizeRollBackups value="5" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="PublicAccessAppender" />
    </root>
  </log4net>

  ...
</configuration>

I hope it's pretty self-explanatory what I'm trying to achieve, but when I run the application (hosted in IIS), I get no log file. FWIW, the directory hierarchy exists up to Temp folder, and I'd like log4net to generate the rest of the directories/files in the path.

I've added the log4net nuget package to my application, and I'm logging with the INFO level.

What am I missing here?

gzak
  • 3,908
  • 6
  • 33
  • 56
  • How are you configuring log4net? – stuartd Feb 01 '16 at 21:41
  • Actually, I'm not configuring log4net at all, and I believe that's the source of my problems. – gzak Feb 01 '16 at 21:42
  • Add a call to `XmlConfigurator.Configure()` in your startup method – stuartd Feb 01 '16 at 21:43
  • I thought for sure that would fix it, but still no dice. I even tried to turn on configuration debugging, and even that isn't working. It's just silently sailing past everything I try to do. – gzak Feb 01 '16 at 22:06
  • 1
    You can [check for configuration errors](https://logging.apache.org/log4net/release/faq.html#trouble-evaluate-configurationerrors-at-runtime) after configuring log4net – stuartd Feb 01 '16 at 22:43
  • The outer `if` condition from that example returns false, meaning it thinks everything was configured properly without errors. I'm pretty much giving up at this point and switching to NLog, I'm not at all attached enough to log4net to continue pulling my (or others') hair out like this. – gzak Feb 01 '16 at 22:54
  • Your date pattern is invalid, @zbynek-vyskovsky-kvr000's answer is correct. However the log4net example of checking for configuration errors I linked to above a) doesn't compile and b) is fundamentally unhelpful - as the documentation states, the `Configured` property returns _whether log4net has been configured_ - which would have been useful in your situation. The link suggests it should return false if there were configuration errors but that is not the case. – stuartd Feb 02 '16 at 00:20
  • :face_palm: it was a permission issue. I just added `log4net.LogManager.GetRepository().ConfigurationMessages` in my watch window (circumventing the `if` check), and I was able to see the detailed debug logs (I also enabled log4net debugging). One of the many entries was a file open permission error. – gzak Feb 02 '16 at 00:23

2 Answers2

1

I think you can't put full path into datePattern, there must be just YYYYmmdd and things like that. Put the file path into file element:

<file value="C:\Users\my_user_name\Documents\Temp\logs\public-access.log" />
<datePattern value="yyyyMMdd" />
<preserveLogFileNameExtension value="true" />

The last element forces to put datePattern before the .log extension which was probably your original goal..

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
  • This is the correct answer - the DatePattern only accepts a date pattern. OP was not configuring log4net as in the comments above. However if the file values is changed to `` then the output file name still has the date in it. +1 too for `preserveLogFileNameExtension` – stuartd Feb 02 '16 at 00:12
0

Here's the working appender configuration:

<appender name="PublicAccessAppender" type="log4net.Appender.RollingFileAppender">
  <file value="C:\temp\my_user_name\Documents\Temp\logs\app.log" />
  <datePattern value=".yyyy-MM-dd" /><!-- can be omitted as it's the default datePattern value -->
  <rollingStyle value="Date" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
</appender>

Note to readers struggling with log4net configuration: asp.net hosted in IIS pretty much has 0 write permssions, so the issue you're most likely struggling with is that your web app simply doesn't have permission to write to the log file.

That's what was happening for me, and I noticed it by inspecting the contents of log4net.LogManager.GetRepository().ConfigurationMessages in the debugger after calling .Configure().

I followed this post to give my web app the necessary persmissions to write to the log file (namely Read/Write/Modify).

Community
  • 1
  • 1
gzak
  • 3,908
  • 6
  • 33
  • 56