在sharepoint下的log4net,错过了魔法

时间:2012-01-19 13:00:30

标签: sharepoint sharepoint-2010 log4net

我整个上午都花了很多时间来探讨在SharePoint 2010下进行设置时log4net没有写入文件的原因。

我已尝试过单独的log4net文件路由,但我目前正在尝试使用在web.config中配置它。

我已在web.config中将信任更新为medium,而log4net dll也在GAC中。当我断点时,我的记录器在启用所有日志记录时看起来很好,但是.Info()没有错误地通过,但是没有创建日志文件。

为了好运,我给了Everyone& asp.net app pool runas用户完全控制根日志文件夹。这是配置代码段和App_Start代码:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" />
    ...

    <log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="c:\temp\log.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="5MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
</appender>
  </log4net>




    private static ILog logger; 

    protected void Application_Start(object sender, EventArgs e)
    {
      ////This tells log4net where to go and look for its configuration file and also to watch it for any changes
      //log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Config\Moogle\Log4Net.config"));

      SPSecurity.RunWithElevatedPrivileges(delegate
        {
          log4net.Config.XmlConfigurator.Configure();

          logger = LogManager.GetLogger("Moogle.Web");
          logger.Info("Application_Start Fired");
        });
    }

修改 将我的配置更新到上面(请参阅我的回答),我仍然收到log4net消息:

log4net: XmlHierarchyConfigurator: Hierarchy Threshold []
log4net: Logger: No appenders could be found for logger [Moogle.Web] repository [log4net-default-repository]
log4net: Logger: Please initialize the log4net system properly.

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我知道log4net在SP2010上下文中非常棒且速度更快我强烈建议在ULS上编写自定义错误消息(现在SP2010中也有DB选项)。

好样品 http://sharepointnadeem.blogspot.com/search/label/SharePoint%20Diagnostic%20Service

答案 1 :(得分:1)

做了一些挖掘之后,我发现log4net的一个有用功能是让它自己做诊断。

您添加了以下应用设置:

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

以及以下部分:

<configuration>
    ...

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

    ...
</configuration>

我现在能够看到它显然是错误的配置,因为它正在报告:

log4net: XmlConfigurator: configuring repository [log4net-default-repository] using .config file section
log4net: XmlConfigurator: Application config file is [C:\inetpub\wwwroot\wss\VirtualDirectories\MOOGLEDEV0180\web.config]
log4net: XmlConfigurator: Configuring Repository [log4net-default-repository]
log4net: XmlHierarchyConfigurator: Configuration update mode [Merge].
log4net: XmlHierarchyConfigurator: Logger [root] Level string is [ALL].
log4net: XmlHierarchyConfigurator: Logger [root] level set to [name="ALL",value=-2147483648].
log4net: XmlHierarchyConfigurator: Hierarchy Threshold []
log4net: Logger: No appenders could be found for logger [Moogle.Web] repository [log4net-default-repository]
log4net: Logger: Please initialize the log4net system properly.

我一直以为这是权限......那么配置是什么?

编辑 - 工作配置

这是一个独立的log4net.config,可以正常工作(小心剪切和粘贴的地方!)

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821"/>
  </configSections>
  <log4net>
    <appender name="RollToFile" type="log4net.Appender.RollingFileAppender">
      <file value="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Logs\Moogle\Moogle.Web.Log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="5MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <!-- Value of priority may be ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
      <priority value="ALL"/>
      <appender-ref ref="RollToFile"/>
    </root>
  </log4net>
</configuration>