简体   繁体   English

部署时Log4Net不记录

[英]Log4Net Not Logging When Deployed

Using version 1.2.11 . 使用版本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. 提供对IUSR,Everyone,本地用户的完整目录访问权限。
  • running the app pool as a local admin account. 将应用程序池作为本地管理员帐户运行。
  • to use internal debugging as Phil Haack describes here . Phil Haack所述 ,使用内部调试。

Stopped and started the app pool after each change. 每次更改后停止并启动应用程序池。

Nothing is produced in the output file. 输出文件中没有生成任何内容。

My log4Net config is below. 我的log4Net配置如下。 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. 我总是忘记为Log4net添加连接配置的单行代码。 This code usually appears in the bootstrap class in the application (eg Global.asax for an ASP.NET app). 此代码通常出现在应用程序的bootstrap类中(例如,ASP.NET应用程序的Global.asax)。

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: 您可以将此属性添加到AssemblyInfo.cs文件中,而不是上面的内联:

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

Either way, this will wire up log4net. 无论哪种方式,这将连接log4net。 More information is found in the Manual Configuration section of the log4net docs . 有关详细信息,请参见log4net文档的“ 手动配置”部分

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: 这是我通常用于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. 对于cource,您需要使用实际值替换上面的公司名称,程序名称和日志文件名称。

This will write to the ProgramData folder where access is typically not restricted. 这将写入ProgramData文件夹,其中访问通常不受限制。 You can navigate to this folder in File Explorer by typing %ProgramData% or %AllUsersProfile% 您可以通过键入%ProgramData%或%AllUsersProfile%在文件资源管理器中导航到此文件夹

Another thing I like about this method is that it works on nearly every microsoft O/S. 我喜欢这种方法的另一件事是它几乎适用于所有的微软操作系统。 XP, Vista, 7, 8 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. Will来自您的运行目录,即iis的目录。 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. 据我所知,Log4net不会创建任何目录。 So you have to create the c:\\logging directory first. 所以你必须先创建c:\\ logging目录。

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

Non of the answers worked for me until I put these lines to the web.config app settings: 在我将这些行放到web.config应用程序设置之前,没有一个答案适合我:

<add key="log4net.Config" value="Log.config" />
<add key="log4net.Config.Watch" value="True" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM