1

I have two almost identical web api services programmed in C# and installed on the same Windows 2008 Server on IIS 6.1. When I do web api calls to them, they both work just fine. I am using log4net for logging purposes. One of them, however, does not always log. They both have the same exact log4net configuration, which is:

<log4net>
 <root>
  <level value="DEBUG" />
  <appender-ref ref="RollingLogFileAppender" />
 </root>
 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="C:\LOGS\SomeFolder\" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <preserveLogFileNameExtension value="true" />
  <rollingStyle value="Date" />
  <datePattern value="'WebApi.One.'yyyy-MM-dd'.log'" />
  <staticLogFileName value="false" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5level [%thread][%date{dd-MM-yy HH:mm:ss,fff}] %logger - %message%newline" />
  </layout>
 </appender>
</log4net>

The only difference in the configuration is the datePattern which has a slightly different name so that they do not log to the same exact file.

For the web api where logging works as expected a new log file is created each day as soon as a web api call comes in. For the web api where logging does not seem to work the new log file is not created. However, if I make a web api call from a browser on the same server as it is installed on, then logging starts. After the logging has started for that day it continues fine (even with web api calls coming from other machines). But the next day no new file is created.

I am unable to see what the difference is. Surely there must be something I am not thinking of that makes these two web apis behave differently when it comes to logging. Remember, both of the services work fine, it is just the logging that is not working for one of them.

Any suggestions?

Edit 1:
After adding diagnostics as suggested by Peter I can see that the access to the path is denied:

log4net:ERROR Could not create Appender [RollingLogFileAppender] of type [log4net.Appender.RollingFileAppender]. Reported error follows. System.UnauthorizedAccessException: Access to the path 'C__LOGS_WebApi.One_' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.Threading.Mutex.MutexTryCodeHelper.MutexTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.Mutex.CreateMutexWithGuaranteedCleanup(Boolean initiallyOwned, String name, Boolean& createdNew, SECURITY_ATTRIBUTES secAttrs) at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name, Boolean& createdNew, MutexSecurity mutexSecurity) at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name) at log4net.Appender.RollingFileAppender.ActivateOptions() at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement) log4net:ERROR Appender named [RollingLogFileAppender] not found.

I don't know why though.

Edit 2:
I checked solutions in these StackOverflow answers:

But still no progress.

This looked very similar, but I could see no solution there (although maybe there is one).

Edit 3:
Folder permissions:

Folder permissions

Halvard
  • 3,891
  • 6
  • 39
  • 51
  • 1
    Change the permissions on the folder where the log file is stored so that the application has the rights to modify the log file(s) – Nkosi Oct 31 '17 at 11:41
  • @Nkosi The group Everyone is allowed everything. Should not that cover it? – Halvard Oct 31 '17 at 13:24
  • @Nkosi You were right. Everyone does noe seem to also cover IIS_IUSRS on this Windows Server (or maybe any?). Anyway, if you want the bounty just present your comment as an answer and I will award you. – Halvard Nov 03 '17 at 12:34

2 Answers2

1

It looks like a security issue, to debug this you should enable debugging for log4net:

<appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
</appSettings>

And

<system.diagnostics>
    <trace autoflush="true">
        <listeners>
            <add 
                name="textWriterTraceListener" 
                type="System.Diagnostics.TextWriterTraceListener" 
                initializeData="C:\tmp\log4net.txt" />
        </listeners>
    </trace>
</system.diagnostics>

in your configuration.

This way you are able to see why the creation of the file fails.

log4net faq

Peter
  • 27,590
  • 8
  • 64
  • 84
  • +1 for system diagnostics. I should have thought of that. Busy day at work, but will add this and see what the error is. Might be security, will give you the checkmark if so. – Halvard Oct 30 '17 at 13:32
1

According to the exception message, the process does not have enough rights to write to the location of the log file.

In my experience with dealing with IIS, changing the security permissions on the folder where the log file is stored so that the application/process has the rights to modify the log file(s) usually works.

To confirm/troubleshoot start by giving everyone full control on the folder and testing that the file can be modified.

If that works then you can confirm it is a permission issue.

I usually give IIS_IUSRS the following permission on the Log folder where I store my logs and archive.

  • Read
  • Write
  • Modify

To limit possible attack vectors I try to give the process as few permission as needed to perform its function. Adding and removing permissions and then testing that it still functions as desired.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Thanks. It works after doing this. Although I still do not understand why it logged when called from the same server (should it not still be IIS_IUSRS trying to log?). But that is not important for me right now :) – Halvard Nov 03 '17 at 15:11