简体   繁体   中英

Log4Net Not Logging When Deployed

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>

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 (eg 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 .

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

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.

如果您使用的是IIS,请确保正确的组具有对Logs文件夹(通常是IIS_USERS)的修改权限。

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" />

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