应用程序不记录

发布于 2024-08-15 03:09:39 字数 7697 浏览 9 评论 0原文

我有许多使用 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 一起正常工作,我也愿意更改为另一个日志记录解决方案。

谢谢 蒂姆

I have a number of ASP.NET applications that use log4net without issue. I have not been able to get log4net to log anything in any of the services I wrote using the MVC libraries. I have the configuration identical (aside from log file name) in all the services. Each service is running as the same user and use the same application pool.

Here is the configuration I am using:

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

I have the configSection entry in all, as follows:

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

I have also turned on log4net debugging and set system.diagnostics to write the trace to a file. Here is where I see a difference in the output between the MVC and non-MVC applications.

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]

Non-MVC(Working):

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]

In both cases I am creating the ILog object as follows:

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

Is there something I am missing or some change I need to make to get this working for MVC? At this point I would also be willing to change to another logging solution if it is known to work correctly with MVC.

Thanks
Tim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

伴我老 2024-08-22 03:09:39

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

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

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

如果使用外部配置文件,情况也是如此。
哈特哈,
夏兰

I know this post is quite old but I was getting this issue (default log4net repository) in a WCF project also.
I was using the web.config with a log4net section (not a seperate log4net.config file).

To get around the issue I placed the following in my AssemblyInfo.cs:

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

The same is also true if using an external configuration file though.
HTH,
Ciaran

早乙女 2024-08-22 03:09:39

我的 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 开始正确记录。

My web application is an ASP.NET MVC one with a separate log4net.config file in the web root.

After a lot of googling and hair-pulling I resorted to initializing log4net through code in Application_Start (in Global.asax):

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

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

This solved the problem for me and log4net started logging correctly.

梦毁影碎の 2024-08-22 03:09:39

使用 NLog。似乎有效。我必须将 NLog.config 文件放在基本目录中(与 web.config 一起)并且它工作正常。能够写入正确的目录,这对我来说证明目录权限设置正确。从开始到完成进行更改大约需要 5 分钟。

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

Use NLog. Just seems to work. I had to put the NLog.config file in the base directory (along side the web.config) and it worked fine. Was able to write to the correct directory, which to me proves the directory permissions were setup correctly. Took about 5 minutes start to finish to make the changes.

It would be nice to know why log4net refused to play.

十六岁半 2024-08-22 03:09:39

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

In my case, it was because the project properties were set to output to bin\debug but the log4net dll required that the log4net.config file exists in the BIN folder. I changed the project properties to output to the bin folder and the log4net.config file was then copied at build time into the right place.

酸甜透明夹心 2024-08-22 03:09:39

此外,确保为 log4net 配置文件设置“复制本地”也可以防止许多问题。

Additionally, ensuring "copy local" is set for the log4net config file will prevent many issues as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文