语句已执行但未创建日志文件。什么地方出了错?

时间:2014-03-27 06:28:01

标签: c# asp.net logging log4net

我按照here给出了记录说明:

我创建了一个Assembly.cs,其中包含以下内容:

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

这是我的web.config文件:

<configuration>

    <configSections>
        <section name="log4net"
                 type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
<log4net>
        <appender name="FileAppender"
                  type="log4net.Appender.FileAppender">
            <file value="C:\Users\SOIS\Documents\Visual Studio 2010\WebSites\DummyPharmacy\logfile.txt" />
            <appendToFile value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
            </layout>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="FileAppender" />
        </root>
    </log4net>
</configuration>

我使用logger的连接类有这个: 使用log4net;

public class Connection
{
  private static readonly ILog log = LogManager.GetLogger(typeof(Connection));

调试显示执行日志记录。但是我的文件夹中没有创建文件。出了什么问题?

Execution

我的输出文件包含:

log4net:ERROR Failed to find configuration section 'log4net' 
in the application's .config file.Check your .config file 
for the <log4net> and <configSections> elements. The configuration
section should look like: <section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

我已经制作了一个单独的log4net.config文件。因为编辑现有的不允许我定义元素。

1 个答案:

答案 0 :(得分:1)

这是因为您没有告诉log4net从您的程序集属性中的log4net.config文件中读取配置:

请参阅官方文档中的Configuration Attributes

// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.config in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.

将其更改为以下工作:

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

或者,如果您更喜欢单个.config文件,请保留该属性并将配置从log4net.config移至Web.config

相关问题