Log4Net Not Logging When Deployed

14,559

Solution 1

If the directory and the file is not being created, then most likely, the configuration is not being read (and therefore used) at runtime.

I always forget to add the single line of code for Log4net that hooks up the configuration. This code usually appears in the bootstrap class in the application (e.g. Global.asax for an ASP.NET app).

XmlConfigurator.Configure(new System.IO.FileInfo(configFile));  // configFile being the path to the file.

Instead of the above in-line, you can add this attribute to the AssemblyInfo.cs file:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Either way, this will wire up log4net. More information is found in the Manual Configuration section of the log4net docs.

Solution 2

If you're using IIS, make sure the correct group has modify access to the Logs folder (usually IIS_USERS).

Solution 3

Sounds like a permissions issue to me. I almost always use a directory where I don't have to enable any special permissions for the applications to write the log files to.

Here is what I generally use with log4net:

<file type="log4net.Util.PatternString" value="${ALLUSERSPROFILE}/<Company Name>/Logs/<Program Name>/<Log file name>.txt" />

Of cource you'll need to substitute Company Name, Program Name and Log file name in the above with actual values.

This will write to the ProgramData folder where access is typically not restricted. You can navigate to this folder in File Explorer by typing %ProgramData% or %AllUsersProfile%

Another thing I like about this method is that it works on nearly every microsoft O/S. XP, Vista, 7, 8

Solution 4

You probably do not know where you are logging:

<file value="..\Logging\log.txt" />

Will is derived from your running directory, which is iis its directory. You can better use a full path like:

<file value="c:\Logging\log.txt" />

Then you can give the right access rights to the logging directory. Log4net will not create any directories as far as I know. So you have to create the c:\logging directory first.

Solution 5

Non of the answers worked for me until I put these lines to the web.config app settings:

<add key="log4net.Config" value="Log.config" />
<add key="log4net.Config.Watch" value="True" />
Share:
14,559

Related videos on Youtube

Phil Sandler
Author by

Phil Sandler

Senior Technical Architect working as an independent consultant in the Chicago area.

Updated on July 17, 2022

Comments

  • Phil Sandler
    Phil Sandler almost 2 years

    Using version 1.2.11. Logging works on my dev machine, but won't create the directory or log when deployed.

    I tried:

    • giving full directory access to IUSR, Everyone, local users.
    • running the app pool as a local admin account.
    • to use internal debugging as Phil Haack describes here.

    Stopped and started the app pool after each change.

    Nothing is produced in the output file.

    My log4Net config is below. Any thoughts on what to try next?

    <?xml version="1.0"?>
    <log4net debug="true">
      <appender name="file" type="log4net.Appender.RollingFileAppender">
        <file value="..\Logging\log.txt" />
        <appendToFile value="true" />
        <rollingStyle value="Composite" />
        <datePattern value="yyyyMMdd" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="1MB" />
        <threshold value="DEBUG" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
      </appender>
      <appender name="console" type="log4net.Appender.DebugAppender">
        <layout type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; - %m%n" />
        </layout>
      </appender>
      <root>
        <level value="ALL" />
        <appender-ref ref="file" />
        <appender-ref ref="console" />
      </root>
    </log4net>
    
    • Davin Tryon
      Davin Tryon
      It might be hiding in the AssemblyInfo.cs. Are you perhaps switching AssemblyInfo.cs when packaging? Also, I'm not sure if building for debug or release might effect it.
    • Davin Tryon
      Davin Tryon
      In code, are you setting up Log4Net to use XML configuration? Should be using the XmlConfigurator class somewhere.
  • Cole W
    Cole W almost 11 years
    I'm not sure how this explains him being able to log when he's in a development environment.
  • JebaDaHut
    JebaDaHut over 7 years
    I was beating my head against the wall on this one for a while. I tried using the AssemblyInfo.cs method, and that worked on my local dev box but not on the server. Once I added the line XmlConfigurator.Configure(); to the Global.asax.cs file, it worked!
  • silencedmessage
    silencedmessage over 7 years
    Same as @JebaDaHut - It was working on my dev machine using the AssemblyInfo.cs line, but not when deployed. After adding XmlConfigurator.ConfigureAndWatch(new FileInfo(....)) did it begin logging on my deployment servers.
  • user735232
    user735232 over 5 years
    This fixed my issue. But I only gave IIS_USRS read and write access (not execute).