c#log4net fillappend什么都没发生

时间:2016-08-19 08:08:32

标签: c# log4net app-config

我需要使用log4net创建.txt并将日志写入.txt我没有知道发生了什么,log4net没有做任何没有创建.txt和写日志。

这是我的log4net.config

<configuration>
    <configSections>   
        <sectionname="log4net"type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>

    <log4net>
        <root>
            <level value="ALL" />
            <appender-ref ref="MyAppender" />
            <appender-ref ref="RollingFileAppender" />
        </root>
        <appender name="MyAppender" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %level %logger - %message%newline" />
            </layout>
        </appender>
        <appender name="MyFileAppender" type="log4net.Appender.FileAppender">
            <file value="application.log" />
            <appendToFile value="true" />
            <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %level %logger - %message%newline" />
            </layout>
        </appender>
        <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
            <file value="rolling.log" />
            <appendToFile value="true" />
            <rollingStyle value="Size" />
            <maxSizeRollBackups value="5" />
            <maximumFileSize value="10MB" />
            <staticLogFileName value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
            </layout>
        </appender>
    </log4net>

    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>       

这是Program.cs使用log.info()和log.error

private static readonly log4net.ILoglog = log4net.LogManager.GetLogge(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


static void Main(string[] args)
{
    log.Info("log_info");
    log.Error("log_error");
}

这是assemblyinfo.cs

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

1 个答案:

答案 0 :(得分:0)

我发现了问题,我有两个.config,一个是App.config,两个是log4net.config,如果只写#34; log4net.config&#34; ,它不会找到log4net.config。因此需要写入log4net.config路径才能正常工作。

            [assembly: log4net.Config.XmlConfigurator(ConfigFile =  @"C:\Users\username\Documents\Visual Studio 2015\Projects\Log\Log\log4net.config", Watch = true)]