应用程序不记录

时间:2009-12-15 16:45:13

标签: asp.net-mvc model-view-controller logging web-config log4net

我有许多使用log4net的ASP.NET应用程序没有问题。我无法使用log4net来记录我使用MVC库编写的任何服务中的任何内容。我在所有服务中都具有相同的配置(除了日志文件名)。每个服务都作为同一个用户运行,并使用相同的应用程序池。

以下是我使用的配置:

<log4net>
        <appender name="XMLAppender" type="log4net.Appender.RollingFileAppender">
            <file value="C:\logs\SERVICENAME.Log.xml"/>
            <appendToFile value="true"/>
            <rollingStyle value="Composite"/>
            <datePattern value="yyyyMM"/>
            <maxSizeRollBackups value="10"/>
            <maximumFileSize value="100MB"/>
            <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
                <locationInfo value="false"/>
            </layout>
    </appender>
        <root>
            <level value="INFO"/>
            <appender-ref ref="XMLAppender"/>
        </root>
    </log4net>

我总共有configSection条目,如下所示:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
...
</configSections>

我还打开了log4net调试并设置system.diagnostics将跟踪写入文件。在这里我看到了MVC和非MVC应用程序之间输出的差异。

MVC:

log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\MVCSERVICENAME\25c1b478\883641ac\assembly\dl3\707a9005\e1887c7f_5040ca01\log4net.DLL]. (.NET Runtime [2.0.50727.3074] on Microsoft Windows NT 6.0.6002 Service Pack 2, v.286)
log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository for assembly [MVCSERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net: DefaultRepositorySelector: Assembly [MVCSERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\MVCSERVICENAME\25c1b478\883641ac\assembly\dl3\13a19b30\fdb642f4_937dca01\MVCSERVICENAME.DLL]
log4net: DefaultRepositorySelector: Assembly [MVCSERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: DefaultRepositorySelector: Assembly [MVCSERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
log4net: Hierarchy: Shutdown called on Hierarchy [log4net-default-repository]

非MVC(工作):

log4net log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\SERVICENAME\33034378\c825df91\assembly\dl3\fe97001a\e1887c7f_5040ca01\log4net.DLL]. (.NET Runtime [2.0.50727.3074] on Microsoft Windows NT 6.0.6002 Service Pack 2, v.286)
log4net DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net DefaultRepositorySelector: Creating repository for assembly [SERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net DefaultRepositorySelector: Assembly [SERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\SERVICENAME\33034378\c825df91\assembly\dl3\827ed068\1a38e2eb_fc7cca01\SERVICENAME.DLL]
log4net DefaultRepositorySelector: Assembly [SERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net DefaultRepositorySelector: Assembly [SERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
log4net XmlConfigurator: configuring repository [log4net-default-repository] using .config file section
log4net XmlConfigurator: Application config file is [C:\content\SERVICENAME\web.config]
log4net XmlConfigurator: Configuring Repository [log4net-default-repository]
log4net XmlHierarchyConfigurator: Configuration update mode [Merge].
log4net XmlHierarchyConfigurator: Logger [root] Level string is [INFO].
log4net XmlHierarchyConfigurator: Logger [root] level set to [name="INFO",value=40000].
log4net XmlHierarchyConfigurator: Loading Appender [XMLAppender] type: [log4net.Appender.RollingFileAppender]
log4net XmlHierarchyConfigurator: Setting Property [File] to String value [C:\Windows\Temp\SERVICENAME.Log.xml]
log4net XmlHierarchyConfigurator: Setting Property [AppendToFile] to Boolean value [True]
log4net XmlHierarchyConfigurator: Setting Property [RollingStyle] to RollingMode value [Composite]
log4net XmlHierarchyConfigurator: Setting Property [DatePattern] to String value [yyyyMM]
log4net XmlHierarchyConfigurator: Setting Property [MaxSizeRollBackups] to Int32 value [10]
log4net XmlHierarchyConfigurator: Setting Property [MaximumFileSize] to String value [100MB]
log4net XmlHierarchyConfigurator: Setting Property [LocationInfo] to Boolean value [False]
log4net XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.XmlLayoutSchemaLog4j]
log4net RollingFileAppender: Type = [0], r0 = [197001], r1 = [197001]
log4net RollingFileAppender: Type = [1], r0 = [197001], r1 = [197001]
log4net RollingFileAppender: Type = [2], r0 = [197001], r1 = [197001]
log4net RollingFileAppender: Type = [3], r0 = [197001], r1 = [197001]
log4net RollingFileAppender: Type = [4], r0 = [197001], r1 = [197001]
log4net RollingFileAppender: Type = [5], r0 = [197001], r1 = [197002]
log4net RollingFileAppender: Searched for existing files in [C:\Windows\Temp]
log4net RollingFileAppender: curSizeRollBackups starts at [0]
log4net RollingFileAppender: [200912] vs. [200912]
log4net FileAppender: Opening file for writing [C:\Windows\Temp\SERVICENAME.Log.xml] append [True]
log4net XmlHierarchyConfigurator: Created Appender [XMLAppender]
log4net XmlHierarchyConfigurator: Adding appender named [XMLAppender] to logger [root].
log4net XmlHierarchyConfigurator: Hierarchy Threshold []
log4net DefaultRepositorySelector: Creating repository for assembly [SERVICENAME.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net DefaultRepositorySelector: Assembly [SERVICENAME.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\SERVICENAME\33034378\c825df91\assembly\dl3\d74dc9d5\8aa6a7e8_fc7cca01\SERVICENAME.Core.DLL]
log4net DefaultRepositorySelector: Assembly [SERVICENAME.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net DefaultRepositorySelector: Assembly [SERVICENAME.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net DefaultRepositorySelector: repository [log4net-default-repository] already exists, using repository type [log4net.Repository.Hierarchy.Hierarchy]

在这两种情况下,我都按如下方式创建ILog对象:

private static readonly ILog log = LogManager.GetLogger(typeof(CLASSNAME));

我是否缺少某些东西或需要做些什么改变才能让它适用于MVC?此时,如果已知可以正常使用MVC,我也愿意更改为另一个日志记录解决方案。

由于 添

6 个答案:

答案 0 :(得分:10)

我知道这篇文章已经很老了,但我也在WCF项目中遇到了这个问题(默认的log4net存储库)。 我使用带有log4net部分的web.config(不是单独的log4net.config文件)。

为了解决这个问题,我在AssemblyInfo.cs中放置了以下内容:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]

如果使用外部配置文件也是如此。 HTH, 夏兰

答案 1 :(得分:4)

我的Web应用程序是一个ASP.NET MVC,在Web根目录中有一个单独的log4net.config文件。

经过大量的谷歌搜索和拔毛后,我通过 Application_Start (在Global.asax中)中的代码初始化log4net:

protected void Application_Start()
{
    var logConfigFilePath = Server.MapPath("~/log4net.config");

    log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(logConfigFilePath));
}

这解决了我的问题,log4net开始正确记录。

答案 2 :(得分:0)

使用NLog。似乎工作。我必须将NLog.config文件放在基本目录中(与web.config一起),它运行正常。能够写入正确的目录,这证明了目录权限设置正确。大约需要5分钟才能完成更改。

很高兴知道为什么log4net拒绝播放。

答案 3 :(得分:0)

答案 4 :(得分:0)

就我而言,这是因为项目属性设置为输出到bin \ debug,但log4net dll要求log4net.config文件存在于BIN文件夹中。我将项目属性更改为输出到bin文件夹,然后将log4net.config文件在构建时复制到正确的位置。

答案 5 :(得分:0)

此外,确保为log4net配置文件设置“copy local”也可以防止出现许多问题。